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:
parent
3bb65e9bab
commit
95b2d9b628
4 changed files with 42 additions and 3 deletions
39
src/jsx.erl
39
src/jsx.erl
|
@ -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 ++ "]",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue