diff --git a/src/jsx_decoder.erl b/src/jsx_decoder.erl index 2fa6b7d..54b694f 100644 --- a/src/jsx_decoder.erl +++ b/src/jsx_decoder.erl @@ -95,7 +95,7 @@ decoder(Handler, State, Config) -> -define(error(State, Bin, Handler, Acc, Stack, Config), case Config#config.error_handler of false -> erlang:error(badarg); - F -> F(Bin, {decoder, State, Handler, Acc, Stack}, Config) + F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config)) end ). -define(error(State, Bin, Handler, Stack, Config), @@ -121,7 +121,7 @@ decoder(Handler, State, Config) -> end end }; - F -> F(Rest, {decoder, State, Handler, Acc, Stack}, Config) + F -> F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config)) end ). -define(incomplete(State, Rest, Handler, Stack, Config), @@ -139,7 +139,7 @@ decoder(Handler, State, Config) -> end end }; - F -> F(Rest, {decoder, State, Handler, null, Stack}, Config) + F -> F(Rest, {decoder, State, Handler, null, Stack}, jsx_utils:config_to_list(Config)) end ). -endif. diff --git a/src/jsx_encoder.erl b/src/jsx_encoder.erl index 57c0c72..ee607c4 100644 --- a/src/jsx_encoder.erl +++ b/src/jsx_encoder.erl @@ -45,7 +45,7 @@ encoder(Handler, State, Config) -> -define(error(State, Term, Handler, Config), case Config#config.error_handler of false -> erlang:error(badarg); - F -> erlang:throw(F(Term, {encoder, State, Handler}, Config)) + F -> erlang:throw(F(Term, {encoder, State, Handler}, jsx_utils:config_to_list(Config))) end ). -endif. diff --git a/src/jsx_parser.erl b/src/jsx_parser.erl index 4563dd6..a094ec3 100644 --- a/src/jsx_parser.erl +++ b/src/jsx_parser.erl @@ -40,7 +40,7 @@ parser(Handler, State, Config) -> -define(error(State, Terms, Handler, Stack, Config), case Config#config.error_handler of false -> erlang:error(badarg); - F -> F(Terms, {parser, State, Handler, Stack}, Config) + F -> F(Terms, {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config)) end ). @@ -63,7 +63,7 @@ parser(Handler, State, Config) -> State(Tokens, Handler, Stack, Config) end }; - F -> F([], {parser, State, Handler, Stack}, Config) + F -> F([], {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config)) end ). -endif. diff --git a/src/jsx_utils.erl b/src/jsx_utils.erl index 2cd9a56..1b96b7c 100644 --- a/src/jsx_utils.erl +++ b/src/jsx_utils.erl @@ -24,6 +24,7 @@ -module(jsx_utils). -export([parse_config/1]). +-export([config_to_list/1]). -export([extract_config/1, valid_flags/0]). -export([json_escape_sequence/1]). -export([clean_string/2]). @@ -103,6 +104,20 @@ parse_config(Options, Config) -> erlang:error(badarg, [Options, Config]). +config_to_list(Config) -> + lists:map( + fun ({pre_encode, F}) -> {pre_encode, F}; + ({error_handler, F}) -> {error_handler, F}; + ({incomplete_handler, F}) -> {incomplete_handler, F}; + ({Key, true}) -> Key + end, + lists:filter( + fun({_, false}) -> false; (_) -> true end, + lists:zip(record_info(fields, config), tl(tuple_to_list(Config))) + ) + ). + + valid_flags() -> [ replaced_bad_utf8, @@ -117,6 +132,7 @@ valid_flags() -> relax, pre_encode, error_handler, + incomplete_handler, %% deprecated flags pre_encoder, %% pre_encode loose_unicode, %% replaced_bad_utf8 @@ -649,7 +665,52 @@ config_test_() -> )}, {"bad option flag", ?_assertError(badarg, parse_config([error]))} ]. - + + +config_to_list_test_() -> + [ + {"empty config", ?_assertEqual( + [], + config_to_list(#config{}) + )}, + {"all flags", ?_assertEqual( + [ + replaced_bad_utf8, + escaped_forward_slashes, + single_quoted_strings, + unescaped_jsonp, + comments, + dirty_strings, + ignored_bad_escapes, + explicit_end + ], + config_to_list( + #config{ + replaced_bad_utf8=true, + escaped_forward_slashes=true, + explicit_end=true, + single_quoted_strings=true, + unescaped_jsonp=true, + comments=true, + dirty_strings=true, + ignored_bad_escapes=true + } + ) + )}, + {"pre_encode", ?_assertEqual( + [{pre_encode, fun lists:length/1}], + config_to_list(#config{pre_encode=fun lists:length/1}) + )}, + {"error handler", ?_assertEqual( + [{error_handler, fun ?MODULE:fake_error_handler/3}], + config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3}) + )}, + {"incomplete handler", ?_assertEqual( + [{incomplete_handler, fun ?MODULE:fake_error_handler/3}], + config_to_list(#config{incomplete_handler=fun ?MODULE:fake_error_handler/3}) + )} + ]. + fake_error_handler(_, _, _) -> ok.