2011-11-24 06:33:15 -08:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
|
|
|
|
2011-11-28 17:30:55 -08:00
|
|
|
-module(jsx_to_term).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
-export([to_term/2]).
|
|
|
|
-export([init/1, handle_event/2]).
|
2014-01-12 20:36:19 +00:00
|
|
|
-export([start_term/0, start_term/1]).
|
2013-10-31 02:47:45 +00:00
|
|
|
-export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-record(config, {
|
2013-03-11 01:01:49 -07:00
|
|
|
labels = binary
|
2011-11-24 06:33:15 -08:00
|
|
|
}).
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-type config() :: list().
|
2013-09-19 17:04:38 +02:00
|
|
|
-export_type([config/0]).
|
|
|
|
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2012-03-25 13:18:26 -07:00
|
|
|
-type json_value() :: list({binary(), json_value()})
|
|
|
|
| list(json_value())
|
2012-03-25 13:09:55 -07:00
|
|
|
| true
|
|
|
|
| false
|
|
|
|
| null
|
|
|
|
| integer()
|
|
|
|
| float()
|
|
|
|
| binary().
|
2012-03-25 13:18:26 -07:00
|
|
|
|
2012-03-27 16:46:43 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-spec to_term(Source::binary(), Config::config()) -> json_value().
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
to_term(Source, Config) when is_list(Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
(jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config(Config) -> parse_config(Config, #config{}).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{labels, Val}|Rest], Config)
|
2013-07-05 03:11:01 +00:00
|
|
|
when Val == binary; Val == atom; Val == existing_atom; Val == attempt_atom ->
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config(Rest, Config#config{labels = Val});
|
|
|
|
parse_config([labels|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{labels = binary});
|
|
|
|
parse_config([{K, _}|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> parse_config(Rest, Config)
|
|
|
|
; false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([K|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> parse_config(Rest, Config)
|
|
|
|
; false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([], Config) ->
|
|
|
|
Config.
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2013-09-19 17:04:38 +02:00
|
|
|
-type state() :: {[any()], #config{}}.
|
|
|
|
-spec init(Config::proplists:proplist()) -> state().
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2013-10-24 05:01:20 +00:00
|
|
|
init(Config) -> {[], parse_config(Config)}.
|
2011-11-29 19:39:01 -08:00
|
|
|
|
2013-09-19 17:04:38 +02:00
|
|
|
-spec handle_event(Event::any(), State::state()) -> state().
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2013-10-24 05:01:20 +00:00
|
|
|
handle_event(end_json, {Term, _Config}) -> Term;
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2013-10-30 00:51:44 +00:00
|
|
|
handle_event(start_object, State) -> start_object(State);
|
|
|
|
handle_event(end_object, State) -> finish(State);
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-10-30 00:51:44 +00:00
|
|
|
handle_event(start_array, State) -> start_array(State);
|
|
|
|
handle_event(end_array, State) -> finish(State);
|
2011-11-28 15:15:11 -08:00
|
|
|
|
2013-10-30 00:51:44 +00:00
|
|
|
handle_event({key, Key}, {_, Config} = State) -> insert(format_key(Key, Config), State);
|
2011-11-28 15:15:11 -08:00
|
|
|
|
2013-10-30 00:51:44 +00:00
|
|
|
handle_event({_, Event}, State) -> insert(Event, State).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
format_key(Key, Config) ->
|
|
|
|
case Config#config.labels of
|
2011-11-28 15:24:44 -08:00
|
|
|
binary -> Key
|
|
|
|
; atom -> binary_to_atom(Key, utf8)
|
|
|
|
; existing_atom -> binary_to_existing_atom(Key, utf8)
|
2013-07-05 03:11:01 +00:00
|
|
|
; attempt_atom ->
|
2013-07-03 21:51:26 -04:00
|
|
|
try binary_to_existing_atom(Key, utf8) of
|
|
|
|
Result -> Result
|
|
|
|
catch
|
|
|
|
error:badarg -> Key
|
|
|
|
end
|
2011-11-28 15:24:44 -08:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2013-10-30 00:57:50 +00:00
|
|
|
%% internal state is a stack and a config object
|
|
|
|
%% `{Stack, Config}`
|
|
|
|
%% the stack is a list of in progress objects/arrays
|
2013-10-24 05:01:20 +00:00
|
|
|
%% `[Current, Parent, Grandparent,...OriginalAncestor]`
|
|
|
|
%% an object has the representation on the stack of
|
|
|
|
%% `{object, [{NthKey, NthValue}, {NMinus1Key, NthMinus1Value},...{FirstKey, FirstValue}]}`
|
|
|
|
%% of if there's a key with a yet to be matched value
|
|
|
|
%% `{object, Key, [{NthKey, NthValue},...]}`
|
|
|
|
%% an array looks like
|
|
|
|
%% `{array, [NthValue, NthMinus1Value,...FirstValue]}`
|
|
|
|
|
2014-01-12 20:36:19 +00:00
|
|
|
start_term() -> {[], #config{}}.
|
|
|
|
|
|
|
|
start_term(Config) when is_list(Config) -> {[], parse_config(Config)}.
|
|
|
|
|
2013-10-24 05:01:20 +00:00
|
|
|
%% allocate a new object on top of the stack
|
2013-10-30 00:51:44 +00:00
|
|
|
start_object({Stack, Config}) -> {[{object, []}] ++ Stack, Config}.
|
2013-10-24 05:01:20 +00:00
|
|
|
|
|
|
|
%% allocate a new array on top of the stack
|
2013-10-30 00:51:44 +00:00
|
|
|
start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
|
2013-10-24 05:01:20 +00:00
|
|
|
|
|
|
|
%% finish an object or array and insert it into the parent object if it exists
|
2013-10-30 00:51:44 +00:00
|
|
|
finish({[{object, []}], Config}) -> {[{}], Config};
|
|
|
|
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
|
|
|
|
finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
|
|
|
|
finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
|
|
|
|
finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
|
|
|
|
finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
|
2013-10-24 05:01:20 +00:00
|
|
|
finish(_) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
%% insert a value when there's no parent object or array
|
2013-10-30 00:51:44 +00:00
|
|
|
insert(Value, {[], Config}) -> {Value, Config};
|
2013-10-24 05:01:20 +00:00
|
|
|
%% insert a key or value into an object or array, autodetects the 'right' thing
|
2013-10-30 00:51:44 +00:00
|
|
|
insert(Key, {[{object, Pairs}|Rest], Config}) ->
|
|
|
|
{[{object, Key, Pairs}] ++ Rest, Config};
|
|
|
|
insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
|
|
|
|
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
|
|
|
|
insert(Value, {[{array, Values}|Rest], Config}) ->
|
|
|
|
{[{array, [Value] ++ Values}] ++ Rest, Config};
|
2013-10-24 05:01:20 +00:00
|
|
|
insert(_, _) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
%% insert a key/value pair into an object
|
2013-10-30 00:51:44 +00:00
|
|
|
insert(Key, Value, {[{object, Pairs}|Rest], Config}) ->
|
|
|
|
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
|
2013-10-24 05:01:20 +00:00
|
|
|
insert(_, _, _) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
|
2014-01-12 20:36:19 +00:00
|
|
|
get_key({[{object, Key, _}|_], _}) -> Key;
|
|
|
|
get_key(_) -> erlang:error(badarg).
|
2013-10-31 02:47:45 +00:00
|
|
|
|
2013-10-24 05:01:20 +00:00
|
|
|
|
2011-11-28 15:24:44 -08:00
|
|
|
|
2011-11-24 06:33:15 -08:00
|
|
|
%% eunit tests
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2013-02-04 20:55:40 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
config_test_() ->
|
2011-11-24 06:33:15 -08:00
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"empty config", ?_assertEqual(#config{}, parse_config([]))},
|
|
|
|
{"implicit binary labels", ?_assertEqual(#config{}, parse_config([labels]))},
|
|
|
|
{"binary labels", ?_assertEqual(#config{}, parse_config([{labels, binary}]))},
|
|
|
|
{"atom labels", ?_assertEqual(#config{labels=atom}, parse_config([{labels, atom}]))},
|
2013-02-04 20:55:40 -08:00
|
|
|
{"existing atom labels", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{labels=existing_atom},
|
|
|
|
parse_config([{labels, existing_atom}])
|
2012-03-16 15:34:57 -07:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
|
|
|
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
|
2011-11-28 15:15:11 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-04 20:55:40 -08:00
|
|
|
format_key_test_() ->
|
2011-11-28 15:15:11 -08:00
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"binary key", ?_assertEqual(<<"key">>, format_key(<<"key">>, #config{labels=binary}))},
|
|
|
|
{"atom key", ?_assertEqual(key, format_key(<<"key">>, #config{labels=atom}))},
|
2013-02-04 20:55:40 -08:00
|
|
|
{"existing atom key", ?_assertEqual(
|
|
|
|
key,
|
2013-02-12 11:54:42 -08:00
|
|
|
format_key(<<"key">>, #config{labels=existing_atom})
|
2013-02-04 20:55:40 -08:00
|
|
|
)},
|
|
|
|
{"nonexisting atom key", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
format_key(<<"nonexistentatom">>, #config{labels=existing_atom})
|
2013-07-03 21:51:26 -04:00
|
|
|
)},
|
|
|
|
{"sloppy existing atom key", ?_assertEqual(
|
|
|
|
key,
|
2013-07-05 03:11:01 +00:00
|
|
|
format_key(<<"key">>, #config{labels=attempt_atom})
|
2013-07-03 21:51:26 -04:00
|
|
|
)},
|
|
|
|
{"nonexisting atom key", ?_assertEqual(
|
|
|
|
<<"nonexistentatom">>,
|
2013-07-05 03:11:01 +00:00
|
|
|
format_key(<<"nonexistentatom">>, #config{labels=attempt_atom})
|
2013-02-04 20:55:40 -08:00
|
|
|
)}
|
2011-11-24 06:33:15 -08:00
|
|
|
].
|
2011-11-28 15:24:44 -08:00
|
|
|
|
|
|
|
|
2013-10-24 05:01:20 +00:00
|
|
|
rep_manipulation_test_() ->
|
|
|
|
[
|
2014-01-12 20:36:19 +00:00
|
|
|
{"allocate a new context", ?_assertEqual(
|
|
|
|
{[], #config{}},
|
|
|
|
start_term()
|
|
|
|
)},
|
|
|
|
{"allocate a new context with option", ?_assertEqual(
|
|
|
|
{[], #config{labels=atom}},
|
|
|
|
start_term([{labels, atom}])
|
|
|
|
)},
|
2013-10-24 05:01:20 +00:00
|
|
|
{"allocate a new object on an empty stack", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, []}], #config{}},
|
|
|
|
start_object({[], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"allocate a new object on a stack", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, []}, {object, []}], #config{}},
|
|
|
|
start_object({[{object, []}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"allocate a new array on an empty stack", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{array, []}], #config{}},
|
|
|
|
start_array({[], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"allocate a new array on a stack", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{array, []}, {object, []}], #config{}},
|
|
|
|
start_array({[{object, []}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"insert a key into an object", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, key, []}, junk], #config{}},
|
|
|
|
insert(key, {[{object, []}, junk], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
2013-10-31 02:47:45 +00:00
|
|
|
{"get current key", ?_assertEqual(
|
2014-01-12 20:36:19 +00:00
|
|
|
key,
|
2013-10-31 02:47:45 +00:00
|
|
|
get_key({[{object, key, []}], #config{}})
|
|
|
|
)},
|
2014-01-12 20:36:19 +00:00
|
|
|
{"try to get non-key from object", ?_assertError(
|
|
|
|
badarg,
|
2013-10-31 02:47:45 +00:00
|
|
|
get_key({[{object, []}], #config{}})
|
|
|
|
)},
|
2014-01-12 20:36:19 +00:00
|
|
|
{"try to get key from array", ?_assertError(
|
|
|
|
badarg,
|
2013-10-31 02:47:45 +00:00
|
|
|
get_key({[{array, []}], #config{}})
|
|
|
|
)},
|
2013-10-24 05:01:20 +00:00
|
|
|
{"insert a value into an object", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, [{key, value}]}, junk], #config{}},
|
|
|
|
insert(value, {[{object, key, []}, junk], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"insert a value into an array", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{array, [value]}, junk], #config{}},
|
|
|
|
insert(value, {[{array, []}, junk], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"insert a key/value pair into an object", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, [{key, value}, {x, y}]}, junk], #config{}},
|
|
|
|
insert(key, value, {[{object, [{x, y}]}, junk], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"finish an object with no ancestor", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{a, b}, {x, y}], #config{}},
|
|
|
|
finish({[{object, [{x, y}, {a, b}]}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"finish an empty object", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{}], #config{}},
|
|
|
|
finish({[{object, []}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"finish an object with an ancestor", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], #config{}},
|
|
|
|
finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"finish an array with no ancestor", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[a, b, c], #config{}},
|
|
|
|
finish({[{array, [c, b, a]}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)},
|
|
|
|
{"finish an array with an ancestor", ?_assertEqual(
|
2013-10-30 00:51:44 +00:00
|
|
|
{[{array, [[a, b, c], d, e, f]}], #config{}},
|
|
|
|
finish({[{array, [c, b, a]}, {array, [d, e, f]}], #config{}})
|
2013-10-24 05:01:20 +00:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-10 19:39:42 -08:00
|
|
|
handle_event_test_() ->
|
2013-02-13 19:13:50 -08:00
|
|
|
Data = jsx:test_cases(),
|
2013-02-10 19:39:42 -08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
Title, ?_assertEqual(
|
|
|
|
Term,
|
2013-10-25 01:35:39 +00:00
|
|
|
lists:foldl(fun handle_event/2, init([]), Events ++ [end_json])
|
2013-02-10 19:39:42 -08:00
|
|
|
)
|
|
|
|
} || {Title, _, Term, Events} <- Data
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2012-03-25 00:26:12 -07:00
|
|
|
-endif.
|