]> gitweb.factorcode.org Git - factor.git/commitdiff
A bit more refactoring and testing of json.reader
authorPeter Burns <peter@metaweb.com>
Sat, 8 Nov 2008 20:08:58 +0000 (12:08 -0800)
committerPeter Burns <peter@metaweb.com>
Sat, 8 Nov 2008 20:08:58 +0000 (12:08 -0800)
basis/json/authors.txt
basis/json/reader/reader-tests.factor
basis/json/reader/reader.factor

index 44b06f94bce2ac1f87c75319d38703e6ad205a53..914f8182787cb52aad59250d58868741a7a7c0a9 100755 (executable)
@@ -1 +1,2 @@
 Chris Double
+Peter Burns
\ No newline at end of file
index 84f22f9282c0b88c7cd0fca749b04c8c2b468af9..ed444948a72ef41df2c52f4d33e0127fc39eca95 100644 (file)
@@ -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" } } } }
index 9b50a6bf38001e8998175ada14615ba74c8a499d..858c9ed4de0f76edfae126565d6bca849493f81e 100644 (file)
@@ -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