From 95b2d9b628f6083b95b5c79a1d676576c3b13834 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 18 Dec 2013 02:13:50 +0000 Subject: [PATCH] allow atoms in erlang terms to be converted to strings in json objects and arrays. `true`, `false` and `null` excluded --- src/jsx.erl | 39 ++++++++++++++++++++++++++++++++++++++- src/jsx_parser.erl | 2 ++ src/jsx_to_json.erl | 2 +- src/jsx_verify.erl | 2 +- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/jsx.erl b/src/jsx.erl index 57a1d45..3dac4f8 100644 --- a/src/jsx.erl +++ b/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 ++ "]", diff --git a/src/jsx_parser.erl b/src/jsx_parser.erl index 769ffd7..c5c23a2 100644 --- a/src/jsx_parser.erl +++ b/src/jsx_parser.erl @@ -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) -> diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index 6f4114e..5bd862e 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -431,7 +431,7 @@ rep_manipulation_test_() -> handle_event_test_() -> - Data = jsx:test_cases(), + Data = jsx:test_cases() ++ jsx:special_test_cases(), [ { Title, ?_assertEqual( diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index 34c50e5..e923691 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -159,7 +159,7 @@ repeated_keys_test_() -> handle_event_test_() -> - Data = jsx:test_cases(), + Data = jsx:test_cases() ++ jsx:special_test_cases(), [ { Title, ?_assertEqual(