json.writer: Ability to turn jsvar-encode substitution on/off via jsvar-encode? dynamic variable. Added test cases.
parent
40c45e08ca
commit
210228b911
|
@ -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
|
IN: json.writer.tests
|
||||||
|
|
||||||
{ "false" } [ f >json ] unit-test
|
{ "false" } [ f >json ] unit-test
|
||||||
|
@ -18,3 +18,9 @@ SYMBOL: testSymbol
|
||||||
{ """"testSymbol"""" } [ testSymbol >json ] unit-test
|
{ """"testSymbol"""" } [ testSymbol >json ] unit-test
|
||||||
|
|
||||||
[ { 0.5 } ] [ { 1/2 } >json 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
|
||||||
|
|
|
@ -33,6 +33,9 @@ M: real json-print ( num -- )
|
||||||
M: sequence json-print ( array -- )
|
M: sequence json-print ( array -- )
|
||||||
CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
|
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 "-" "_" ;
|
TR: jsvar-encode "-" "_" ;
|
||||||
|
|
||||||
: tuple>fields ( object -- seq )
|
: tuple>fields ( object -- seq )
|
||||||
|
@ -45,7 +48,7 @@ M: tuple json-print ( tuple -- )
|
||||||
|
|
||||||
M: hashtable json-print ( hashtable -- )
|
M: hashtable json-print ( hashtable -- )
|
||||||
CHAR: { write1
|
CHAR: { write1
|
||||||
[ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
|
[ [ swap jsvar-encode? get [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ]
|
||||||
{ } assoc>map "," join write
|
{ } assoc>map "," join write
|
||||||
CHAR: } write1 ;
|
CHAR: } write1 ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue