From 64cdabf57a386e89520edd20809103be6d10544a Mon Sep 17 00:00:00 2001 From: Peter Burns Date: Sat, 8 Nov 2008 12:08:58 -0800 Subject: [PATCH] A bit more refactoring and testing of json.reader --- basis/json/authors.txt | 1 + basis/json/reader/reader-tests.factor | 12 +++++++++++- basis/json/reader/reader.factor | 17 ++++++++++------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/basis/json/authors.txt b/basis/json/authors.txt index 44b06f94bc..914f818278 100755 --- a/basis/json/authors.txt +++ b/basis/json/authors.txt @@ -1 +1,2 @@ Chris Double +Peter Burns \ No newline at end of file diff --git a/basis/json/reader/reader-tests.factor b/basis/json/reader/reader-tests.factor index 84f22f9282..ed444948a7 100644 --- a/basis/json/reader/reader-tests.factor +++ b/basis/json/reader/reader-tests.factor @@ -1,4 +1,4 @@ -USING: arrays json.reader kernel multiline strings tools.test ; +USING: arrays json.reader kernel multiline strings tools.test hashtables ; IN: json.reader.tests { f } [ "false" json> ] unit-test @@ -19,14 +19,24 @@ IN: json.reader.tests { 0.125 } [ "0.125" json> ] unit-test { -0.125 } [ "-0.125" json> ] unit-test +! not widely supported by javascript, but allowed in the grammar, and a nice +! feature to get +{ -0.0 } [ "-0.0" json> ] unit-test + { " fuzzy pickles " } [ <" " fuzzy pickles " "> json> ] unit-test { "while 1:\n\tpass" } [ <" "while 1:\n\tpass" "> json> ] unit-test +! unicode is allowed in json +{ "ß∂¬ƒ˚∆" } [ <" "ß∂¬ƒ˚∆""> json> ] unit-test { 8 9 10 12 13 34 47 92 } >string 1array [ <" "\b\t\n\f\r\"\/\\" "> json> ] unit-test { HEX: abcd } >string 1array [ <" "\uaBCd" "> json> ] unit-test { { } } [ "[]" json> ] unit-test { { 1 "two" 3.0 } } [ <" [1, "two", 3.0] "> json> ] unit-test { H{ } } [ "{}" json> ] unit-test + +! the returned hashtable should be different every time +{ H{ } } [ "key" "value" "{}" json> ?set-at "{}" json> swap drop ] unit-test + { H{ { "US$" 1.0 } { "EU€" 1.5 } } } [ <" { "US$":1.00, "EU\u20AC":1.50 } "> json> ] unit-test { H{ { "fib" { 1 1 2 3 5 8 H{ { "etc" "etc" } } } } diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 9b50a6bf38..858c9ed4de 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -1,11 +1,14 @@ ! 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 prettyprint ; + arrays vectors hashtables assocs prettyprint ; IN: json.reader SINGLETON: json-null + +: grammar-list>vector ( seq -- vec ) first2 values swap prefix ; + ! Grammar for JSON from RFC 4627 EBNF: json> @@ -24,22 +27,22 @@ char = '\\"' [[ CHAR: " ]] | "\\n" [[ CHAR: \n ]] | "\\r" [[ CHAR: \r ]] | "\\t" [[ CHAR: \t ]] - | "\\u" (hex hex hex hex) [[ hex> ]] => [[ 1 swap nth ]] + | "\\u" (hex hex hex hex) [[ hex> ]] => [[ second ]] | [^"\] string = '"' char*:cs '"' => [[ cs >string ]] -sign = ("-" | "+")? => [[ "-" = [ "-" ] [ "" ] if ]] +sign = ("-" | "+")? => [[ "-" = "-" "" ? ]] digits = [0-9]+ => [[ >string ]] decimal = "." digits => [[ concat ]] exp = ("e" | "E") sign digits => [[ concat ]] number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]] -elements = value ("," value)* => [[ first2 [ second ] map swap prefix >array ]] -array = "[" elements?:arr "]" => [[ arr { } or ]] +elements = value ("," value)* => [[ grammar-list>vector ]] +array = "[" elements?:arr "]" => [[ arr >array ]] pair = ws string:key ws ":" value:val => [[ { key val } ]] -members = pair ("," pair)* => [[ first2 [ second ] map swap prefix >hashtable ]] -object = "{" (members)?:hash "}" => [[ hash H{ } or ]] +members = pair ("," pair)* => [[ grammar-list>vector ]] +object = "{" members?:hash "}" => [[ hash >hashtable ]] val = true | false