json.writer now converts ratios to floats

db4
Slava Pestov 2008-11-15 03:07:55 -06:00
parent ca1f3b5af0
commit e313988bf0
5 changed files with 23 additions and 13 deletions

3
basis/json/json.factor Normal file
View File

@ -0,0 +1,3 @@
IN: json
SINGLETON: json-null

View File

@ -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
{ f } [ "false" json> ] unit-test

View File

@ -1,14 +1,16 @@
! Copyright (C) 2008 Peter Burns.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
arrays vectors hashtables assocs prettyprint ;
USING: kernel peg peg.ebnf math.parser math.private strings math
math.functions sequences arrays vectors hashtables assocs
prettyprint json ;
IN: json.reader
SINGLETON: json-null
<PRIVATE
: grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
PRIVATE>
! Grammar for JSON from RFC 4627
EBNF: json>

View File

@ -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
{ "false" } [ f >json ] unit-test
@ -16,3 +16,5 @@ IN: json.writer.tests
! Random symbols are written simply as strings
SYMBOL: testSymbol
{ <" "testSymbol""> } [ testSymbol >json ] unit-test
[ { 0.5 } ] [ { 1/2 } >json json> ] unit-test

View 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 words namespaces make
prettyprint hashtables mirrors tr json.reader ;
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
@ -24,9 +24,12 @@ M: json-null json-print ( null -- )
M: string json-print ( obj -- )
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 ;
M: real json-print ( num -- )
>float number>string write ;
M: sequence json-print ( array -- )
CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
@ -46,5 +49,4 @@ M: hashtable json-print ( hashtable -- )
{ } assoc>map "," join write
CHAR: } write1 ;
M: object json-print ( object -- )
unparse json-print ;
M: word json-print name>> json-print ;