From bffe13b05da62d816140fe4fe5ba23d337fdeb65 Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Wed, 21 Oct 2020 11:00:53 -0700 Subject: [PATCH 1/7] added counter --- basis/json/reader/reader.factor | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 7d223c06231..932887a77b0 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -10,6 +10,8 @@ IN: json.reader ERROR: not-a-json-number string ; +SYMBOL: counter + : json-number ( char stream -- num char ) [ 1string ] [ "\s\t\r\n,:}]" swap stream-read-until ] bi* [ @@ -109,9 +111,9 @@ DEFER: (read-json-string) { object vector object } declare { { CHAR: \" [ over read-json-string suffix! ] } - { CHAR: [ [ json-open-array ] } + { CHAR: [ [ counter get 1 + counter set json-open-array ] } { CHAR: , [ v-over-push ] } - { CHAR: ] [ json-close-array ] } + { CHAR: ] [ counter get 1 - counter set json-close-array ] } { CHAR: { [ json-open-hash ] } { CHAR: : [ v-pick-push ] } { CHAR: } [ json-close-hash ] } @@ -133,15 +135,22 @@ DEFER: (read-json-string) : first-json-object ( objects -- obj ) [ H{ } clone ] [ first ] if-empty ; +: get-json-object ( objects -- obj ) + { + { [ dup length 1 = counter get 0 = and ] [ first ] } + [ json-error ] + } + cond ; + PRIVATE> : read-json-objects ( -- objects ) - input-stream get json-read-input ; + input-stream get 0 counter set json-read-input ; GENERIC: json> ( string -- object ) M: string json> - [ read-json-objects first-json-object ] with-string-reader ; + [ read-json-objects get-json-object ] with-string-reader ; : path>json ( path -- json ) - utf8 [ read-json-objects first-json-object ] with-file-reader ; + utf8 [ read-json-objects get-json-object ] with-file-reader ; From 38ad20f6c18937122910bf78e7b935c562d7c735 Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Wed, 21 Oct 2020 11:23:24 -0700 Subject: [PATCH 2/7] removed old first-json-object word --- basis/json/reader/reader.factor | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 932887a77b0..8580d3ad839 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -130,11 +130,8 @@ DEFER: (read-json-string) : json-read-input ( stream -- objects ) V{ } clone over '[ _ stream-read1 ] [ scan ] while* nip ; -! If there are no json objects, return an empty hashtable -! This happens for empty files. -: first-json-object ( objects -- obj ) - [ H{ } clone ] [ first ] if-empty ; +! A properly formed JSON file should contain exactly one object with balanced brackets. : get-json-object ( objects -- obj ) { { [ dup length 1 = counter get 0 = and ] [ first ] } From cf7f4f3a5cbab1adafdef0936715fd05296af74f Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Wed, 21 Oct 2020 19:52:35 -0700 Subject: [PATCH 3/7] added tests --- basis/json/reader/reader-tests.factor | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/basis/json/reader/reader-tests.factor b/basis/json/reader/reader-tests.factor index c39a565e9fe..01697b6c087 100644 --- a/basis/json/reader/reader-tests.factor +++ b/basis/json/reader/reader-tests.factor @@ -84,4 +84,11 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test [ "\n\n\n " json> ] [ not-a-json-number? ] must-fail-with -{ H{ } } [ "" json> ] unit-test +! unclosed objects and mismatched brackets are not allowed + [ "[\"a\", +4 +,1," json> ] must-fail + +[ "[]]]" json> ] must-fail + +[ "{[: \"x\"}" json> ] must-fail \ No newline at end of file From 6bc2c02da0064a2a37c6ebfb54dc275e6d9d722a Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Thu, 22 Oct 2020 20:46:22 -0700 Subject: [PATCH 4/7] cleaning up namespace --- basis/json/reader/reader.factor | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 8580d3ad839..e374c98bdc7 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -111,9 +111,9 @@ DEFER: (read-json-string) { object vector object } declare { { CHAR: \" [ over read-json-string suffix! ] } - { CHAR: [ [ counter get 1 + counter set json-open-array ] } + { CHAR: [ [ 1 counter +@ json-open-array ] } { CHAR: , [ v-over-push ] } - { CHAR: ] [ counter get 1 - counter set json-close-array ] } + { CHAR: ] [ -1 counter +@ json-close-array ] } { CHAR: { [ json-open-hash ] } { CHAR: : [ v-pick-push ] } { CHAR: } [ json-close-hash ] } @@ -133,21 +133,17 @@ DEFER: (read-json-string) ! A properly formed JSON file should contain exactly one object with balanced brackets. : get-json-object ( objects -- obj ) - { - { [ dup length 1 = counter get 0 = and ] [ first ] } - [ json-error ] - } - cond ; - + dup length 1 = counter get 0 = and [ first ] [ json-error ] if ; + PRIVATE> : read-json-objects ( -- objects ) - input-stream get 0 counter set json-read-input ; + input-stream get json-read-input ; GENERIC: json> ( string -- object ) M: string json> - [ read-json-objects get-json-object ] with-string-reader ; + [ 0 counter [ read-json-objects get-json-object ] with-variable ] with-string-reader ; : path>json ( path -- json ) - utf8 [ read-json-objects get-json-object ] with-file-reader ; + utf8 [ 0 counter [ read-json-objects get-json-object ] with-variable ] with-file-reader ; From ebb29be499383c3996b0be44442987de7d602435 Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Thu, 22 Oct 2020 20:54:55 -0700 Subject: [PATCH 5/7] spacing --- basis/json/reader/reader.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index e374c98bdc7..780d27cc08d 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -143,7 +143,7 @@ PRIVATE> GENERIC: json> ( string -- object ) M: string json> - [ 0 counter [ read-json-objects get-json-object ] with-variable ] with-string-reader ; + [ 0 counter [ read-json-objects get-json-object ] with-variable ] with-string-reader ; : path>json ( path -- json ) utf8 [ 0 counter [ read-json-objects get-json-object ] with-variable ] with-file-reader ; From fa6fed13d164e7a561c64b9a0b5246bb2b345b8b Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Thu, 22 Oct 2020 21:02:07 -0700 Subject: [PATCH 6/7] minor comment revision --- basis/json/reader/reader.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index 780d27cc08d..8ba83882eef 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -131,7 +131,7 @@ DEFER: (read-json-string) V{ } clone over '[ _ stream-read1 ] [ scan ] while* nip ; -! A properly formed JSON file should contain exactly one object with balanced brackets. +! A properly formed JSON input should contain exactly one object with balanced brackets. : get-json-object ( objects -- obj ) dup length 1 = counter get 0 = and [ first ] [ json-error ] if ; From 79357c04e64ebd6c2a22d3b9a895510213c9e544 Mon Sep 17 00:00:00 2001 From: Abtin Molavi Date: Thu, 22 Oct 2020 21:10:18 -0700 Subject: [PATCH 7/7] one more formatting change --- basis/json/reader/reader-tests.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/json/reader/reader-tests.factor b/basis/json/reader/reader-tests.factor index 01697b6c087..e86b689e1bd 100644 --- a/basis/json/reader/reader-tests.factor +++ b/basis/json/reader/reader-tests.factor @@ -91,4 +91,4 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test [ "[]]]" json> ] must-fail -[ "{[: \"x\"}" json> ] must-fail \ No newline at end of file +[ "{[: \"x\"}" json> ] must-fail