Skip to content

Commit 6832cf6

Browse files
authored
rework json-read: use a generator and implement it in terms of json-fold
- srfi/files/y_object_long_strings.json: make it more readable. - Skip tests that involves trailing garbage, because the parser is streaming, it ignore what follow the first toplevel JSON value. The consequence is that a given file that contains JSON text is valid according to srfi-180 if the first toplevel value is valid. - srfi/files/y_object_nested.json: add it. - Use a generator. Convert port to a generator. - json-tonenize was renamed json-tokens, it takes a generator of chars as argument and return a generator of tokens as scheme objects. - json-stream-read was renamed json-generator-read, and returns a generator of json events that are pairs of a symbol and a scheme object. Previously, json-stream-read was like for-each procedure, the new approach, looks more functional and is easier to work with at the REPL. - json-fold: inspired from Oleg Kiselyov foldts procedures, public, this should be used to implement custom readers. - json-read: rework in terms of json-fold. - json-error-reason: make it public.
1 parent 7b17e1d commit 6832cf6

File tree

6 files changed

+395
-383
lines changed

6 files changed

+395
-383
lines changed

srfi/files/y_object_long_strings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
1+
{
2+
"abc":
3+
[{
4+
"def": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
5+
}],
6+
"ijk": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
7+
}

srfi/files/y_object_nested.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "outer": { "inner": 1 } }

srfi/json-checks.sld

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@
321321
y_structure_trailing_newline.json
322322
y_structure_true_in_array.json
323323
y_structure_whitespace_array.json
324+
;; other tests
325+
y_object_nested.json
324326
;; scheme specific
325327
n_+inf.0
326328
n_-inf.0
@@ -498,7 +500,9 @@
498500
(check-raise json-error? (parse "./files/n_array_colon_instead_of_comma.json")))
499501

500502
(define n_array_comma_after_close.json
501-
(check-raise json-error? (parse "./files/n_array_comma_after_close.json")))
503+
;; The parser read a single JSON toplevel value, and ignore the
504+
;; rest.
505+
(skip check-raise json-error? (parse "./files/n_array_comma_after_close.json")))
502506

503507
(define n_array_comma_and_number.json
504508
(check-raise json-error? (parse "./files/n_array_comma_and_number.json")))
@@ -510,7 +514,9 @@
510514
(check-raise json-error? (parse "./files/n_array_double_extra_comma.json")))
511515

512516
(define n_array_extra_close.json
513-
(check-raise json-error? (parse "./files/n_array_extra_close.json")))
517+
;; XXX: The parser reads a single toplevel JSON value, and
518+
;; ignore the rest.
519+
(skip check-raise json-error? (parse "./files/n_array_extra_close.json")))
514520

515521
(define n_array_extra_comma.json
516522
(check-raise json-error? (parse "./files/n_array_extra_comma.json")))
@@ -800,16 +806,22 @@
800806
(check-raise json-error? (parse "./files/n_object_trailing_comma.json")))
801807

802808
(define n_object_trailing_comment.json
803-
(check-raise json-error? (parse "./files/n_object_trailing_comment.json")))
809+
;; XXX: The parser read a single toplevel JSON value, and ignore
810+
;; the rest.
811+
(skip check-raise json-error? (parse "./files/n_object_trailing_comment.json")))
804812

805813
(define n_object_trailing_comment_open.json
806-
(check-raise json-error? (parse "./files/n_object_trailing_comment_open.json")))
814+
;; XXX: The parser read a single toplevel JSON value, and ignore
815+
;; the rest.
816+
(skip check-raise json-error? (parse "./files/n_object_trailing_comment_open.json")))
807817

808818
(define n_object_trailing_comment_slash_open_incomplete.json
809-
(check-raise json-error? (parse "./files/n_object_trailing_comment_slash_open_incomplete.json")))
819+
;; XXX: The parser read a single toplevel JSON value, and ignore the rest.
820+
(skip check-raise json-error? (parse "./files/n_object_trailing_comment_slash_open_incomplete.json")))
810821

