Update json for inheritance
parent
68e49c8770
commit
ea12d45337
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2006 Chris Double.
|
! Copyright (C) 2006 Chris Double.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel io.streams.string io strings splitting sequences math
|
USING: kernel io.streams.string io strings splitting sequences math
|
||||||
math.parser assocs classes.tuple classes words namespaces
|
math.parser assocs classes words namespaces prettyprint
|
||||||
hashtables ;
|
hashtables mirrors ;
|
||||||
IN: json.writer
|
IN: json.writer
|
||||||
|
|
||||||
#! Writes the object out to a stream in JSON format
|
#! Writes the object out to a stream in JSON format
|
||||||
|
@ -39,21 +39,13 @@ M: sequence json-print ( array -- string )
|
||||||
#! javascript variable names.
|
#! javascript variable names.
|
||||||
[ (jsvar-encode) ] map ;
|
[ (jsvar-encode) ] map ;
|
||||||
|
|
||||||
: slots ( object -- values names )
|
: tuple>fields ( object -- string )
|
||||||
#! Given an object return an array of slots names and a sequence of slot values
|
<mirror> [
|
||||||
#! the slot name and the slot value.
|
[ swap jsvar-encode >json % " : " % >json % ] "" make
|
||||||
[ tuple-slots ] keep class slot-names ;
|
] { } assoc>map ;
|
||||||
|
|
||||||
: slots>fields ( values names -- array )
|
M: tuple json-print ( tuple -- string )
|
||||||
#! Convert the arrays containing the slot names and values
|
CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
|
||||||
#! to an array of strings suitable for describing that slot
|
|
||||||
#! as a field in a javascript object.
|
|
||||||
[
|
|
||||||
[ jsvar-encode >json % " : " % >json % ] "" make
|
|
||||||
] 2map ;
|
|
||||||
|
|
||||||
M: object json-print ( object -- string )
|
|
||||||
CHAR: { write1 slots slots>fields "," join write CHAR: } write1 ;
|
|
||||||
|
|
||||||
M: hashtable json-print ( hashtable -- string )
|
M: hashtable json-print ( hashtable -- string )
|
||||||
CHAR: { write1
|
CHAR: { write1
|
||||||
|
@ -61,3 +53,5 @@ M: hashtable json-print ( hashtable -- string )
|
||||||
{ } assoc>map "," join write
|
{ } assoc>map "," join write
|
||||||
CHAR: } write1 ;
|
CHAR: } write1 ;
|
||||||
|
|
||||||
|
M: object json-print ( object -- string )
|
||||||
|
unparse json-print ;
|
||||||
|
|
Loading…
Reference in New Issue