remove pre_encode support
This commit is contained in:
parent
0238916472
commit
07d84e25d0
3 changed files with 10 additions and 175 deletions
|
@ -65,11 +65,6 @@ parse_config([relax|Rest], Config) ->
|
||||||
comments = true,
|
comments = true,
|
||||||
ignored_bad_escapes = true
|
ignored_bad_escapes = true
|
||||||
});
|
});
|
||||||
parse_config([{pre_encode, Encoder}|Rest] = Options, Config) when is_function(Encoder, 1) ->
|
|
||||||
case Config#config.pre_encode of
|
|
||||||
false -> parse_config(Rest, Config#config{pre_encode=Encoder})
|
|
||||||
; _ -> erlang:error(badarg, [Options, Config])
|
|
||||||
end;
|
|
||||||
parse_config([{error_handler, ErrorHandler}|Rest] = Options, Config) when is_function(ErrorHandler, 3) ->
|
parse_config([{error_handler, ErrorHandler}|Rest] = Options, Config) when is_function(ErrorHandler, 3) ->
|
||||||
case Config#config.error_handler of
|
case Config#config.error_handler of
|
||||||
false -> parse_config(Rest, Config#config{error_handler=ErrorHandler})
|
false -> parse_config(Rest, Config#config{error_handler=ErrorHandler})
|
||||||
|
@ -81,11 +76,6 @@ parse_config([{incomplete_handler, IncompleteHandler}|Rest] = Options, Config) w
|
||||||
; _ -> erlang:error(badarg, [Options, Config])
|
; _ -> erlang:error(badarg, [Options, Config])
|
||||||
end;
|
end;
|
||||||
%% deprecated flags
|
%% deprecated flags
|
||||||
parse_config([{pre_encoder, Encoder}|Rest] = Options, Config) when is_function(Encoder, 1) ->
|
|
||||||
case Config#config.pre_encode of
|
|
||||||
false -> parse_config(Rest, Config#config{pre_encode=Encoder})
|
|
||||||
; _ -> erlang:error(badarg, [Options, Config])
|
|
||||||
end;
|
|
||||||
parse_config([loose_unicode|Rest], Config) ->
|
parse_config([loose_unicode|Rest], Config) ->
|
||||||
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
||||||
parse_config([escape_forward_slash|Rest], Config) ->
|
parse_config([escape_forward_slash|Rest], Config) ->
|
||||||
|
@ -104,8 +94,7 @@ parse_config(Options, Config) ->
|
||||||
|
|
||||||
config_to_list(Config) ->
|
config_to_list(Config) ->
|
||||||
lists:map(
|
lists:map(
|
||||||
fun ({pre_encode, F}) -> {pre_encode, F};
|
fun ({error_handler, F}) -> {error_handler, F};
|
||||||
({error_handler, F}) -> {error_handler, F};
|
|
||||||
({incomplete_handler, F}) -> {incomplete_handler, F};
|
({incomplete_handler, F}) -> {incomplete_handler, F};
|
||||||
({Key, true}) -> Key
|
({Key, true}) -> Key
|
||||||
end,
|
end,
|
||||||
|
@ -128,11 +117,9 @@ valid_flags() ->
|
||||||
ignored_bad_escapes,
|
ignored_bad_escapes,
|
||||||
explicit_end,
|
explicit_end,
|
||||||
relax,
|
relax,
|
||||||
pre_encode,
|
|
||||||
error_handler,
|
error_handler,
|
||||||
incomplete_handler,
|
incomplete_handler,
|
||||||
%% deprecated flags
|
%% deprecated flags
|
||||||
pre_encoder, %% pre_encode
|
|
||||||
loose_unicode, %% replaced_bad_utf8
|
loose_unicode, %% replaced_bad_utf8
|
||||||
escape_forward_slash, %% escaped_forward_slashes
|
escape_forward_slash, %% escaped_forward_slashes
|
||||||
single_quotes, %% single_quoted_strings
|
single_quotes, %% single_quoted_strings
|
||||||
|
@ -202,7 +189,6 @@ config_test_() ->
|
||||||
},
|
},
|
||||||
{"deprecated flags", ?_assertEqual(
|
{"deprecated flags", ?_assertEqual(
|
||||||
#config{
|
#config{
|
||||||
pre_encode=fun lists:length/1,
|
|
||||||
replaced_bad_utf8=true,
|
replaced_bad_utf8=true,
|
||||||
escaped_forward_slashes=true,
|
escaped_forward_slashes=true,
|
||||||
single_quoted_strings=true,
|
single_quoted_strings=true,
|
||||||
|
@ -211,7 +197,6 @@ config_test_() ->
|
||||||
ignored_bad_escapes=true
|
ignored_bad_escapes=true
|
||||||
},
|
},
|
||||||
parse_config([
|
parse_config([
|
||||||
{pre_encoder, fun lists:length/1},
|
|
||||||
loose_unicode,
|
loose_unicode,
|
||||||
escape_forward_slash,
|
escape_forward_slash,
|
||||||
single_quotes,
|
single_quotes,
|
||||||
|
@ -220,17 +205,6 @@ config_test_() ->
|
||||||
ignore_bad_escapes
|
ignore_bad_escapes
|
||||||
])
|
])
|
||||||
)},
|
)},
|
||||||
{"pre_encode flag", ?_assertEqual(
|
|
||||||
#config{pre_encode=fun lists:length/1},
|
|
||||||
parse_config([{pre_encode, fun lists:length/1}])
|
|
||||||
)},
|
|
||||||
{"two pre_encoders defined", ?_assertError(
|
|
||||||
badarg,
|
|
||||||
parse_config([
|
|
||||||
{pre_encode, fun(_) -> true end},
|
|
||||||
{pre_encode, fun(_) -> false end}
|
|
||||||
])
|
|
||||||
)},
|
|
||||||
{"error_handler flag", ?_assertEqual(
|
{"error_handler flag", ?_assertEqual(
|
||||||
#config{error_handler=fun ?MODULE:fake_error_handler/3},
|
#config{error_handler=fun ?MODULE:fake_error_handler/3},
|
||||||
parse_config([{error_handler, fun ?MODULE:fake_error_handler/3}])
|
parse_config([{error_handler, fun ?MODULE:fake_error_handler/3}])
|
||||||
|
@ -287,10 +261,6 @@ config_to_list_test_() ->
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)},
|
)},
|
||||||
{"pre_encode", ?_assertEqual(
|
|
||||||
[{pre_encode, fun lists:length/1}],
|
|
||||||
config_to_list(#config{pre_encode=fun lists:length/1})
|
|
||||||
)},
|
|
||||||
{"error handler", ?_assertEqual(
|
{"error handler", ?_assertEqual(
|
||||||
[{error_handler, fun ?MODULE:fake_error_handler/3}],
|
[{error_handler, fun ?MODULE:fake_error_handler/3}],
|
||||||
config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3})
|
config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3})
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
dirty_strings = false,
|
dirty_strings = false,
|
||||||
ignored_bad_escapes = false,
|
ignored_bad_escapes = false,
|
||||||
explicit_end = false,
|
explicit_end = false,
|
||||||
pre_encode = false,
|
|
||||||
error_handler = false,
|
error_handler = false,
|
||||||
incomplete_handler = false
|
incomplete_handler = false
|
||||||
}).
|
}).
|
|
@ -52,7 +52,7 @@ encoder(Handler, State, Config) ->
|
||||||
|
|
||||||
|
|
||||||
start(Term, {Handler, State}, Config) ->
|
start(Term, {Handler, State}, Config) ->
|
||||||
try Handler:handle_event(end_json, value(pre_encode(Term, Config), {Handler, State}, Config))
|
try Handler:handle_event(end_json, value(Term, {Handler, State}, Config))
|
||||||
catch
|
catch
|
||||||
throw:Error -> Error;
|
throw:Error -> Error;
|
||||||
Type:Value -> erlang:Type(Value)
|
Type:Value -> erlang:Type(Value)
|
||||||
|
@ -77,23 +77,16 @@ value(List, Handler, Config) when is_list(List) ->
|
||||||
value(Term, Handler, Config) -> ?error(value, Term, Handler, Config).
|
value(Term, Handler, Config) -> ?error(value, Term, Handler, Config).
|
||||||
|
|
||||||
|
|
||||||
list_or_object([Term|Rest], {Handler, State}, Config) ->
|
list_or_object([{K, V}|Rest], {Handler, State}, Config) when is_atom(K); is_binary(K) ->
|
||||||
case pre_encode(Term, Config) of
|
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Config);
|
||||||
{K, V} when is_atom(K); is_binary(K) ->
|
list_or_object(Terms, {Handler, State}, Config) when is_list(Terms) ->
|
||||||
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Config)
|
list(Terms, {Handler, Handler:handle_event(start_array, State)}, Config).
|
||||||
; T ->
|
|
||||||
list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Config)
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
object([{Key, Value}, Next|Rest], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
|
object([{Key, Value}, Next|Rest], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
|
||||||
V = pre_encode(Value, Config),
|
object([Next|Rest], {Handler,
|
||||||
object(
|
|
||||||
[pre_encode(Next, Config)|Rest],
|
|
||||||
{
|
|
||||||
Handler,
|
|
||||||
value(
|
value(
|
||||||
V,
|
Value,
|
||||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
||||||
Config
|
Config
|
||||||
)
|
)
|
||||||
|
@ -106,7 +99,7 @@ object([{Key, Value}], {Handler, State}, Config) when is_atom(Key); is_binary(Ke
|
||||||
{
|
{
|
||||||
Handler,
|
Handler,
|
||||||
value(
|
value(
|
||||||
pre_encode(Value, Config),
|
Value,
|
||||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
||||||
Config
|
Config
|
||||||
)
|
)
|
||||||
|
@ -118,14 +111,11 @@ object(Term, Handler, Config) -> ?error(object, Term, Handler, Config).
|
||||||
|
|
||||||
|
|
||||||
list([Value, Next|Rest], {Handler, State}, Config) ->
|
list([Value, Next|Rest], {Handler, State}, Config) ->
|
||||||
list([pre_encode(Next, Config)|Rest], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
list([Next|Rest], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
||||||
list([Value], {Handler, State}, Config) ->
|
list([Value], {Handler, State}, Config) ->
|
||||||
list([], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
list([], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
||||||
list([], {Handler, State}, _Config) -> Handler:handle_event(end_array, State).
|
list([], {Handler, State}, _Config) -> Handler:handle_event(end_array, State).
|
||||||
|
|
||||||
pre_encode(Value, #config{pre_encode=false}) -> Value;
|
|
||||||
pre_encode(Value, Config) -> (Config#config.pre_encode)(Value).
|
|
||||||
|
|
||||||
|
|
||||||
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
|
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
|
||||||
fix_key(Key) when is_binary(Key) -> Key.
|
fix_key(Key) when is_binary(Key) -> Key.
|
||||||
|
@ -160,130 +150,6 @@ encode_test_() ->
|
||||||
|
|
||||||
encode(Term, Config) -> start(Term, {jsx, []}, jsx_config:parse_config(Config)).
|
encode(Term, Config) -> start(Term, {jsx, []}, jsx_config:parse_config(Config)).
|
||||||
|
|
||||||
pre_encoders_test_() ->
|
|
||||||
Term = [
|
|
||||||
{<<"object">>, [
|
|
||||||
{<<"literals">>, [true, false, null]},
|
|
||||||
{<<"strings">>, [<<"foo">>, <<"bar">>, <<"baz">>]},
|
|
||||||
{<<"numbers">>, [1, 1.0, 1.0e0]}
|
|
||||||
]}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
{"no pre encode", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"object">>}, start_object,
|
|
||||||
{key, <<"literals">>}, start_array,
|
|
||||||
{literal, true}, {literal, false}, {literal, null},
|
|
||||||
end_array,
|
|
||||||
{key, <<"strings">>}, start_array,
|
|
||||||
{string, <<"foo">>}, {string, <<"bar">>}, {string, <<"baz">>},
|
|
||||||
end_array,
|
|
||||||
{key, <<"numbers">>}, start_array,
|
|
||||||
{integer, 1}, {float, 1.0}, {float, 1.0},
|
|
||||||
end_array,
|
|
||||||
end_object,
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode(Term, [])
|
|
||||||
)},
|
|
||||||
{"replace lists with empty lists", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"object">>}, start_object,
|
|
||||||
{key, <<"literals">>}, start_array, end_array,
|
|
||||||
{key, <<"strings">>}, start_array, end_array,
|
|
||||||
{key, <<"numbers">>}, start_array, end_array,
|
|
||||||
end_object,
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end}])
|
|
||||||
)},
|
|
||||||
{"replace objects with empty objects", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end}])
|
|
||||||
)},
|
|
||||||
{"replace all non-list and non_tuple values with false", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"object">>}, start_object,
|
|
||||||
{key, <<"literals">>}, start_array,
|
|
||||||
{literal, false}, {literal, false}, {literal, false},
|
|
||||||
end_array,
|
|
||||||
{key, <<"strings">>}, start_array,
|
|
||||||
{literal, false}, {literal, false}, {literal, false},
|
|
||||||
end_array,
|
|
||||||
{key, <<"numbers">>}, start_array,
|
|
||||||
{literal, false}, {literal, false}, {literal, false},
|
|
||||||
end_array,
|
|
||||||
end_object,
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode(Term, [{pre_encode, fun(V) when is_list(V); is_tuple(V) -> V; (_) -> false end}])
|
|
||||||
)},
|
|
||||||
{"replace all atoms with atom_to_list", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"object">>}, start_object,
|
|
||||||
{key, <<"literals">>}, start_array,
|
|
||||||
{string, <<"true">>}, {string, <<"false">>}, {string, <<"null">>},
|
|
||||||
end_array,
|
|
||||||
{key, <<"strings">>}, start_array,
|
|
||||||
{string, <<"foo">>}, {string, <<"bar">>}, {string, <<"baz">>},
|
|
||||||
end_array,
|
|
||||||
{key, <<"numbers">>}, start_array,
|
|
||||||
{integer, 1}, {float, 1.0}, {float, 1.0},
|
|
||||||
end_array,
|
|
||||||
end_object,
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode(Term, [{pre_encode, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}])
|
|
||||||
)},
|
|
||||||
{"pre_encode tuple", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_array,
|
|
||||||
{integer, 1}, {integer, 2}, {integer, 3},
|
|
||||||
end_array,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode({1, 2, 3}, [{pre_encode, fun(Tuple) when is_tuple(Tuple) -> tuple_to_list(Tuple); (V) -> V end}])
|
|
||||||
)},
|
|
||||||
{"pre_encode 2-tuples", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"two">>}, {integer, 2}, {key, <<"three">>}, {integer, 3},
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode([{two, 1}, {three, 2}], [{pre_encode, fun({K, V}) -> {K, V + 1}; (V) -> V end}])
|
|
||||||
)},
|
|
||||||
{"pre_encode one field record", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_object,
|
|
||||||
{key, <<"bar">>}, {literal, false},
|
|
||||||
end_object,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode([{foo, bar}], [{pre_encode, fun({foo, V}) -> {V, undefined}; (undefined) -> false; (V) -> V end}])
|
|
||||||
)},
|
|
||||||
{"pre_encode list", ?_assertEqual(
|
|
||||||
[
|
|
||||||
start_array,
|
|
||||||
{integer, 2}, {integer, 3}, {integer, 4},
|
|
||||||
end_array,
|
|
||||||
end_json
|
|
||||||
],
|
|
||||||
encode([1,2,3], [{pre_encode, fun(X) when is_integer(X) -> X + 1; (V) -> V end}])
|
|
||||||
)}
|
|
||||||
].
|
|
||||||
|
|
||||||
error_test_() ->
|
error_test_() ->
|
||||||
[
|
[
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue