allow a single trailing comma in objects or arrays
This commit is contained in:
parent
c25bb49902
commit
6b43609730
4 changed files with 56 additions and 0 deletions
|
@ -65,6 +65,7 @@ parse_config([dirty_strings|Rest], Config) ->
|
|||
parse_config(Rest, Config#config{dirty_strings=true});
|
||||
parse_config([strict|Rest], Config) ->
|
||||
parse_config(Rest, Config#config{strict_comments=true,
|
||||
strict_commas=true,
|
||||
strict_utf8=true,
|
||||
strict_single_quotes=true,
|
||||
strict_escapes=true
|
||||
|
@ -89,6 +90,8 @@ parse_config(_Options, _Config) -> erlang:error(badarg).
|
|||
parse_strict([], Rest, Config) -> parse_config(Rest, Config);
|
||||
parse_strict([comments|Strict], Rest, Config) ->
|
||||
parse_strict(Strict, Rest, Config#config{strict_comments=true});
|
||||
parse_strict([trailing_commas|Strict], Rest, Config) ->
|
||||
parse_strict(Strict, Rest, Config#config{strict_commas=true});
|
||||
parse_strict([utf8|Strict], Rest, Config) ->
|
||||
parse_strict(Strict, Rest, Config#config{strict_utf8=true});
|
||||
parse_strict([single_quotes|Strict], Rest, Config) ->
|
||||
|
@ -182,6 +185,7 @@ config_test_() ->
|
|||
unescaped_jsonp = true,
|
||||
dirty_strings = true,
|
||||
strict_comments = true,
|
||||
strict_commas = true,
|
||||
strict_utf8 = true,
|
||||
strict_single_quotes = true,
|
||||
strict_escapes = true,
|
||||
|
@ -199,6 +203,7 @@ config_test_() ->
|
|||
{"strict flag",
|
||||
?_assertEqual(
|
||||
#config{strict_comments = true,
|
||||
strict_commas = true,
|
||||
strict_utf8 = true,
|
||||
strict_single_quotes = true,
|
||||
strict_escapes = true
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
unescaped_jsonp = false :: boolean(),
|
||||
dirty_strings = false :: boolean(),
|
||||
strict_comments = false :: boolean(),
|
||||
strict_commas = false :: boolean(),
|
||||
strict_utf8 = false :: boolean(),
|
||||
strict_single_quotes = false :: boolean(),
|
||||
strict_escapes = false :: boolean(),
|
||||
|
|
|
@ -211,6 +211,8 @@ value(<<?start_array, Rest/binary>>, Handler, Stack, Config) ->
|
|||
array(Rest, handle_event(start_array, Handler, Config), [array|Stack], Config);
|
||||
value(<<S, Rest/binary>>, Handler, Stack, Config) when ?is_whitespace(S) ->
|
||||
value(Rest, Handler, Stack, Config);
|
||||
value(<<?end_array, _/binary>> = Rest, Handler, Stack, Config=#config{strict_commas=false}) ->
|
||||
maybe_done(Rest, Handler, Stack, Config);
|
||||
value(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
||||
?error(value, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
||||
value(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
||||
|
@ -289,6 +291,8 @@ key(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_
|
|||
string(Rest, Handler, new_seq(), [singlequote|Stack], Config);
|
||||
key(<<S, Rest/binary>>, Handler, Stack, Config) when ?is_whitespace(S) ->
|
||||
key(Rest, Handler, Stack, Config);
|
||||
key(<<?end_object, Rest/binary>>, Handler, [key|Stack], Config=#config{strict_commas=false}) ->
|
||||
maybe_done(<<?end_object, Rest/binary>>, Handler, [object|Stack], Config);
|
||||
key(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
||||
?error(key, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
||||
key(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
||||
|
@ -1627,6 +1631,43 @@ bom_test_() ->
|
|||
].
|
||||
|
||||
|
||||
trailing_comma_test_() ->
|
||||
[
|
||||
{"trailing comma in object", ?_assertEqual(
|
||||
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
||||
decode(<<"{\"key\": true,}">>, [])
|
||||
)},
|
||||
{"strict trailing comma in object", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"{\"key\": true,}">>, [{strict, [trailing_commas]}])
|
||||
)},
|
||||
{"two trailing commas in object", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"{\"key\": true,,}">>, [])
|
||||
)},
|
||||
{"comma in empty object", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"{,}">>, [])
|
||||
)},
|
||||
{"trailing comma in list", ?_assertEqual(
|
||||
[start_array, {literal, true}, end_array, end_json],
|
||||
decode(<<"[true,]">>, [])
|
||||
)},
|
||||
{"strict trailing comma in list", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"[true,]">>, [{strict, [trailing_commas]}])
|
||||
)},
|
||||
{"two trailing commas in list", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"[true,,]">>, [])
|
||||
)},
|
||||
{"comma in empty list", ?_assertError(
|
||||
badarg,
|
||||
decode(<<"[,]">>, [])
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
incomplete_test_() ->
|
||||
[
|
||||
{"stream false", ?_assertError(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue