json.reader: Fix json reader for empty files to parse as H{ }. Add path>json word.

Rename read-jsons
db4
Doug Coleman 2015-08-03 12:23:08 -07:00
parent 876a7f2301
commit abb8bd74b9
3 changed files with 21 additions and 12 deletions

View File

@ -7,12 +7,12 @@ HELP: json>
{ $values { "string" "a string in JSON format" } { "object" "a deserialized object" } } { $values { "string" "a string in JSON format" } { "object" "a deserialized object" } }
{ $description "Deserializes the JSON formatted string into a Factor object. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ; { $description "Deserializes the JSON formatted string into a Factor object. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ;
HELP: read-jsons HELP: read-json-objects
{ $values { "objects" "a vector of deserialized objects" } } { $values { "objects" "a vector of deserialized objects" } }
{ $description "Reads JSON formatted strings into a vector of Factor object until the end of the stream is reached. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ; { $description "Reads JSON formatted strings into a vector of Factor object until the end of the stream is reached. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ;
ARTICLE: "json.reader" "JSON reader" ARTICLE: "json.reader" "JSON reader"
"The " { $vocab-link "json.reader" } " vocabulary defines a word for parsing strings in JSON format." "The " { $vocab-link "json.reader" } " vocabulary defines a word for parsing strings in JSON format."
{ $subsections json> read-jsons } ; { $subsections json> read-json-objects } ;
ABOUT: "json.reader" ABOUT: "json.reader"

View File

@ -61,7 +61,7 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
{ 0 } [ " 0 " json> ] unit-test { 0 } [ " 0 " json> ] unit-test
{ V{ H{ { "a" "b" } } H{ { "c" "d" } } } } { V{ H{ { "a" "b" } } H{ { "c" "d" } } } }
[ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-jsons ] with-string-reader ] unit-test [ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-json-objects ] with-string-reader ] unit-test
! empty objects are allowed as values in objects ! empty objects are allowed as values in objects
{ H{ { "foo" H{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test { H{ { "foo" H{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test
@ -79,3 +79,5 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
[ "<!doctype html>\n<html>\n<head>\n " json> ] [ "<!doctype html>\n<html>\n<head>\n " json> ]
[ not-a-json-number? ] must-fail-with [ not-a-json-number? ] must-fail-with
{ H{ } } [ "" json> ] unit-test

View File

@ -1,10 +1,9 @@
! Copyright (C) 2008 Peter Burns, 2009 Philipp Winkler ! Copyright (C) 2008 Peter Burns, 2009 Philipp Winkler
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators fry io io.encodings.utf8 io.files
USING: assocs combinators fry io io.streams.string json kernel io.streams.string json kernel kernel.private math math.order
kernel.private math math.order math.parser namespaces sbufs math.parser namespaces sbufs sequences sequences.private strings
sequences sequences.private strings vectors ; vectors ;
IN: json.reader IN: json.reader
<PRIVATE <PRIVATE
@ -126,15 +125,23 @@ DEFER: (read-json-string)
[ pick json-number [ suffix! ] dip [ scan ] when* ] [ pick json-number [ suffix! ] dip [ scan ] when* ]
} case ; } case ;
: stream-json-read ( stream -- objects ) : json-read-input ( stream -- objects )
V{ } clone over '[ _ stream-read1 dup ] [ scan ] while drop nip ; V{ } clone over '[ _ stream-read1 dup ] [ scan ] while drop nip ;
! If there are no json objects, return an empty hashtable
! This happens for empty files.
: first-json-object ( objects -- obj )
[ H{ } clone ] [ first ] if-empty ;
PRIVATE> PRIVATE>
: read-jsons ( -- objects ) : read-json-objects ( -- objects )
input-stream get stream-json-read ; input-stream get json-read-input ;
GENERIC: json> ( string -- object ) GENERIC: json> ( string -- object )
M: string json> M: string json>
[ read-jsons first ] with-string-reader ; [ read-json-objects first-json-object ] with-string-reader ;
: path>json ( path -- json )
utf8 [ read-json-objects first-json-object ] with-file-reader ;