add {pre_encoder, F} variant of {pre_encoders, [F, G,...]}

This commit is contained in:
alisdair sullivan 2012-04-04 20:13:27 -07:00
parent 89292c940d
commit ae13b934c6
2 changed files with 8 additions and 4 deletions

View file

@ -812,7 +812,7 @@ pre_encoders_test_() ->
] ]
)}, )},
{"replace lists with empty lists", ?_assertEqual( {"replace lists with empty lists", ?_assertEqual(
encode(Term, [{pre_encoders, [fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end]}]), encode(Term, [{pre_encoder, fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end}]),
[ [
start_object, start_object,
{key, <<"object">>}, start_object, {key, <<"object">>}, start_object,
@ -825,7 +825,7 @@ pre_encoders_test_() ->
] ]
)}, )},
{"replace objects with empty objects", ?_assertEqual( {"replace objects with empty objects", ?_assertEqual(
encode(Term, [{pre_encoders, [fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end]}]), encode(Term, [{pre_encoder, fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end}]),
[ [
start_object, start_object,
end_object, end_object,
@ -833,7 +833,7 @@ pre_encoders_test_() ->
] ]
)}, )},
{"replace all non-list values with false", ?_assertEqual( {"replace all non-list values with false", ?_assertEqual(
encode(Term, [{pre_encoders, [fun(V) when is_list(V) -> V; (_) -> false end]}]), encode(Term, [{pre_encoder, fun(V) when is_list(V) -> V; (_) -> false end}]),
[ [
start_object, start_object,
{key, <<"object">>}, start_object, {key, <<"object">>}, start_object,
@ -852,7 +852,7 @@ pre_encoders_test_() ->
] ]
)}, )},
{"replace all atoms with atom_to_list", ?_assertEqual( {"replace all atoms with atom_to_list", ?_assertEqual(
encode(Term, [{pre_encoders, [fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end]}]), encode(Term, [{pre_encoder, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}]),
[ [
start_object, start_object,
{key, <<"object">>}, start_object, {key, <<"object">>}, start_object,

View file

@ -61,6 +61,9 @@ parse_opts([relax|Rest], Opts) ->
comments = true, comments = true,
ignored_bad_escapes = true ignored_bad_escapes = true
}); });
parse_opts([{pre_encoder, Encoder}|Rest], Opts) when is_function(Encoder, 1) ->
AllEncoders = Opts#opts.pre_encoders ++ [Encoder],
parse_opts(Rest, Opts#opts{pre_encoders=AllEncoders});
parse_opts([{pre_encoders, Encoders}|Rest], Opts) when is_list(Encoders) -> parse_opts([{pre_encoders, Encoders}|Rest], Opts) when is_list(Encoders) ->
lists:foreach(fun(F) when is_function(F, 1) -> ok end, Encoders), lists:foreach(fun(F) when is_function(F, 1) -> ok end, Encoders),
AllEncoders = Opts#opts.pre_encoders ++ Encoders, AllEncoders = Opts#opts.pre_encoders ++ Encoders,
@ -94,6 +97,7 @@ valid_flags() ->
ignored_bad_escapes, ignored_bad_escapes,
explicit_end, explicit_end,
relax, relax,
pre_encoder,
pre_encoders, pre_encoders,
%% deprecated flags %% deprecated flags
loose_unicode, %% replaced_bad_utf8 loose_unicode, %% replaced_bad_utf8