post_decode working
This commit is contained in:
parent
d9bb9ee9ac
commit
79b8740da8
1 changed files with 41 additions and 0 deletions
|
@ -186,4 +186,45 @@ naked_test_() ->
|
||||||
{"naked string", ?_assertEqual(to_term(<<"\"string\"">>, []), <<"string">>)}
|
{"naked string", ?_assertEqual(to_term(<<"\"string\"">>, []), <<"string">>)}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
post_decoders_test_() ->
|
||||||
|
JSON = <<"{\"object\": {
|
||||||
|
\"literals\": [true, false, null],
|
||||||
|
\"strings\": [\"foo\", \"bar\", \"baz\"],
|
||||||
|
\"numbers\": [1, 1.0, 1e0]
|
||||||
|
}}">>,
|
||||||
|
[
|
||||||
|
{"no post_decode", ?_assertEqual(
|
||||||
|
to_term(JSON, []),
|
||||||
|
[{<<"object">>, [
|
||||||
|
{<<"literals">>, [true, false, null]},
|
||||||
|
{<<"strings">>, [<<"foo">>, <<"bar">>, <<"baz">>]},
|
||||||
|
{<<"numbers">>, [1, 1.0, 1.0]}
|
||||||
|
]}]
|
||||||
|
)},
|
||||||
|
{"replace arrays with empty arrays", ?_assertEqual(
|
||||||
|
to_term(JSON, [{post_decode, fun([T|_] = V) when is_tuple(T) -> V; (V) when is_list(V) -> []; (V) -> V end}]),
|
||||||
|
[{<<"object">>, [{<<"literals">>, []}, {<<"strings">>, []}, {<<"numbers">>, []}]}]
|
||||||
|
)},
|
||||||
|
{"replace objects with empty objects", ?_assertEqual(
|
||||||
|
to_term(JSON, [{post_decode, fun(V) when is_list(V) -> [{}]; (V) -> V end}]),
|
||||||
|
[{}]
|
||||||
|
)},
|
||||||
|
{"replace all non-list values with false", ?_assertEqual(
|
||||||
|
to_term(JSON, [{post_decode, fun(V) when is_list(V) -> V; (_) -> false end}]),
|
||||||
|
[{<<"object">>, [
|
||||||
|
{<<"literals">>, [false, false, false]},
|
||||||
|
{<<"strings">>, [false, false, false]},
|
||||||
|
{<<"numbers">>, [false, false, false]}
|
||||||
|
]}]
|
||||||
|
)},
|
||||||
|
{"atoms_to_strings", ?_assertEqual(
|
||||||
|
to_term(JSON, [{post_decode, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}]),
|
||||||
|
[{<<"object">>, [
|
||||||
|
{<<"literals">>, [<<"true">>, <<"false">>, <<"null">>]},
|
||||||
|
{<<"strings">>, [<<"foo">>, <<"bar">>, <<"baz">>]},
|
||||||
|
{<<"numbers">>, [1, 1.0, 1.0]}
|
||||||
|
]}]
|
||||||
|
)}
|
||||||
|
].
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue