diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index 5bd862e..6b271ca 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -25,6 +25,7 @@ -export([to_json/2, format/2]). -export([init/1, handle_event/2]). +-export([start_json/0, start_json/1]). -export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]). @@ -150,6 +151,10 @@ indent_or_space(Config) -> %% `{array, Array}` %% `Object` and `Array` are utf8 encoded binaries +start_json() -> {[], #config{}}. + +start_json(Config) when is_list(Config) -> {[], parse_config(Config)}. + %% allocate a new object on top of the stack start_object({Stack, Config}) -> {[{object, ?start_object}] ++ Stack, Config}. @@ -234,8 +239,8 @@ insert(Key, Value, {[{object, Object}|Rest], Config}) when is_binary(Key), is_bi insert(_, _, _) -> erlang:error(badarg). -get_key({[{object, Key, _}|_], _}) -> {ok, Key}; -get_key(_) -> {error, nokey}. +get_key({[{object, Key, _}|_], _}) -> Key; +get_key(_) -> erlang:error(badarg). @@ -360,6 +365,14 @@ format_test_() -> rep_manipulation_test_() -> [ + {"allocate a new context", ?_assertEqual( + {[], #config{}}, + start_json() + )}, + {"allocate a new context with config", ?_assertEqual( + {[], #config{space=1, indent=2}}, + start_json([{space, 1}, {indent, 2}]) + )}, {"allocate a new object on an empty stack", ?_assertEqual( {[{object, <<"{">>}], #config{}}, start_object({[], #config{}}) @@ -381,15 +394,15 @@ rep_manipulation_test_() -> insert(<<"\"key\"">>, {[{object, <<"{">>}], #config{}}) )}, {"get current key", ?_assertEqual( - {ok, key}, + key, get_key({[{object, key, <<"{">>}], #config{}}) )}, - {"try to get non-key from object", ?_assertEqual( - {error, nokey}, + {"try to get non-key from object", ?_assertError( + badarg, get_key({[{object, <<"{">>}], #config{}}) )}, - {"try to get key from array", ?_assertEqual( - {error, nokey}, + {"try to get key from array", ?_assertError( + badarg, get_key({[{array, <<"[">>}], #config{}}) )}, {"insert a value into an object", ?_assertEqual( diff --git a/src/jsx_to_term.erl b/src/jsx_to_term.erl index fd90d10..9e3589c 100644 --- a/src/jsx_to_term.erl +++ b/src/jsx_to_term.erl @@ -25,6 +25,7 @@ -export([to_term/2]). -export([init/1, handle_event/2]). +-export([start_term/0, start_term/1]). -export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]). @@ -117,6 +118,10 @@ format_key(Key, Config) -> %% an array looks like %% `{array, [NthValue, NthMinus1Value,...FirstValue]}` +start_term() -> {[], #config{}}. + +start_term(Config) when is_list(Config) -> {[], parse_config(Config)}. + %% allocate a new object on top of the stack start_object({Stack, Config}) -> {[{object, []}] ++ Stack, Config}. @@ -149,8 +154,8 @@ insert(Key, Value, {[{object, Pairs}|Rest], Config}) -> insert(_, _, _) -> erlang:error(badarg). -get_key({[{object, Key, _}|_], _}) -> {ok, Key}; -get_key(_) -> {error, nokey}. +get_key({[{object, Key, _}|_], _}) -> Key; +get_key(_) -> erlang:error(badarg). @@ -200,6 +205,14 @@ format_key_test_() -> rep_manipulation_test_() -> [ + {"allocate a new context", ?_assertEqual( + {[], #config{}}, + start_term() + )}, + {"allocate a new context with option", ?_assertEqual( + {[], #config{labels=atom}}, + start_term([{labels, atom}]) + )}, {"allocate a new object on an empty stack", ?_assertEqual( {[{object, []}], #config{}}, start_object({[], #config{}}) @@ -221,15 +234,15 @@ rep_manipulation_test_() -> insert(key, {[{object, []}, junk], #config{}}) )}, {"get current key", ?_assertEqual( - {ok, key}, + key, get_key({[{object, key, []}], #config{}}) )}, - {"try to get non-key from object", ?_assertEqual( - {error, nokey}, + {"try to get non-key from object", ?_assertError( + badarg, get_key({[{object, []}], #config{}}) )}, - {"try to get key from array", ?_assertEqual( - {error, nokey}, + {"try to get key from array", ?_assertError( + badarg, get_key({[{array, []}], #config{}}) )}, {"insert a value into an object", ?_assertEqual(