json.writer now converts ratios to floats
parent
ca1f3b5af0
commit
e313988bf0
|
@ -0,0 +1,3 @@
|
||||||
|
IN: json
|
||||||
|
|
||||||
|
SINGLETON: json-null
|
|
@ -1,4 +1,5 @@
|
||||||
USING: arrays json.reader kernel multiline strings tools.test hashtables ;
|
USING: arrays json.reader kernel multiline strings tools.test
|
||||||
|
hashtables json ;
|
||||||
IN: json.reader.tests
|
IN: json.reader.tests
|
||||||
|
|
||||||
{ f } [ "false" json> ] unit-test
|
{ f } [ "false" json> ] unit-test
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
! Copyright (C) 2008 Peter Burns.
|
! Copyright (C) 2008 Peter Burns.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
|
USING: kernel peg peg.ebnf math.parser math.private strings math
|
||||||
arrays vectors hashtables assocs prettyprint ;
|
math.functions sequences arrays vectors hashtables assocs
|
||||||
|
prettyprint json ;
|
||||||
IN: json.reader
|
IN: json.reader
|
||||||
|
|
||||||
SINGLETON: json-null
|
<PRIVATE
|
||||||
|
|
||||||
|
|
||||||
: grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
|
: grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
! Grammar for JSON from RFC 4627
|
! Grammar for JSON from RFC 4627
|
||||||
EBNF: json>
|
EBNF: json>
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
USING: json.writer tools.test multiline json.reader ;
|
USING: json.writer tools.test multiline json.reader json ;
|
||||||
IN: json.writer.tests
|
IN: json.writer.tests
|
||||||
|
|
||||||
{ "false" } [ f >json ] unit-test
|
{ "false" } [ f >json ] unit-test
|
||||||
|
@ -16,3 +16,5 @@ IN: json.writer.tests
|
||||||
! Random symbols are written simply as strings
|
! Random symbols are written simply as strings
|
||||||
SYMBOL: testSymbol
|
SYMBOL: testSymbol
|
||||||
{ <" "testSymbol""> } [ testSymbol >json ] unit-test
|
{ <" "testSymbol""> } [ testSymbol >json ] unit-test
|
||||||
|
|
||||||
|
[ { 0.5 } ] [ { 1/2 } >json json> ] unit-test
|
|
@ -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
|
USING: accessors kernel io.streams.string io strings splitting
|
||||||
math math.parser assocs classes words namespaces make
|
sequences math math.parser assocs classes words namespaces make
|
||||||
prettyprint hashtables mirrors tr json.reader ;
|
prettyprint hashtables mirrors tr json ;
|
||||||
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
|
||||||
|
@ -24,9 +24,12 @@ M: json-null json-print ( null -- )
|
||||||
M: string json-print ( obj -- )
|
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: number json-print ( num -- )
|
M: integer json-print ( num -- )
|
||||||
number>string write ;
|
number>string write ;
|
||||||
|
|
||||||
|
M: real json-print ( num -- )
|
||||||
|
>float number>string write ;
|
||||||
|
|
||||||
M: sequence json-print ( array -- )
|
M: sequence json-print ( array -- )
|
||||||
CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
|
CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
|
||||||
|
|
||||||
|
@ -46,5 +49,4 @@ M: hashtable json-print ( hashtable -- )
|
||||||
{ } assoc>map "," join write
|
{ } assoc>map "," join write
|
||||||
CHAR: } write1 ;
|
CHAR: } write1 ;
|
||||||
|
|
||||||
M: object json-print ( object -- )
|
M: word json-print name>> json-print ;
|
||||||
unparse json-print ;
|
|
||||||
|
|
Loading…
Reference in New Issue