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." } ;
+{ $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." }
+{ $notes "The full name of this word could be " { $snippet "json-string>object" } "." } ;
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." } ;
-{ json> read-json } related-words
+{ >json json> read-json write-json } related-words
HELP: path>json
{ $values
}
{ $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
+{ path>json path>jsons json>path jsons>path } related-words
HELP: path>jsons
{ $values
HELP: >json
{ $values { "obj" object } { "string" "the object converted to JSON format" } }
-{ $description "Serializes the object into a JSON formatted string." } ;
+{ $description "Serializes the object into a JSON formatted string." }
+{ $notes "The full name of this word could be " { $snippet "object>json-string" } "." } ;
HELP: write-json
{ $values { "obj" object } }
}
{ $see-also >json } ;
-{ >json write-json } related-words
-
{ json-fp-special-error json-allow-fp-special? } related-words
HELP: json-fp-special-error
-USING: hashtables io.streams.string json json.private kernel
-linked-assocs literals math namespaces strings tools.test ;
+USING: hashtables io.encodings.utf8 io.files io.files.unique
+io.streams.string json json.private kernel linked-assocs
+literals math namespaces sequences strings tools.test ;
IN: json.tests
! !!!!!!!!!!!!
LH{ { "baz" 3 } { "qux" 4 } }
} dup >jsonlines jsonlines> =
] unit-test
+
+{ "6" } [ "[1,2,3]" [ sum ] rewrite-json-string ] unit-test
+{ "9\n81" } [ "3\n9" [ [ sq ] map ] rewrite-jsons-string ] unit-test
+
+{ "[1,2]" } [
+ [
+ "[1]" "test-json"
+ [ utf8 set-file-contents ]
+ [ [ { 2 } append ] rewrite-json-path ]
+ [ utf8 file-contents ] tri
+ ] cleanup-unique-directory
+] unit-test
+
+{ "121\n144" } [
+ [
+ "11\n12" "test-jsons"
+ [ utf8 set-file-contents ]
+ [ [ [ sq ] map ] rewrite-jsons-path ]
+ [ utf8 file-contents ] tri
+ ] cleanup-unique-directory
+] unit-test
M: string json>
[ read-json get-json ] with-string-reader ;
-: path>json ( path -- json )
- utf8 [ read-json get-json ] with-file-reader ;
-
-: path>jsons ( path -- jsons )
- utf8 [ read-json ] with-file-reader ;
-
SYMBOL: json-allow-fp-special?
f json-allow-fp-special? set-global
: >jsonlines ( objects -- string )
[ write-jsonlines ] with-string-writer ;
+
+: path>json ( path -- json )
+ utf8 [ read-json get-json ] with-file-reader ;
+
+: path>jsons ( path -- jsons )
+ utf8 [ read-json ] with-file-reader ;
+
+: json>path ( json path -- )
+ utf8 [ write-json ] with-file-writer ;
+
+: jsons>path ( jsons path -- )
+ utf8 [ write-jsonlines ] with-file-writer ;
+
+: rewrite-json-string ( string quot: ( json -- json' ) -- string )
+ [ json> ] dip call >json ; inline
+
+: rewrite-jsons-string ( string quot: ( jsons -- jsons' ) -- string )
+ [ jsonlines> ] dip call >jsonlines ; inline
+
+: rewrite-json-path ( path quot: ( json -- json' ) -- )
+ [ [ path>json ] dip call ] keepd json>path ; inline
+
+: rewrite-jsons-path ( path quot: ( jsons -- jsons' ) -- )
+ [ [ path>jsons ] dip call ] keepd jsons>path ; inline