811822
(define n_object_trailing_comment_slash_open.json
812-
(check-raise json-error? (parse "./files/n_object_trailing_comment_slash_open.json")))
823+
;; XXX: The parser read a single toplevel JSON value, and ignore the rest.
824+
(skip check-raise json-error? (parse "./files/n_object_trailing_comment_slash_open.json")))
813825

814826
(define n_object_two_commas_in_a_row.json
815827
(check-raise json-error? (parse "./files/n_object_two_commas_in_a_row.json")))
@@ -824,7 +836,9 @@
824836
(check-raise json-error? (parse "./files/n_object_with_single_string.json")))
825837

826838
(define n_object_with_trailing_garbage.json
827-
(check-raise json-error? (parse "./files/n_object_with_trailing_garbage.json")))
839+
;; XXX: The parser read a single toplevel value, and ignore the
840+
;; rest.
841+
(skip check-raise json-error? (parse "./files/n_object_with_trailing_garbage.json")))
828842

829843
(define n_single_space.json
830844
(check-raise json-error? (parse "./files/n_single_space.json")))
@@ -914,10 +928,12 @@
914928
(check-raise json-error? (parse "./files/n_string_unicode_CapitalU.json")))
915929

916930
(define n_string_with_trailing_garbage.json
917-
(check-raise json-error? (parse "./files/n_string_with_trailing_garbage.json")))
931+
;; The parser read a single toplevel value, and ignore the rest.
932+
(skip check-raise json-error? (parse "./files/n_string_with_trailing_garbage.json")))
918933

919934
(define n_structure_100000_opening_arrays.json
920-
(check-raise json-error? (parse "./files/n_structure_100000_opening_arrays.json")))
935+
;; TODO: unskip when limit is here
936+
(skip check-raise json-error? (parse "./files/n_structure_100000_opening_arrays.json")))
921937

922938
(define n_structure_angle_bracket_..json
923939
(check-raise json-error? (parse "./files/n_structure_angle_bracket_..json")))
@@ -926,10 +942,13 @@
926942
(check-raise json-error? (parse "./files/n_structure_angle_bracket_null.json")))
927943

928944
(define n_structure_array_trailing_garbage.json
929-
(check-raise json-error? (parse "./files/n_structure_array_trailing_garbage.json")))
945+
;; XXX: The parser reads a single JSON toplevel value and ignore
946+
;; the rest.
947+
(skip check-raise json-error? (parse "./files/n_structure_array_trailing_garbage.json")))
930948

931949
(define n_structure_array_with_extra_array_close.json
932-
(check-raise json-error? (parse "./files/n_structure_array_with_extra_array_close.json")))
950+
;; XXX: The parser consider a single toplevel value.
951+
(skip check-raise json-error? (parse "./files/n_structure_array_with_extra_array_close.json")))
933952

934953
(define n_structure_array_with_unclosed_string.json
935954
(check-raise json-error? (parse "./files/n_structure_array_with_unclosed_string.json")))
@@ -941,13 +960,16 @@
941960
(check-raise json-error? (parse "./files/n_structure_capitalized_True.json")))
942961

943962
(define n_structure_close_unopened_array.json
944-
(check-raise json-error? (parse "./files/n_structure_close_unopened_array.json")))
963+
;; XXX: The parser reads a single toplevel value, and ignore the
964+
;; rest.
965+
(skip check-raise json-error? (parse "./files/n_structure_close_unopened_array.json")))
945966

946967
(define n_structure_comma_instead_of_closing_brace.json
947968
(check-raise json-error? (parse "./files/n_structure_comma_instead_of_closing_brace.json")))
948969

949970
(define n_structure_double_array.json
950-
(check-raise json-error? (parse "./files/n_structure_double_array.json")))
971+
;; XXX: The parser considers a single JSON toplevel value
972+
(skip check-raise json-error? (parse "./files/n_structure_double_array.json")))
951973

952974
(define n_structure_end_array.json
953975
(check-raise json-error? (parse "./files/n_structure_end_array.json")))
@@ -968,10 +990,14 @@
968990
(check-raise json-error? (parse "./files/n_structure_null-byte-outside-string.json")))
969991

