]> gitweb.factorcode.org Git - factor.git/commitdiff
Update json for inheritance
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 30 Mar 2008 08:06:28 +0000 (03:06 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 30 Mar 2008 08:06:28 +0000 (03:06 -0500)
extra/json/writer/writer.factor

index f847bbff68882b245084256b5c51644ba18d984e..1741b96e75d0467ec3d4b7fc59975d2b2f2b3473 100644 (file)
@@ -1,8 +1,8 @@
 ! 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.tuple classes words namespaces 
-       hashtables ;
+       math.parser assocs classes words namespaces prettyprint
+       hashtables mirrors ;
 IN: json.writer
 
 #! Writes the object out to a stream in JSON format
@@ -39,25 +39,19 @@ M: sequence json-print ( array -- string )
   #! javascript variable names.
   [ (jsvar-encode) ] map ;
   
-: slots ( object -- values names )
-  #! Given an object return an array of slots names and a sequence of slot values
-  #! the slot name and the slot value. 
-  [ tuple-slots ] keep class slot-names ;
-
-: slots>fields ( values names -- array )
-  #! Convert the arrays containing the slot names and values
-  #! to an array of strings suitable for describing that slot
-  #! as a field in a javascript object.
-  [ 
-    [ jsvar-encode >json % " : " % >json % ] "" make 
-  ] 2map ;
+: tuple>fields ( object -- string )
+  <mirror> [
+    [ swap jsvar-encode >json % " : " % >json % ] "" make
+  ] { } assoc>map ;
 
-M: object json-print ( object -- string )
-  CHAR: { write1 slots slots>fields "," join write CHAR: } write1 ;
+M: tuple json-print ( tuple -- string )
+  CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
 
 M: hashtable json-print ( hashtable -- string )
   CHAR: { write1 
   [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
   { } assoc>map "," join write 
   CHAR: } write1 ;
-  
+
+M: object json-print ( object -- string )
+    unparse json-print ;