Skip to content

Commit

Permalink
add {pre_encoder, F} variant of {pre_encoders, [F, G,...]}
Browse files Browse the repository at this point in the history
  • Loading branch information
alisdair sullivan committed Apr 5, 2012
1 parent 89292c9 commit ae13b93
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/jsx_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ pre_encoders_test_() ->
]
)},
{"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,
{key, <<"object">>}, start_object,
Expand All @@ -825,15 +825,15 @@ pre_encoders_test_() ->
]
)},
{"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,
end_object,
end_json
]
)},
{"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,
{key, <<"object">>}, start_object,
Expand All @@ -852,7 +852,7 @@ pre_encoders_test_() ->
]
)},
{"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,
{key, <<"object">>}, start_object,
Expand Down
4 changes: 4 additions & 0 deletions src/jsx_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ parse_opts([relax|Rest], Opts) ->
comments = 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) ->
lists:foreach(fun(F) when is_function(F, 1) -> ok end, Encoders),
AllEncoders = Opts#opts.pre_encoders ++ Encoders,
Expand Down Expand Up @@ -94,6 +97,7 @@ valid_flags() ->
ignored_bad_escapes,
explicit_end,
relax,
pre_encoder,
pre_encoders,
%% deprecated flags
loose_unicode, %% replaced_bad_utf8
Expand Down

0 comments on commit ae13b93

Please sign in to comment.