introduce option to skip checking for repeated keys

This commit is contained in:
alisdair sullivan 2014-08-26 22:28:44 -07:00
parent 5d184327e4
commit e2ef23a46a
4 changed files with 24 additions and 1 deletions

View file

@ -39,8 +39,10 @@ main([]) ->
format(frequency:profile({"empty array to json", ?averageN({jsx, encode, [[]]}, 1000)})), format(frequency:profile({"empty array to json", ?averageN({jsx, encode, [[]]}, 1000)})),
format(frequency:profile({"sample tweet to term", ?averageN({jsx, decode, [sample_tweet()]}, 1000)})), format(frequency:profile({"sample tweet to term", ?averageN({jsx, decode, [sample_tweet()]}, 1000)})),
format(frequency:profile({"sample tweet to json", ?averageN({jsx, encode, [jsx:decode(sample_tweet())]}, 1000)})), format(frequency:profile({"sample tweet to json", ?averageN({jsx, encode, [jsx:decode(sample_tweet())]}, 1000)})),
format(frequency:profile({"sample tweet to json (without key checks)", ?averageN({jsx, encode, [jsx:decode(sample_tweet()), [repeat_keys]]}, 1000)})),
format(frequency:profile({"sample github user to term", ?averageN({jsx, decode, [sample_github_user()]}, 1000)})), format(frequency:profile({"sample github user to term", ?averageN({jsx, decode, [sample_github_user()]}, 1000)})),
format(frequency:profile({"sample github user to json", ?averageN({jsx, encode, [jsx:decode(sample_github_user())]}, 1000)})). format(frequency:profile({"sample github user to json", ?averageN({jsx, encode, [jsx:decode(sample_github_user())]}, 1000)})),
format(frequency:profile({"sample github user to json", ?averageN({jsx, encode, [jsx:decode(sample_github_user()), [repeat_keys]]}, 1000)})).
format([]) -> ok; format([]) -> ok;

View file

@ -63,6 +63,8 @@ parse_config([unescaped_jsonp|Rest], Config) ->
parse_config(Rest, Config#config{unescaped_jsonp=true}); parse_config(Rest, Config#config{unescaped_jsonp=true});
parse_config([dirty_strings|Rest], Config) -> parse_config([dirty_strings|Rest], Config) ->
parse_config(Rest, Config#config{dirty_strings=true}); parse_config(Rest, Config#config{dirty_strings=true});
parse_config([repeat_keys|Rest], Config) ->
parse_config(Rest, Config#config{repeat_keys=true});
parse_config([strict|Rest], Config) -> parse_config([strict|Rest], Config) ->
parse_config(Rest, Config#config{strict_comments=true, parse_config(Rest, Config#config{strict_comments=true,
strict_commas=true, strict_commas=true,
@ -146,6 +148,7 @@ valid_flags() ->
escaped_strings, escaped_strings,
unescaped_jsonp, unescaped_jsonp,
dirty_strings, dirty_strings,
repeat_keys,
strict, strict,
stream, stream,
error_handler, error_handler,
@ -184,6 +187,7 @@ config_test_() ->
escaped_strings = true, escaped_strings = true,
unescaped_jsonp = true, unescaped_jsonp = true,
dirty_strings = true, dirty_strings = true,
repeat_keys = true,
strict_comments = true, strict_comments = true,
strict_commas = true, strict_commas = true,
strict_utf8 = true, strict_utf8 = true,
@ -195,6 +199,7 @@ config_test_() ->
escaped_strings, escaped_strings,
unescaped_jsonp, unescaped_jsonp,
dirty_strings, dirty_strings,
repeat_keys,
strict, strict,
stream stream
]) ])
@ -264,6 +269,7 @@ config_to_list_test_() ->
escaped_strings, escaped_strings,
unescaped_jsonp, unescaped_jsonp,
dirty_strings, dirty_strings,
repeat_keys,
stream, stream,
strict strict
], ],
@ -272,6 +278,7 @@ config_to_list_test_() ->
escaped_strings = true, escaped_strings = true,
unescaped_jsonp = true, unescaped_jsonp = true,
dirty_strings = true, dirty_strings = true,
repeat_keys = true,
strict_comments = true, strict_comments = true,
strict_utf8 = true, strict_utf8 = true,
strict_single_quotes = true, strict_single_quotes = true,

View file

@ -3,6 +3,7 @@
escaped_strings = false :: boolean(), escaped_strings = false :: boolean(),
unescaped_jsonp = false :: boolean(), unescaped_jsonp = false :: boolean(),
dirty_strings = false :: boolean(), dirty_strings = false :: boolean(),
repeat_keys = false :: boolean(),
strict_comments = false :: boolean(), strict_comments = false :: boolean(),
strict_commas = false :: boolean(), strict_commas = false :: boolean(),
strict_utf8 = false :: boolean(), strict_utf8 = false :: boolean(),

View file

@ -141,6 +141,19 @@ object([end_object|Tokens], Handler, [{object, _}|Stack], Config) ->
object([{key, Key}|Tokens], Handler, Stack, Config) object([{key, Key}|Tokens], Handler, Stack, Config)
when is_atom(Key); is_binary(Key); is_integer(Key) -> when is_atom(Key); is_binary(Key); is_integer(Key) ->
object([Key|Tokens], Handler, Stack, Config); object([Key|Tokens], Handler, Stack, Config);
object([Key|Tokens], Handler, [{object, _Keys}|Stack], Config=#config{repeat_keys=true})
when is_atom(Key); is_binary(Key); is_integer(Key) ->
try clean_string(fix_key(Key), Config)
of K ->
value(
Tokens,
handle_event({key, K}, Handler, Config),
[{object, []}|Stack],
Config
)
catch error:badarg ->
?error(object, [{string, Key}|Tokens], Handler, Stack, Config)
end;
object([Key|Tokens], Handler, [{object, Keys}|Stack], Config) object([Key|Tokens], Handler, [{object, Keys}|Stack], Config)
when is_atom(Key); is_binary(Key); is_integer(Key) -> when is_atom(Key); is_binary(Key); is_integer(Key) ->
try try