allow atoms in erlang terms to be converted to strings in json objects

and arrays. `true`, `false` and `null` excluded
This commit is contained in:
alisdair sullivan 2013-12-18 02:13:50 +00:00
parent 3bb65e9bab
commit 95b2d9b628
4 changed files with 42 additions and 3 deletions

View file

@ -35,7 +35,7 @@
-ifdef(TEST).
%% data and helper functions for tests
-export([test_cases/0]).
-export([test_cases/0, special_test_cases/0]).
-export([init/1, handle_event/2]).
-endif.
@ -174,9 +174,13 @@ test_cases() ->
++ floats()
++ compound_object().
%% segregate these so we can skip them in `jsx_to_term`
special_test_cases() -> special_objects() ++ special_array().
empty_array() -> [{"[]", <<"[]">>, [], [start_array, end_array]}].
nested_array() ->
[{
"[[[]]]",
@ -188,6 +192,7 @@ nested_array() ->
empty_object() -> [{"{}", <<"{}">>, [{}], [start_object, end_object]}].
nested_object() ->
[{
"{\"key\":{\"key\":{}}}",
@ -221,6 +226,7 @@ naked_strings() ->
|| String <- Raw
].
strings() ->
naked_strings()
++ [ wrap_with_array(Test) || Test <- naked_strings() ]
@ -246,6 +252,7 @@ naked_integers() ->
|| X <- Raw ++ [ -1 * Y || Y <- Raw ] ++ [0]
].
integers() ->
naked_integers()
++ [ wrap_with_array(Test) || Test <- naked_integers() ]
@ -276,6 +283,7 @@ naked_floats() ->
|| X <- Raw ++ [ -1 * Y || Y <- Raw ]
].
floats() ->
naked_floats()
++ [ wrap_with_array(Test) || Test <- naked_floats() ]
@ -293,6 +301,7 @@ naked_literals() ->
|| Literal <- [true, false, null]
].
literals() ->
naked_literals()
++ [ wrap_with_array(Test) || Test <- naked_literals() ]
@ -337,6 +346,34 @@ compound_object() ->
}].
special_objects() ->
[
{
"[{key, atom}]",
<<"{\"key\":\"atom\"}">>,
[{key, atom}],
[start_object, {key, <<"key">>}, {string, <<"atom">>}, end_object]
},
{
"[{1, true}]",
<<"{\"1\":true}">>,
[{1, true}],
[start_object, {key, <<"1">>}, {literal, true}, end_object]
}
].
special_array() ->
[
{
"[foo, bar]",
<<"[\"foo\",\"bar\"]">>,
[foo, bar],
[start_array, {string, <<"foo">>}, {string, <<"bar">>}, end_array]
}
].
wrap_with_array({Title, JSON, Term, Events}) ->
{
"[" ++ Title ++ "]",

View file

@ -115,6 +115,8 @@ value([{string, String}|Tokens], Handler, Stack, Config) when is_binary(String)
end;
value([String|Tokens], Handler, Stack, Config) when is_binary(String) ->
value([{string, String}] ++ Tokens, Handler, Stack, Config);
value([String|Tokens], Handler, Stack, Config) when is_atom(String) ->
value([{string, atom_to_binary(String, utf8)}] ++ Tokens, Handler, Stack, Config);
value([{raw, Raw}|Tokens], Handler, Stack, Config) when is_binary(Raw) ->
value((jsx:decoder(?MODULE, [], []))(Raw) ++ Tokens, Handler, Stack, Config);
value([], Handler, Stack, Config) ->

View file

@ -431,7 +431,7 @@ rep_manipulation_test_() ->
handle_event_test_() ->
Data = jsx:test_cases(),
Data = jsx:test_cases() ++ jsx:special_test_cases(),
[
{
Title, ?_assertEqual(

View file

@ -159,7 +159,7 @@ repeated_keys_test_() ->
handle_event_test_() ->
Data = jsx:test_cases(),
Data = jsx:test_cases() ++ jsx:special_test_cases(),
[
{
Title, ?_assertEqual(