diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor new file mode 100644 index 0000000000..1b29bac824 --- /dev/null +++ b/basis/json/writer/writer-tests.factor @@ -0,0 +1,18 @@ +USING: json.writer tools.test multiline json.reader ; +IN: json.writer.tests + +{ "false" } [ f >json ] unit-test +{ "true" } [ t >json ] unit-test +{ "null" } [ json-null >json ] unit-test +{ "0" } [ 0 >json ] unit-test +{ "102" } [ 102 >json ] unit-test +{ "-102" } [ -102 >json ] unit-test +{ "102.0" } [ 102.0 >json ] unit-test +{ "102.5" } [ 102.5 >json ] unit-test + +{ "[1,\"two\",3.0]" } [ { 1 "two" 3.0 } >json ] unit-test +{ <" {"US$":1.0,"EU€":1.5}"> } [ H{ { "US$" 1.0 } { "EU€" 1.5 } } >json ] unit-test + +! Random symbols are written simply as strings +SYMBOL: testSymbol +{ <" "testSymbol""> } [ testSymbol >json ] unit-test \ No newline at end of file diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor index cbcf426545..4a61aa3438 100644 --- a/basis/json/writer/writer.factor +++ b/basis/json/writer/writer.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: kernel io.streams.string io strings splitting sequences math math.parser assocs classes words namespaces make -prettyprint hashtables mirrors tr ; +prettyprint hashtables mirrors tr json.reader ; IN: json.writer #! Writes the object out to a stream in JSON format @@ -15,6 +15,9 @@ GENERIC: json-print ( obj -- ) M: f json-print ( f -- ) drop "false" write ; +M: t json-print ( t -- ) + drop "true" write + M: string json-print ( obj -- ) CHAR: " write1 "\"" split "\\\"" join CHAR: \r swap remove "\n" split "\\r\\n" join write CHAR: " write1 ; @@ -41,4 +44,4 @@ M: hashtable json-print ( hashtable -- ) CHAR: } write1 ; M: object json-print ( object -- ) - unparse json-print ; + dup json-null = [ "null" write drop ] [ unparse json-print ] if ;