From e20e6746a2b1785ed89354e78dc5fc7630636140 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 3 Jul 2011 17:42:44 -0700 Subject: [PATCH] removes all traces of unquoted key and comment support --- src/jsx_common.hrl | 12 +- src/jsx_decoder.hrl | 132 +--------------------- src/jsx_eep0018.erl | 8 +- src/jsx_format.erl | 4 +- src/jsx_verify.erl | 22 +--- test/cases/comments.json | 1 - test/cases/comments.test | 14 --- test/cases/empty_array_with_comment.json | 1 - test/cases/empty_array_with_comment.test | 4 - test/cases/empty_object_with_comment.json | 1 - test/cases/empty_object_with_comment.test | 4 - test/cases/exp.json | 2 +- test/cases/exp.test | 1 - test/cases/fraction.json | 2 +- test/cases/fraction.test | 3 +- test/cases/integer.json | 2 +- test/cases/integer.test | 1 - test/cases/object_with_unquoted_keys.json | 1 - test/cases/object_with_unquoted_keys.test | 23 ---- 19 files changed, 13 insertions(+), 225 deletions(-) delete mode 100644 test/cases/comments.json delete mode 100644 test/cases/comments.test delete mode 100644 test/cases/empty_array_with_comment.json delete mode 100644 test/cases/empty_array_with_comment.test delete mode 100644 test/cases/empty_object_with_comment.json delete mode 100644 test/cases/empty_object_with_comment.test delete mode 100644 test/cases/object_with_unquoted_keys.json delete mode 100644 test/cases/object_with_unquoted_keys.test diff --git a/src/jsx_common.hrl b/src/jsx_common.hrl index 20787ae..cdc660c 100644 --- a/src/jsx_common.hrl +++ b/src/jsx_common.hrl @@ -32,9 +32,7 @@ -type jsx_opts() :: [jsx_opt()]. --type jsx_opt() :: {comments, true | false} - | {escaped_unicode, ascii | codepoint | none} - | {unquoted_keys, true | false} +-type jsx_opt() :: {escaped_unicode, ascii | codepoint | none} | {multi_term, true | false} | {encoding, auto | utf8 @@ -109,8 +107,6 @@ -type decoder_opts() :: [decoder_opt()]. -type decoder_opt() :: {strict, true | false} - | {comments, true | false} - | {unquoted_keys, true | false} | {encoding, supported_utf()} | {label, atom | binary | existing_atom} | {float, true | false}. @@ -118,16 +114,12 @@ -type verify_opts() :: [verify_opt()]. -type verify_opt() :: {strict, true | false} - | {encoding, auto | supported_utf()} - | {unquoted_keys, true | false} - | {comments, true | false}. + | {encoding, auto | supported_utf()}. -type format_opts() :: [format_opt()]. -type format_opt() :: {strict, true | false} | {encoding, auto | supported_utf()} - | {unquoted_keys, true | false} - | {comments, true | false} | {space, integer()} | space | {indent, integer()} diff --git a/src/jsx_decoder.hrl b/src/jsx_decoder.hrl index c046223..a1349d7 100644 --- a/src/jsx_decoder.hrl +++ b/src/jsx_decoder.hrl @@ -30,9 +30,7 @@ %% opts record for decoder -record(opts, { - comments = false, escaped_unicode = codepoint, - unquoted_keys = false, multi_term = false, encoding = auto }). @@ -71,8 +69,6 @@ -define(negative, 16#2D). -define(positive, 16#2B). -%% comments --define(star, 16#2a). %% some useful guards @@ -137,20 +133,10 @@ parse_opts(Opts) -> parse_opts(Opts, #opts{}). parse_opts([], Opts) -> - Opts; -parse_opts([{comments, Value}|Rest], Opts) -> - true = lists:member(Value, [true, false]), - parse_opts(Rest, Opts#opts{comments=Value}); -parse_opts([comments|Rest], Opts) -> - parse_opts(Rest, Opts#opts{comments=true}); + Opts; parse_opts([{escaped_unicode, Value}|Rest], Opts) -> true = lists:member(Value, [ascii, codepoint, none]), parse_opts(Rest, Opts#opts{escaped_unicode=Value}); -parse_opts([{unquoted_keys, Value}|Rest], Opts) -> - true = lists:member(Value, [true, false]), - parse_opts(Rest, Opts#opts{unquoted_keys=Value}); -parse_opts([unquoted_keys|Rest], Opts) -> - parse_opts(Rest, Opts#opts{unquoted_keys=true}); parse_opts([{multi_term, Value}|Rest], Opts) -> true = lists:member(Value, [true, false]), parse_opts(Rest, Opts#opts{multi_term=Value}); @@ -183,8 +169,6 @@ start(<>, Stack, Opts) -> zero(Rest, Stack, Opts, "0"); start(<>, Stack, Opts) when ?is_nonzero(S) -> integer(Rest, Stack, Opts, [S]); -start(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> start(Resume, Stack, Opts) end); start(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -207,8 +191,6 @@ maybe_done(<>, [object|Stack], Opts) -> key(Rest, [key|Stack], Opts); maybe_done(<>, [array|_] = Stack, Opts) -> value(Rest, Stack, Opts); -maybe_done(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> maybe_done(Resume, Stack, Opts) end); maybe_done(Rest, [], #opts{multi_term=true}=Opts) -> {event, end_json, fun() -> start(Rest, [], Opts) end}; maybe_done(Rest, [], Opts) -> @@ -227,8 +209,6 @@ maybe_done(Bin, Stack, Opts) -> done(<>, Opts) when ?is_whitespace(S) -> done(Rest, Opts); -done(<>, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> done(Resume, Opts) end); done(<<>>, Opts) -> {event, end_json, fun() -> {incomplete, fun(end_stream) -> @@ -255,11 +235,6 @@ object(<>, Stack, Opts) -> string(Rest, Stack, Opts, []); object(<>, [key|Stack], Opts) -> {event, end_object, fun() -> maybe_done(Rest, Stack, Opts) end}; -object(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> object(Resume, Stack, Opts) end); -object(<>, Stack, #opts{unquoted_keys=true}=Opts) - when ?is_noncontrol(S) -> - unquoted_key(Rest, Stack, Opts, [S]); object(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -294,8 +269,6 @@ array(<>, Stack, Opts) -> {event, start_array, fun() -> array(Rest, [array|Stack], Opts) end}; array(<>, [array|Stack], Opts) -> {event, end_array, fun() -> maybe_done(Rest, Stack, Opts) end}; -array(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> array(Resume, Stack, Opts) end); array(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -328,8 +301,6 @@ value(<>, Stack, Opts) -> {event, start_object, fun() -> object(Rest, [key|Stack], Opts) end}; value(<>, Stack, Opts) -> {event, start_array, fun() -> array(Rest, [array|Stack], Opts) end}; -value(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> value(Resume, Stack, Opts) end); value(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -346,8 +317,6 @@ colon(<>, Stack, Opts) when ?is_whitespace(S) -> colon(Rest, Stack, Opts); colon(<>, [key|Stack], Opts) -> value(Rest, [object|Stack], Opts); -colon(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> colon(Resume, Stack, Opts) end); colon(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -364,11 +333,6 @@ key(<>, Stack, Opts) when ?is_whitespace(S) -> key(Rest, Stack, Opts); key(<>, Stack, Opts) -> string(Rest, Stack, Opts, []); -key(<>, Stack, #opts{comments=true}=Opts) -> - maybe_comment(Rest, fun(Resume) -> key(Resume, Stack, Opts) end); -key(<>, Stack, #opts{unquoted_keys=true}=Opts) - when ?is_noncontrol(S) -> - unquoted_key(Rest, Stack, Opts, [S]); key(Bin, Stack, Opts) -> case ?partial_codepoint(Bin) of true -> @@ -381,33 +345,6 @@ key(Bin, Stack, Opts) -> end. -unquoted_key(<>, Stack, Opts, Acc) - when ?is_whitespace(S) -> - {event, {key, lists:reverse(Acc)}, fun() -> colon(Rest, Stack, Opts) end}; -unquoted_key(<>, [key|Stack], Opts, Acc) -> - {event, - {key, lists:reverse(Acc)}, - fun() -> value(Rest, [object|Stack], Opts) end - }; -unquoted_key(<>, Stack, Opts, Acc) - when ?is_noncontrol(S) -> - unquoted_key(Rest, Stack, Opts, [S] ++ Acc); -unquoted_key(Bin, Stack, Opts, Acc) -> - case partial_utf(Bin) of - true -> - {incomplete, fun(end_stream) -> - {error, {badjson, Bin}} - ; (Stream) -> - unquoted_key(<>, - Stack, - Opts, - Acc - ) - end} - ; false -> {error, {badjson, Bin}} - end. - - %% string has an additional parameter, an accumulator (Acc) used to hold the %% intermediate representation of the string being parsed. using a list of %% integers representing unicode codepoints is faster than constructing @@ -649,8 +586,6 @@ zero(<>, Stack, Opts, Acc) when ?is_whitespace(S) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(Rest, Stack, Opts) end}; -zero(<>, Stack, #opts{comments=true}=Opts, Acc) -> - maybe_comment(Rest, fun(Resume) -> zero(Resume, Stack, Opts, Acc) end); zero(<<>>, [], Opts, Acc) -> {incomplete, fun(end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> @@ -700,12 +635,6 @@ integer(<>, Stack, Opts, Acc) when ?is_whitespace(S) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(Rest, Stack, Opts) end}; -integer(<>, - Stack, - #opts{comments=true}=Opts, - Acc -) -> - maybe_comment(Rest, fun(Resume) -> integer(Resume, Stack, Opts, Acc) end); integer(<<>>, [], Opts, Acc) -> {incomplete, fun(end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> @@ -774,12 +703,6 @@ decimal(<>, Stack, Opts, Acc) when ?is_whitespace(S) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(Rest, Stack, Opts) end}; -decimal(<>, - Stack, - #opts{comments=true}=Opts, - Acc -) -> - maybe_comment(Rest, fun(Resume) -> decimal(Resume, Stack, Opts, Acc) end); decimal(<<>>, [], Opts, Acc) -> {incomplete, fun(end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> @@ -856,8 +779,6 @@ exp(<>, Stack, Opts, Acc) when ?is_whitespace(S) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(Rest, Stack, Opts) end}; -exp(<>, Stack, #opts{comments=true}=Opts, Acc) -> - maybe_comment(Rest, fun(Resume) -> exp(Resume, Stack, Opts, Acc) end); exp(<<>>, [], Opts, Acc) -> {incomplete, fun(end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> @@ -1014,55 +935,4 @@ null(Bin, Stack, Opts) -> null(<>, Stack, Opts) end} ; false -> {error, {badjson, Bin}} - end. - - -%% comments are c style, ex: /* blah blah */ -%% any unicode character is valid in a comment except the */ sequence which -%% ends the comment. they're implemented as a closure called when the comment -%% ends that returns execution to the point where the comment began. comments -%% are not reported in any way, simply parsed. -maybe_comment(<>, Resume) -> - comment(Rest, Resume); -maybe_comment(Bin, Resume) -> - case ?partial_codepoint(Bin) of - true -> - {incomplete, fun(end_stream) -> - {error, {badjson, Bin}} - ; (Stream) -> - maybe_comment(<>, Resume) - end} - ; false -> {error, {badjson, Bin}} - end. - - -comment(<>, Resume) -> - maybe_comment_done(Rest, Resume); -comment(<<_/?utfx, Rest/binary>>, Resume) -> - comment(Rest, Resume); -comment(Bin, Resume) -> - case ?partial_codepoint(Bin) of - true -> - {incomplete, fun(end_stream) -> - {error, {badjson, Bin}} - ; (Stream) -> - comment(<>, Resume) - end} - ; false -> {error, {badjson, Bin}} - end. - - -maybe_comment_done(<>, Resume) -> - Resume(Rest); -maybe_comment_done(<<_/?utfx, Rest/binary>>, Resume) -> - comment(Rest, Resume); -maybe_comment_done(Bin, Resume) -> - case ?partial_codepoint(Bin) of - true -> - {incomplete, fun(end_stream) -> - {error, {badjson, Bin}} - ; (Stream) -> - maybe_comment_done(<>, Resume) - end} - ; false -> {error, {badjson, Bin}} end. \ No newline at end of file diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index ee56d71..acbc1a3 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -386,13 +386,7 @@ decode_test_() -> {"strict mode", ?_assertError(badarg, json_to_term(<<"1.0">>, [{strict, true}] ) - )}, - {"comments", - ?_assert(json_to_term(<<"[ /* a comment in an empty array */ ]">>, - [{comments, true}] - ) =:= [] - ) - } + )} ]. encode_test_() -> diff --git a/src/jsx_format.erl b/src/jsx_format.erl index 170d4e9..85ca14a 100644 --- a/src/jsx_format.erl +++ b/src/jsx_format.erl @@ -73,12 +73,12 @@ extract_parser_opts(Opts) -> extract_parser_opts([], Acc) -> Acc; extract_parser_opts([{K,V}|Rest], Acc) -> - case lists:member(K, [comments, encoding, unquoted_keys]) of + case lists:member(K, [encoding]) of true -> [{K,V}] ++ Acc ; false -> extract_parser_opts(Rest, Acc) end; extract_parser_opts([K|Rest], Acc) -> - case lists:member(K, [comments, encoding, unquoted_keys]) of + case lists:member(K, [encoding]) of true -> [K] ++ Acc ; false -> extract_parser_opts(Rest, Acc) end. diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index 6979329..e0fbc08 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -52,12 +52,12 @@ extract_parser_opts(Opts) -> extract_parser_opts([], Acc) -> Acc; extract_parser_opts([{K,V}|Rest], Acc) -> - case lists:member(K, [comments, encoding, unquoted_keys]) of + case lists:member(K, [encoding]) of true -> [{K,V}] ++ Acc ; false -> extract_parser_opts(Rest, Acc) end; extract_parser_opts([K|Rest], Acc) -> - case lists:member(K, [comments, encoding, unquoted_keys]) of + case lists:member(K, [encoding]) of true -> [K] ++ Acc ; false -> extract_parser_opts(Rest, Acc) end. @@ -158,15 +158,13 @@ false_test_() -> {"trailing comma", ?_assert(is_json(<<"[ true, false, null, ]">>, []) =:= false) }, - {"unquoted key", ?_assert(is_json(<<"{ key: false }">>, []) =:= false)}, {"repeated key", ?_assert(is_json( <<"{\"key\": true, \"key\": true}">>, [] ) =:= false ) - }, - {"comments", ?_assert(is_json(<<"[ /* a comment */ ]">>, []) =:= false)} + } ]. less_strict_test_() -> @@ -183,20 +181,6 @@ less_strict_test_() -> [{strict, false}] ) =:= true ) - }, - {"comments", - ?_assert(is_json( - <<"[ /* a comment */ ]">>, - [{comments, true}] - ) =:= true - ) - }, - {"unquoted keys", - ?_assert(is_json( - <<"{unquotedkey : true}">>, - [{unquoted_keys, true}] - ) =:= true - ) } ]. diff --git a/test/cases/comments.json b/test/cases/comments.json deleted file mode 100644 index f82d177..0000000 --- a/test/cases/comments.json +++ /dev/null @@ -1 +0,0 @@ -/*preceding comment*/ [/*array open comment*/ "a string"/*string comment*/, 1/*number comment*/, {/*object open comment*/"key"/*post key comment*/:/*colon comment*/[], /*pre key comment*/"another key": 0/*object close comment*/}, true/*literal comment*/] /*concluding comment*/ \ No newline at end of file diff --git a/test/cases/comments.test b/test/cases/comments.test deleted file mode 100644 index c684eaf..0000000 --- a/test/cases/comments.test +++ /dev/null @@ -1,14 +0,0 @@ -{name, "comments"}. -{jsx, [start_array, - {string,"a string"}, - {integer,"1"}, - start_object, - {key,"key"}, - start_array,end_array, - {key,"another key"}, - {integer,"0"}, - end_object, - {literal,true}, - end_array,end_json]}. -{json, "comments.json"}. -{jsx_flags, [{comments,true}]}. diff --git a/test/cases/empty_array_with_comment.json b/test/cases/empty_array_with_comment.json deleted file mode 100644 index b55de2a..0000000 --- a/test/cases/empty_array_with_comment.json +++ /dev/null @@ -1 +0,0 @@ -[ /* this is a comment and should be ignored */ ] \ No newline at end of file diff --git a/test/cases/empty_array_with_comment.test b/test/cases/empty_array_with_comment.test deleted file mode 100644 index 086831b..0000000 --- a/test/cases/empty_array_with_comment.test +++ /dev/null @@ -1,4 +0,0 @@ -{name, "empty_array_with_comment"}. -{jsx, [start_array,end_array,end_json]}. -{json, "empty_array_with_comment.json"}. -{jsx_flags, [{comments,true}]}. diff --git a/test/cases/empty_object_with_comment.json b/test/cases/empty_object_with_comment.json deleted file mode 100644 index fec47cb..0000000 --- a/test/cases/empty_object_with_comment.json +++ /dev/null @@ -1 +0,0 @@ -{/*comment*/} \ No newline at end of file diff --git a/test/cases/empty_object_with_comment.test b/test/cases/empty_object_with_comment.test deleted file mode 100644 index 09e47d9..0000000 --- a/test/cases/empty_object_with_comment.test +++ /dev/null @@ -1,4 +0,0 @@ -{name, "empty_object_with_comment"}. -{jsx, [start_object,end_object,end_json]}. -{json, "empty_object_with_comment.json"}. -{jsx_flags, [{comments,true}]}. diff --git a/test/cases/exp.json b/test/cases/exp.json index 2544928..502c9b3 100644 --- a/test/cases/exp.json +++ b/test/cases/exp.json @@ -1 +1 @@ -[[2.0e7], 2.0e7, {"key":2.0e7, "another key":2.0E7}, 4.2e70/*a comment*/ ] \ No newline at end of file +[[2.0e7], 2.0e7, {"key":2.0e7, "another key":2.0E7}, 4.2e70 ] \ No newline at end of file diff --git a/test/cases/exp.test b/test/cases/exp.test index a79a0e8..490bbbd 100644 --- a/test/cases/exp.test +++ b/test/cases/exp.test @@ -12,4 +12,3 @@ {float,"4.2e70"}, end_array,end_json]}. {json, "exp.json"}. -{jsx_flags, [{comments,true}]}. diff --git a/test/cases/fraction.json b/test/cases/fraction.json index bbe0ffb..5b3f042 100644 --- a/test/cases/fraction.json +++ b/test/cases/fraction.json @@ -1 +1 @@ -[[2.0], 2.0, {"key":2.0e7, "another key":2.0E7}, {"key":2.0, "another key":2.0}, 4.2/*a comment*/ ] \ No newline at end of file +[[2.0], 2.0, {"key":2.0e7, "another key":2.0E7}, {"key":2.0, "another key":2.0}, 4.2 ] \ No newline at end of file diff --git a/test/cases/fraction.test b/test/cases/fraction.test index 6f3aa73..30b1de2 100644 --- a/test/cases/fraction.test +++ b/test/cases/fraction.test @@ -16,5 +16,4 @@ end_object, {float,"4.2"}, end_array,end_json]}. -{json, "fraction.json"}. -{jsx_flags, [{comments,true}]}. +{json, "fraction.json"}. \ No newline at end of file diff --git a/test/cases/integer.json b/test/cases/integer.json index 616cb6b..fb3cf6e 100644 --- a/test/cases/integer.json +++ b/test/cases/integer.json @@ -1 +1 @@ -[[20], 20, {"key":20, "another key":20}, 42/*a comment*/ ] \ No newline at end of file +[[20], 20, {"key":20, "another key":20}, 42 ] \ No newline at end of file diff --git a/test/cases/integer.test b/test/cases/integer.test index b5dbc27..06f75a3 100644 --- a/test/cases/integer.test +++ b/test/cases/integer.test @@ -12,4 +12,3 @@ {integer,"42"}, end_array,end_json]}. {json, "integer.json"}. -{jsx_flags, [{comments,true}]}. diff --git a/test/cases/object_with_unquoted_keys.json b/test/cases/object_with_unquoted_keys.json deleted file mode 100644 index c4b564a..0000000 --- a/test/cases/object_with_unquoted_keys.json +++ /dev/null @@ -1 +0,0 @@ -{foo : "bar", baz:true, false : null,object:{ key : "value" },list:[null,null,null,[],"\n\r\\"]} \ No newline at end of file diff --git a/test/cases/object_with_unquoted_keys.test b/test/cases/object_with_unquoted_keys.test deleted file mode 100644 index f15b287..0000000 --- a/test/cases/object_with_unquoted_keys.test +++ /dev/null @@ -1,23 +0,0 @@ -{name, "object with unquoted keys"}. -{jsx, [start_object, - {key,"foo"}, - {string,"bar"}, - {key,"baz"}, - {literal,true}, - {key,"false"}, - {literal,null}, - {key,"object"}, - start_object, - {key,"key"}, - {string,"value"}, - end_object, - {key,"list"}, - start_array, - {literal,null}, - {literal,null}, - {literal,null}, - start_array,end_array, - {string,"\n\r\\"}, - end_array,end_object,end_json]}. -{json, "object_with_unquoted_keys.json"}. -{jsx_flags, [{unquoted_keys, true}]}.