From 210228b911eb210b5f6f1d239348eb7d95669266 Mon Sep 17 00:00:00 2001
From: otoburb <otoburb@gmail.com>
Date: Thu, 23 Jun 2011 01:28:24 -0400
Subject: [PATCH 1/4] json.writer: Ability to turn jsvar-encode substitution
 on/off via jsvar-encode? dynamic variable. Added test cases.

---
 basis/json/writer/writer-tests.factor | 8 +++++++-
 basis/json/writer/writer.factor       | 7 +++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor
index 692a264d0a..50304d0a85 100644
--- a/basis/json/writer/writer-tests.factor
+++ b/basis/json/writer/writer-tests.factor
@@ -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 
diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor
index e374919039..a72d1383a2 100644
--- a/basis/json/writer/writer.factor
+++ b/basis/json/writer/writer.factor
@@ -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 ;
 

From fb95a19352e171ca7270ddf565a75fccc89e3894 Mon Sep 17 00:00:00 2001
From: otoburb <otoburb@gmail.com>
Date: Mon, 4 Jul 2011 23:14:51 -0400
Subject: [PATCH 2/4] json.writer: Currified jsvar-encode. Removed whitespace
 in tuple>fields json output. Added tests to json.writer-tests.

---
 basis/json/writer/writer-tests.factor | 16 +++++++++++++---
 basis/json/writer/writer.factor       | 14 +++++++-------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor
index 50304d0a85..7f99456cf2 100644
--- a/basis/json/writer/writer-tests.factor
+++ b/basis/json/writer/writer-tests.factor
@@ -1,4 +1,4 @@
-USING: hashtables json.writer tools.test json.reader json namespaces ;
+USING: hashtables json.writer tools.test json.reader json kernel namespaces ;
 IN: json.writer.tests
 
 { "false" } [ f >json ] unit-test
@@ -20,7 +20,17 @@ SYMBOL: testSymbol
 [ { 0.5 } ] [ { 1/2 } >json json> ] unit-test
 
 [ "{\"b-b\":\"asdf\"}" ] 
