]> gitweb.factorcode.org Git - factor.git/commitdiff
json.reader: Add a path>jsons for files that have multiple json objs.
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 11 Mar 2021 01:46:51 +0000 (19:46 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Thu, 11 Mar 2021 02:07:17 +0000 (20:07 -0600)
Lots of people log a stream of json objects to disk. The vocabulary already
handles this case but then path>json discards the results, which is "correct"
but not that useful for this case.

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

index 06b2f3a18a1daf5858af8ea4941edbe867f0dc16..204f630ed74a8e7aaa222d0809e28f8227552afa 100644 (file)
@@ -11,8 +11,24 @@ HELP: read-json
 { $values { "objects" { $sequence "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." } ;
 
+HELP: path>json
+{ $values
+    { "path" "a pathname string" }
+    { "json" "a JSON object" }
+}
+{ $description "Reads a file into a single JSON object. Throws an error if the file contains more than one json." } ;
+{ path>json path>jsons } related-words
+
+HELP: path>jsons
+{ $values
+    { "path" "a pathname string" }
+    { "jsons" "a sequences of JSON objects" }
+}
+{ $description "Reads a file into a sequence of JSON objects and returns them all." } ;
+
 ARTICLE: "json.reader" "JSON reader"
 "The " { $vocab-link "json.reader" } " vocabulary defines a word for parsing strings in JSON format."
-{ $subsections json> read-json } ;
+"For more information, see " { $url "https://en.wikipedia.org/wiki/JSON" } "."
+{ $subsections json> read-json path>json path>jsons } ;
 
 ABOUT: "json.reader"
index 7d5e2e896c2339f7b604a3d2da6f886069faf135..95e39f1e63ea32abf860edbef88de20e14b6d80f 100644 (file)
@@ -147,3 +147,7 @@ M: string json>
 
 : path>json ( path -- json )
     utf8 [ 0 counter [ read-json get-json ] with-variable ] with-file-reader ;
+
+! Read a file with one json object per line
+: path>jsons ( path -- jsons )
+    utf8 [ read-json ] with-file-reader ;