Added some tests for json.writer, fixed a bug with t >json and json-null >json

db4
Peter Burns 2008-11-06 04:26:49 -08:00
parent c2117d4046
commit 260862603c
2 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -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 ;