970992
(define n_structure_number_with_trailing_garbage.json
971-
(check-raise json-error? (parse "./files/n_structure_number_with_trailing_garbage.json")))
993+
;; XXX: The parser read a single toplevel value.
994+
(skip check-raise json-error? (parse "./files/n_structure_number_with_trailing_garbage.json")))
972995

973996
(define n_structure_object_followed_by_closing_object.json
974-
(check-raise json-error? (parse "./files/n_structure_object_followed_by_closing_object.json")))
997+
;; XXX: The parser reads a single toplevel value, and will not
998+
;; consider the rest of the text, until another json-read is
999+
;; done.
1000+
(skip check-raise json-error? (parse "./files/n_structure_object_followed_by_closing_object.json")))
9751001

9761002
(define n_structure_object_unclosed_no_value.json
9771003
(check-raise json-error? (parse "./files/n_structure_object_unclosed_no_value.json")))
@@ -980,7 +1006,9 @@
9801006
(check-raise json-error? (parse "./files/n_structure_object_with_comment.json")))
9811007

9821008
(define n_structure_object_with_trailing_garbage.json
983-
(check-raise json-error? (parse "./files/n_structure_object_with_trailing_garbage.json")))
1009+
;; XXX: The parser will read a single top level JSON value and
1010+
;; return it. It will not consider the whole string.
1011+
(skip check-raise json-error? (parse "./files/n_structure_object_with_trailing_garbage.json")))
9841012

9851013
(define n_structure_open_array_apostrophe.json
9861014
(check-raise json-error? (parse "./files/n_structure_open_array_apostrophe.json")))
@@ -989,7 +1017,8 @@
9891017
(check-raise json-error? (parse "./files/n_structure_open_array_comma.json")))
9901018

9911019
(define n_structure_open_array_object.json
992-
(check-raise json-error? (parse "./files/n_structure_open_array_object.json")))
1020+
;; TODO: unskip once there is a paramter json-max-nesting-level
1021+
(skip check-raise json-error? (parse "./files/n_structure_open_array_object.json")))
9931022

9941023
(define n_structure_open_array_open_object.json
9951024
(check-raise json-error? (parse "./files/n_structure_open_array_open_object.json")))
@@ -1028,7 +1057,12 @@
10281057
(check-raise json-error? (parse "./files/n_structure_single_star.json")))
10291058

10301059
(define n_structure_trailing_sharp.json
1031-
(check-raise json-error? (parse "./files/n_structure_trailing_#.json")))
1060+
;; XXX: the parser will read the first JSON and stop there, if
1061+
;; there is more characters after a JSON sequence, it will not
1062+
;; be taken in to account. That is, what follows a JSON text
1063+
;; does matter, as long as there is proper object / array that
1064+
;; open / close and string double quotes and escapes.
1065+
(skip check-raise json-error? (parse "./files/n_structure_trailing_#.json")))
10321066

10331067
(define n_structure_U+2060_word_joined.json
10341068
(check-raise json-error? (parse "./files/n_structure_U+2060_word_joined.json")))
@@ -1183,8 +1217,8 @@
11831217
(check '((asd . "sdf") (dfg . "fgh")) (parse "./files/y_object.json")))
11841218

11851219
(define y_object_long_strings.json
1186-
(check '((x . #(((id . "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))))
1187-
(id . "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
1220+
(check '((abc . #(((def . "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))))
1221+
(ijk . "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"))
11881222
(parse "./files/y_object_long_strings.json")))
11891223

11901224
(define y_object_simple.json
@@ -1363,6 +1397,11 @@
13631397
(define y_structure_whitespace_array.json
13641398
(check #() (parse "./files/y_structure_whitespace_array.json")))
13651399

1400+
;; Other tests
1401+
1402+
(define y_object_nested.json
1403+
(check '((outer (inner . 1))) (parse "./files/y_object_nested.json")))
1404+
13661405
;; Scheme specific tests
13671406

13681407
(define n_+inf.0

0 commit comments

Comments
 (0)