json.writer: Ability to turn jsvar-encode substitution on/off via jsvar-encode? dynamic variable. Added test cases.

db4
otoburb 2011-06-23 01:28:24 -04:00
parent 40c45e08ca
commit 210228b911
2 changed files with 12 additions and 3 deletions

View File

@ -1,4 +1,4 @@
USING: json.writer tools.test json.reader json ;
USING: hashtables json.writer tools.test json.reader json namespaces ;
IN: json.writer.tests
{ "false" } [ f >json ] unit-test
@ -18,3 +18,9 @@ SYMBOL: testSymbol
{ """"testSymbol"""" } [ testSymbol >json ] unit-test
[ { 0.5 } ] [ { 1/2 } >json json> ] unit-test
[ "{\"b-b\":\"asdf\"}" ]
[ "asdf" "b-b" associate f jsvar-encode? [ >json ] with-variable ] unit-test
[ "{\"b_b\":\"asdf\"}" ]
[ "asdf" "b-b" associate >json ] unit-test

View File

@ -33,6 +33,9 @@ M: real json-print ( num -- )
M: sequence json-print ( array -- )
CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
! if jsvar-encode? is true, then implement jsvar-encode
SYMBOL: jsvar-encode?
t jsvar-encode? set-global
TR: jsvar-encode "-" "_" ;
: tuple>fields ( object -- seq )
@ -45,7 +48,7 @@ M: tuple json-print ( tuple -- )
M: hashtable json-print ( hashtable -- )
CHAR: { write1
[ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
[ [ swap jsvar-encode? get [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ]
{ } assoc>map "," join write
CHAR: } write1 ;