introduce Config' object to internal rep in jsx_to_json' to match

`jsx_to_term'
This commit is contained in:
alisdair sullivan 2013-10-30 00:51:44 +00:00
parent 5753baefaf
commit 3f7f0f1f9c

View file

@ -76,15 +76,15 @@ init(Config) -> {[], parse_config(Config)}.
handle_event(end_json, {Term, _Config}) -> Term; handle_event(end_json, {Term, _Config}) -> Term;
handle_event(start_object, {Stack, Config}) -> {start_object(Stack), Config}; handle_event(start_object, State) -> start_object(State);
handle_event(end_object, {Stack, Config}) -> {finish(Stack), Config}; handle_event(end_object, State) -> finish(State);
handle_event(start_array, {Stack, Config}) -> {start_array(Stack), Config}; handle_event(start_array, State) -> start_array(State);
handle_event(end_array, {Stack, Config}) -> {finish(Stack), Config}; handle_event(end_array, State) -> finish(State);
handle_event({key, Key}, {Stack, Config}) -> {insert(format_key(Key, Config), Stack), Config}; handle_event({key, Key}, {_, Config} = State) -> insert(format_key(Key, Config), State);
handle_event({_, Event}, {Stack, Config}) -> {insert(Event, Stack), Config}. handle_event({_, Event}, State) -> insert(Event, State).
format_key(Key, Config) -> format_key(Key, Config) ->
@ -111,30 +111,34 @@ format_key(Key, Config) ->
%% `{array, [NthValue, NthMinus1Value,...FirstValue]}` %% `{array, [NthValue, NthMinus1Value,...FirstValue]}`
%% allocate a new object on top of the stack %% allocate a new object on top of the stack
start_object(Stack) -> [{object, []}] ++ Stack. start_object({Stack, Config}) -> {[{object, []}] ++ Stack, Config}.
%% allocate a new array on top of the stack %% allocate a new array on top of the stack
start_array(Stack) -> [{array, []}] ++ Stack. start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
%% finish an object or array and insert it into the parent object if it exists %% finish an object or array and insert it into the parent object if it exists
finish([{object, []}]) -> [{}]; finish({[{object, []}], Config}) -> {[{}], Config};
finish([{object, []}|Rest]) -> insert([{}], Rest); finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
finish([{object, Pairs}]) -> lists:reverse(Pairs); finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
finish([{object, Pairs}|Rest]) -> insert(lists:reverse(Pairs), Rest); finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
finish([{array, Values}]) -> lists:reverse(Values); finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
finish([{array, Values}|Rest]) -> insert(lists:reverse(Values), Rest); finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
finish(_) -> erlang:error(badarg). finish(_) -> erlang:error(badarg).
%% insert a value when there's no parent object or array %% insert a value when there's no parent object or array
insert(Value, []) -> Value; insert(Value, {[], Config}) -> {Value, Config};
%% insert a key or value into an object or array, autodetects the 'right' thing %% insert a key or value into an object or array, autodetects the 'right' thing
insert(Key, [{object, Pairs}|Rest]) -> [{object, Key, Pairs}] ++ Rest; insert(Key, {[{object, Pairs}|Rest], Config}) ->
insert(Value, [{object, Key, Pairs}|Rest]) -> [{object, [{Key, Value}] ++ Pairs}] ++ Rest; {[{object, Key, Pairs}] ++ Rest, Config};
insert(Value, [{array, Values}|Rest]) -> [{array, [Value] ++ Values}] ++ Rest; insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
insert(Value, {[{array, Values}|Rest], Config}) ->
{[{array, [Value] ++ Values}] ++ Rest, Config};
insert(_, _) -> erlang:error(badarg). insert(_, _) -> erlang:error(badarg).
%% insert a key/value pair into an object %% insert a key/value pair into an object
insert(Key, Value, [{object, Pairs}|Rest]) -> [{object, [{Key, Value}] ++ Pairs}] ++ Rest; insert(Key, Value, {[{object, Pairs}|Rest], Config}) ->
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
insert(_, _, _) -> erlang:error(badarg). insert(_, _, _) -> erlang:error(badarg).
@ -187,56 +191,56 @@ format_key_test_() ->
rep_manipulation_test_() -> rep_manipulation_test_() ->
[ [
{"allocate a new object on an empty stack", ?_assertEqual( {"allocate a new object on an empty stack", ?_assertEqual(
[{object, []}], {[{object, []}], #config{}},
start_object([]) start_object({[], #config{}})
)}, )},
{"allocate a new object on a stack", ?_assertEqual( {"allocate a new object on a stack", ?_assertEqual(
[{object, []}, {object, []}], {[{object, []}, {object, []}], #config{}},
start_object([{object, []}]) start_object({[{object, []}], #config{}})
)}, )},
{"allocate a new array on an empty stack", ?_assertEqual( {"allocate a new array on an empty stack", ?_assertEqual(
[{array, []}], {[{array, []}], #config{}},
start_array([]) start_array({[], #config{}})
)}, )},
{"allocate a new array on a stack", ?_assertEqual( {"allocate a new array on a stack", ?_assertEqual(
[{array, []}, {object, []}], {[{array, []}, {object, []}], #config{}},
start_array([{object, []}]) start_array({[{object, []}], #config{}})
)}, )},
{"insert a key into an object", ?_assertEqual( {"insert a key into an object", ?_assertEqual(
[{object, key, []}, junk], {[{object, key, []}, junk], #config{}},
insert(key, [{object, []}, junk]) insert(key, {[{object, []}, junk], #config{}})
)}, )},
{"insert a value into an object", ?_assertEqual( {"insert a value into an object", ?_assertEqual(
[{object, [{key, value}]}, junk], {[{object, [{key, value}]}, junk], #config{}},
insert(value, [{object, key, []}, junk]) insert(value, {[{object, key, []}, junk], #config{}})
)}, )},
{"insert a value into an array", ?_assertEqual( {"insert a value into an array", ?_assertEqual(
[{array, [value]}, junk], {[{array, [value]}, junk], #config{}},
insert(value, [{array, []}, junk]) insert(value, {[{array, []}, junk], #config{}})
)}, )},
{"insert a key/value pair into an object", ?_assertEqual( {"insert a key/value pair into an object", ?_assertEqual(
[{object, [{key, value}, {x, y}]}, junk], {[{object, [{key, value}, {x, y}]}, junk], #config{}},
insert(key, value, [{object, [{x, y}]}, junk]) insert(key, value, {[{object, [{x, y}]}, junk], #config{}})
)}, )},
{"finish an object with no ancestor", ?_assertEqual( {"finish an object with no ancestor", ?_assertEqual(
[{a, b}, {x, y}], {[{a, b}, {x, y}], #config{}},
finish([{object, [{x, y}, {a, b}]}]) finish({[{object, [{x, y}, {a, b}]}], #config{}})
)}, )},
{"finish an empty object", ?_assertEqual( {"finish an empty object", ?_assertEqual(
[{}], {[{}], #config{}},
finish([{object, []}]) finish({[{object, []}], #config{}})
)}, )},
{"finish an object with an ancestor", ?_assertEqual( {"finish an object with an ancestor", ?_assertEqual(
[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], {[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], #config{}},
finish([{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}]) finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}})
)}, )},
{"finish an array with no ancestor", ?_assertEqual( {"finish an array with no ancestor", ?_assertEqual(
[a, b, c], {[a, b, c], #config{}},
finish([{array, [c, b, a]}]) finish({[{array, [c, b, a]}], #config{}})
)}, )},
{"finish an array with an ancestor", ?_assertEqual( {"finish an array with an ancestor", ?_assertEqual(
[{array, [[a, b, c], d, e, f]}], {[{array, [[a, b, c], d, e, f]}], #config{}},
finish([{array, [c, b, a]}, {array, [d, e, f]}]) finish({[{array, [c, b, a]}, {array, [d, e, f]}], #config{}})
)} )}
]. ].