diff --git a/basis/json/json.factor b/basis/json/json.factor new file mode 100644 index 0000000000..08d2590ee7 --- /dev/null +++ b/basis/json/json.factor @@ -0,0 +1,3 @@ +IN: json + +SINGLETON: json-null diff --git a/basis/json/reader/reader-tests.factor b/basis/json/reader/reader-tests.factor index fbd91601bf..e97d45babe 100644 --- a/basis/json/reader/reader-tests.factor +++ b/basis/json/reader/reader-tests.factor @@ -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 diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 858c9ed4de..f1520a9272 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -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 - +vector ( seq -- vec ) first2 values swap prefix ; +PRIVATE> + ! Grammar for JSON from RFC 4627 EBNF: json> diff --git a/basis/json/writer/writer-tests.factor b/basis/json/writer/writer-tests.factor index 1b29bac824..6b6118c443 100644 --- a/basis/json/writer/writer-tests.factor +++ b/basis/json/writer/writer-tests.factor @@ -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 @@ -15,4 +15,6 @@ IN: json.writer.tests ! Random symbols are written simply as strings SYMBOL: testSymbol -{ <" "testSymbol""> } [ testSymbol >json ] unit-test \ No newline at end of file +{ <" "testSymbol""> } [ testSymbol >json ] unit-test + +[ { 0.5 } ] [ { 1/2 } >json json> ] unit-test \ No newline at end of file diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor index d50a4de8f5..e374919039 100644 --- a/basis/json/writer/writer.factor +++ b/basis/json/writer/writer.factor @@ -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 ;