]> gitweb.factorcode.org Git - factor.git/commitdiff
json.writer: Ability to turn jsvar-encode substitution on/off via jsvar-encode? dynam...
authorotoburb <otoburb@gmail.com>
Thu, 23 Jun 2011 05:28:24 +0000 (01:28 -0400)
committerotoburb <otoburb@gmail.com>
Thu, 23 Jun 2011 05:28:24 +0000 (01:28 -0400)
basis/json/writer/writer-tests.factor
basis/json/writer/writer.factor

index 692a264d0aace72afd76796d0275ad4058fff41d..50304d0a850457e5306b4ad7b0f0e01653d2a774 100644 (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 
index e374919039aedeb2bb0ab078552df33597b822a7..a72d1383a200a268beae87dfc66c62ab93e417bf 100644 (file)
@@ -33,7 +33,10 @@ M: real json-print ( num -- )
 M: sequence json-print ( array -- ) 
     CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
 
-TR: jsvar-encode "-" "_" ;
+! if jsvar-encode? is true, then implement jsvar-encode
+SYMBOL: jsvar-encode?
+t jsvar-encode? set-global
+TR: jsvar-encode "-" "_" ; 
   
 : tuple>fields ( object -- seq )
     <mirror> [
@@ -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 ;