-    [ "asdf" "b-b" associate f jsvar-encode? [ >json ] with-variable ] unit-test
+    [ f jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
 
 [ "{\"b_b\":\"asdf\"}" ]
-    [ "asdf" "b-b" associate >json ] unit-test 
+    [ t jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
+
+TUPLE: person name age a-a ;
+[ "{\"name\":\"David-David\",\"age\":32,\"a_a\":{\"b_b\":\"asdf\"}}" ]
+    [ t jsvar-encode? 
+        [ "David-David" 32 H{ { "b-b" "asdf" } } person boa >json ] 
+        with-variable ] unit-test
+[ "{\"name\":\"Alpha-Beta\",\"age\":32,\"a-a\":{\"b-b\":\"asdf\"}}" ]
+    [ f jsvar-encode? 
+        [ "Alpha-Beta" 32 H{ { "b-b" "asdf" } } person boa >json ] 
+        with-variable ] unit-test
diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor
index a72d1383a2..65ef7aebe3 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: accessors kernel io.streams.string io strings splitting
 sequences math math.parser assocs classes words namespaces make
-prettyprint hashtables mirrors tr json ;
+prettyprint hashtables mirrors tr json fry ;
 IN: json.writer
 
 #! Writes the object out to a stream in JSON format
@@ -33,23 +33,23 @@ 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 )
-    <mirror> [
-        [ swap jsvar-encode >json % " : " % >json % ] "" make
-    ] { } assoc>map ;
+    <mirror>
+    jsvar-encode? get
+    '[ [ swap _ [ jsvar-encode ] when >json % ":" % >json % ] "" make ] { } assoc>map ;
 
 M: tuple json-print ( tuple -- )
     CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
 
 M: hashtable json-print ( hashtable -- )
     CHAR: { write1 
-    [ [ swap jsvar-encode? get [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] 
-    { } assoc>map "," join write 
+    jsvar-encode? get
+    '[ [ swap _ [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] { } assoc>map
+    "," join write 
     CHAR: } write1 ;
 
 M: word json-print name>> json-print ;

From 063076cfa5824f7073b033b996344ab70bbde9ab Mon Sep 17 00:00:00 2001
From: otoburb <otoburb@gmail.com>
Date: Thu, 23 Jun 2011 01:28:24 -0400
Subject: [PATCH 3/4] json.writer: Ability to turn jsvar-encode substituion
 on/off via jsvar-encode? dynamic variable. Removed whitespace in tuple>fields
 json output. Added tests to json.writer-tests.

---
 basis/json/writer/writer-tests.factor | 18 +++++++++++++++++-
 basis/json/writer/writer.factor       | 17 ++++++++++-------
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor
index 692a264d0a..7f99456cf2 100644
--- a/basis/json/writer/writer-tests.factor
+++ b/basis/json/writer/writer-tests.factor
@@ -1,4 +1,4 @@
-USING: json.writer tools.test json.reader json ;
+USING: hashtables json.writer tools.test json.reader json kernel namespaces ;
 IN: json.writer.tests
 
 { "false" } [ f >json ] unit-test
@@ -18,3 +18,19 @@ SYMBOL: testSymbol
 { """"testSymbol"""" } [ testSymbol >json ] unit-test
 
 [ { 0.5 } ] [ { 1/2 } >json json> ] unit-test
+
+[ "{\"b-b\":\"asdf\"}" ] 
+    [ f jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
+
+[ "{\"b_b\":\"asdf\"}" ]
+    [ t jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
+
+TUPLE: person name age a-a ;
+[ "{\"name\":\"David-David\",\"age\":32,\"a_a\":{\"b_b\":\"asdf\"}}" ]
+    [ t jsvar-encode? 
+        [ "David-David" 32 H{ { "b-b" "asdf" } } person boa >json ] 
+        with-variable ] unit-test
+[ "{\"name\":\"Alpha-Beta\",\"age\":32,\"a-a\":{\"b-b\":\"asdf\"}}" ]
+    [ f jsvar-encode? 
+        [ "Alpha-Beta" 32 H{ { "b-b" "asdf" } } person boa >json ] 
+        with-variable ] unit-test
diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor
index e374919039..65ef7aebe3 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: accessors kernel io.streams.string io strings splitting
 sequences math math.parser assocs classes words namespaces make
-prettyprint hashtables mirrors tr json ;
+prettyprint hashtables mirrors tr json fry ;
 IN: json.writer
 
 #! Writes the object out to a stream in JSON format
@@ -33,20 +33,23 @@ M: real json-print ( num -- )
 M: sequence json-print ( array -- ) 
     CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
 
-TR: jsvar-encode "-" "_" ;
+SYMBOL: jsvar-encode?
+t jsvar-encode? set-global
+TR: jsvar-encode "-" "_" ; 
   
 : tuple>fields ( object -- seq )
-    <mirror> [
-        [ swap jsvar-encode >json % " : " % >json % ] "" make
-    ] { } assoc>map ;
+    <mirror>
+    jsvar-encode? get
+    '[ [ swap _ [ jsvar-encode ] when >json % ":" % >json % ] "" make ] { } assoc>map ;
 
 M: tuple json-print ( tuple -- )
     CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
 
 M: hashtable json-print ( hashtable -- )
     CHAR: { write1 
-    [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
-    { } assoc>map "," join write 
+    jsvar-encode? get
+    '[ [ swap _ [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] { } assoc>map
+    "," join write 
     CHAR: } write1 ;
 
 M: word json-print name>> json-print ;

From b56c2b9589edfe71b43b359b4d24adf428aa2b2c Mon Sep 17 00:00:00 2001
From: otoburb <otoburb@gmail.com>
Date: Tue, 5 Jul 2011 00:07:59 -0400
Subject: [PATCH 4/4] json.writer: Updated docs to mention the dynamic variable
 jsvar-encode?

---
 basis/json/writer/writer-docs.factor | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/basis/json/writer/writer-docs.factor b/basis/json/writer/writer-docs.factor
index 9a6ba5f2b1..6445f0a7cb 100644
--- a/basis/json/writer/writer-docs.factor
+++ b/basis/json/writer/writer-docs.factor
@@ -10,7 +10,9 @@ HELP: >json
 
 HELP: json-print
 { $values { "obj" "an object" } }
-{ $description "Serializes the object into a JSON formatted string and outputs it to the standard output stream." } 
+{ $description "Serializes the object into a JSON formatted string and outputs it to the standard output stream. 
+
+By default, tuples and hashtables are serialized into Javascript-friendly JSON formatted output by converting keys containing dashes into underscores. This behaviour can be modified by setting the dynamic variable " { $strong "jsvar-encode?" } " to false." } 
 { $see-also >json } ;
 
 ARTICLE: "json.writer" "JSON writer"