]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/json/writer/writer.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / json / writer / writer.factor
index a7efe7ad0f37524bbffbf541addda45bf37ab8fb..79c7542dbb4f47d063b5abc11a55590b3c9c64b7 100644 (file)
@@ -1,47 +1,55 @@
 ! Copyright (C) 2006 Chris Double.
 ! 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 ;
+USING: accessors kernel io.streams.string io strings splitting
+sequences math math.parser assocs classes words namespaces make
+prettyprint hashtables mirrors tr json ;
 IN: json.writer
 
 #! Writes the object out to a stream in JSON format
 GENERIC: json-print ( obj -- )
 
 : >json ( obj -- string )
-  #! Returns a string representing the factor object in JSON format
-  [ json-print ] with-string-writer ;
+    #! Returns a string representing the factor object in JSON format
+    [ json-print ] with-string-writer ;
 
 M: t json-print ( f -- )
   drop "true" write ;
 
 M: f json-print ( f -- )
-  drop "false" write ;
+    drop "false" write ;
+
+M: t json-print ( t -- )
+    drop "true" write ;
+
+M: json-null json-print ( null -- )
+    drop "null" write ;
 
 M: string json-print ( obj -- )
-  CHAR: " write1 "\"" split "\\\"" join CHAR: \r swap remove "\n" split "\\r\\n" join write CHAR: " write1 ;
+    CHAR: " write1 "\"" split "\\\"" join CHAR: \r swap remove "\n" split "\\r\\n" join write CHAR: " write1 ;
+
+M: integer json-print ( num -- )
+    number>string write ;
 
-M: number json-print ( num -- )  
-  number>string write ;
+M: real json-print ( num -- )
+    >float number>string write ;
 
 M: sequence json-print ( array -- ) 
-  CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
+    CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
 
 TR: jsvar-encode "-" "_" ;
   
 : tuple>fields ( object -- seq )
-  <mirror> [
-    [ swap jsvar-encode >json % " : " % >json % ] "" make
-  ] { } assoc>map ;
+    <mirror> [
+        [ swap jsvar-encode >json % " : " % >json % ] "" make
+    ] { } assoc>map ;
 
 M: tuple json-print ( tuple -- )
-  CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
+    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 
-  CHAR: } write1 ;
+    CHAR: { write1 
+    [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
+    { } assoc>map "," join write 
+    CHAR: } write1 ;
 
-M: object json-print ( object -- )
-    unparse json-print ;
+M: word json-print name>> json-print ;