]> gitweb.factorcode.org Git - factor.git/commitdiff
json.reader: Fix json reader for empty files to parse as H{ }. Add path>json word.
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 3 Aug 2015 19:23:08 +0000 (12:23 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 3 Aug 2015 19:23:08 +0000 (12:23 -0700)
Rename read-jsons

basis/json/reader/reader-docs.factor
basis/json/reader/reader-tests.factor
basis/json/reader/reader.factor

index 488a7c6f8b4e6fb11dd93dd04f6985773c1de73b..9d81b2018f5a32cae4fb63c891239e219dde51fa 100644 (file)
@@ -7,12 +7,12 @@ HELP: json>
 { $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." } ;
 
-HELP: read-jsons
+HELP: read-json-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." } ;
 
 ARTICLE: "json.reader" "JSON reader"
 "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"
index 3bef14e9d3cea648edbc590ba49cbb336edd6c48..200066c17e15c41e2aeeada413e1529cc59c3932 100644 (file)
@@ -61,7 +61,7 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
 { 0 } [ "   0   " json> ] unit-test
 
 { 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
 { 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> ]
 [ not-a-json-number? ] must-fail-with
+
+{ H{ } } [ "" json> ] unit-test
index 5a8fe895a964593f072148bd8f5ca21d3533053a..742d8773f568f54452e83a7d136e672b31db649c 100644 (file)
@@ -1,10 +1,9 @@
 ! Copyright (C) 2008 Peter Burns, 2009 Philipp Winkler
 ! See http://factorcode.org/license.txt for BSD license.
-
-USING: assocs combinators fry io io.streams.string json kernel
-kernel.private math math.order math.parser namespaces sbufs
-sequences sequences.private strings vectors ;
-
+USING: assocs combinators fry io io.encodings.utf8 io.files
+io.streams.string json kernel kernel.private math math.order
+math.parser namespaces sbufs sequences sequences.private strings
+vectors ;
 IN: json.reader
 
 <PRIVATE
@@ -126,15 +125,23 @@ DEFER: (read-json-string)
         [ pick json-number [ suffix! ] dip [ scan ] when*  ]
     } case ;
 
-: stream-json-read ( stream -- objects )
+: json-read-input ( stream -- objects )
     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>
 
-: read-jsons ( -- objects )
-    input-stream get stream-json-read ;
+: read-json-objects ( -- objects )
+    input-stream get json-read-input ;
 
 GENERIC: json> ( string -- object )
 
 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 ;