2011-11-24 06:33:15 -08:00
|
|
|
%% The MIT License
|
|
|
|
|
|
|
|
%% Copyright (c) 2011 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
|
|
|
|
|
|
%% 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]).
|
|
|
|
|
|
|
|
|
|
|
|
-record(opts, {
|
2012-04-06 08:51:50 -07:00
|
|
|
labels = binary,
|
|
|
|
post_decode = false
|
2011-11-24 06:33:15 -08:00
|
|
|
}).
|
|
|
|
|
|
|
|
-type opts() :: list().
|
|
|
|
|
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
|
|
|
|
2012-03-25 13:18:26 -07:00
|
|
|
-spec to_term(Source::binary(), Opts::opts()) -> json_value().
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2011-11-29 21:11:17 -08:00
|
|
|
to_term(Source, Opts) when is_list(Opts) ->
|
2012-03-05 19:53:55 -08:00
|
|
|
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source).
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
|
|
|
|
|
|
|
|
parse_opts([{labels, Val}|Rest], Opts)
|
|
|
|
when Val == binary; Val == atom; Val == existing_atom ->
|
|
|
|
parse_opts(Rest, Opts#opts{labels = Val});
|
|
|
|
parse_opts([labels|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{labels = binary});
|
2012-04-06 08:51:50 -07:00
|
|
|
parse_opts([{post_decode, F}|Rest], Opts=#opts{post_decode=false}) when is_function(F, 1) ->
|
|
|
|
parse_opts(Rest, Opts#opts{post_decode=F});
|
|
|
|
parse_opts([{post_decode, _}|_] = Options, Opts) ->
|
|
|
|
erlang:error(badarg, [Options, Opts]);
|
2011-11-24 06:33:15 -08:00
|
|
|
parse_opts([_|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts);
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts.
|
|
|
|
|
|
|
|
|
2011-11-29 19:39:01 -08:00
|
|
|
init(Opts) -> {[[]], parse_opts(Opts)}.
|
|
|
|
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(end_json, {[[Terms]], _Opts}) -> Terms;
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(start_object, {Terms, Opts}) -> {[[]|Terms], Opts};
|
|
|
|
handle_event(end_object, {[[], {key, Key}, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[{Key, post_decode([{}], Opts)}] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(end_object, {[Object, {key, Key}, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[{Key, post_decode(lists:reverse(Object), Opts)}] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(end_object, {[[], Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[post_decode([{}], Opts)] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(end_object, {[Object, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[post_decode(lists:reverse(Object), Opts)] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
|
|
|
|
handle_event(start_array, {Terms, Opts}) -> {[[]|Terms], Opts};
|
|
|
|
handle_event(end_array, {[List, {key, Key}, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[{Key, post_decode(lists:reverse(List), Opts)}] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event(end_array, {[Current, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[post_decode(lists:reverse(Current), Opts)] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
|
2011-11-28 15:24:44 -08:00
|
|
|
handle_event({key, Key}, {Terms, Opts}) -> {[{key, format_key(Key, Opts)}] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
|
|
|
|
handle_event({_, Event}, {[{key, Key}, Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[{Key, post_decode(Event, Opts)}] ++ Last] ++ Terms, Opts};
|
2011-11-28 15:15:11 -08:00
|
|
|
handle_event({_, Event}, {[Last|Terms], Opts}) ->
|
2012-04-06 08:51:50 -07:00
|
|
|
{[[post_decode(Event, Opts)] ++ Last] ++ Terms, Opts}.
|
2011-11-24 06:33:15 -08:00
|
|
|
|
|
|
|
|
2011-11-28 15:24:44 -08:00
|
|
|
format_key(Key, Opts) ->
|
|
|
|
case Opts#opts.labels of
|
|
|
|
binary -> Key
|
|
|
|
; atom -> binary_to_atom(Key, utf8)
|
|
|
|
; existing_atom -> binary_to_existing_atom(Key, utf8)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2012-04-06 08:51:50 -07:00
|
|
|
post_decode(Value, #opts{post_decode=false}) -> Value;
|
|
|
|
post_decode(Value, Opts) -> (Opts#opts.post_decode)(Value).
|
|
|
|
|
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").
|
|
|
|
|
|
|
|
basic_test_() ->
|
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"empty object", ?_assertEqual(to_term(<<"{}">>, []), [{}])},
|
|
|
|
{"simple object", ?_assertEqual(to_term(<<"{\"key\": true}">>, []), [{<<"key">>, true}])},
|
|
|
|
{"less simple object", ?_assertEqual(
|
|
|
|
to_term(<<"{\"a\": 1, \"b\": 2}">>, []),
|
|
|
|
[{<<"a">>, 1}, {<<"b">>, 2}]
|
|
|
|
)},
|
|
|
|
{"nested object", ?_assertEqual(
|
|
|
|
to_term(<<"{\"key\": {\"key\": true}}">>, []),
|
|
|
|
[{<<"key">>, [{<<"key">>, true}]}]
|
|
|
|
)},
|
2011-11-28 15:15:11 -08:00
|
|
|
{"empty array", ?_assert(to_term(<<"[]">>, []) =:= [])},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"list of lists", ?_assertEqual(to_term(<<"[[],[],[]]">>, []), [[], [], []])},
|
|
|
|
{"list of strings", ?_assertEqual(to_term(<<"[\"hi\", \"there\"]">>, []), [<<"hi">>, <<"there">>])},
|
|
|
|
{"list of numbers", ?_assertEqual(to_term(<<"[1, 2.0, 3e4, -5]">>, []), [1, 2.0, 3.0e4, -5])},
|
|
|
|
{"list of literals", ?_assertEqual(to_term(<<"[true,false,null]">>, []), [true,false,null])},
|
|
|
|
{"list of objects", ?_assertEqual(
|
|
|
|
to_term(<<"[{}, {\"a\":1, \"b\":2}, {\"key\":[true,false]}]">>, []),
|
|
|
|
[[{}], [{<<"a">>,1},{<<"b">>,2}], [{<<"key">>,[true,false]}]]
|
|
|
|
)}
|
2011-11-28 15:15:11 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
comprehensive_test_() ->
|
2012-03-16 15:34:57 -07:00
|
|
|
{"comprehensive test", ?_assertEqual(to_term(comp_json(), []), comp_term())}.
|
2011-11-28 15:15:11 -08:00
|
|
|
|
|
|
|
comp_json() ->
|
|
|
|
<<"[
|
|
|
|
{\"a key\": {\"a key\": -17.346, \"another key\": 3e152, \"last key\": 14}},
|
|
|
|
[0,1,2,3,4,5],
|
|
|
|
[{\"a\": \"a\", \"b\": \"b\"}, {\"c\": \"c\", \"d\": \"d\"}],
|
|
|
|
[true, false, null],
|
|
|
|
{},
|
|
|
|
[],
|
|
|
|
[{},{}],
|
|
|
|
{\"key\": [], \"another key\": {}}
|
|
|
|
]">>.
|
|
|
|
|
|
|
|
comp_term() ->
|
|
|
|
[
|
|
|
|
[{<<"a key">>, [{<<"a key">>, -17.346}, {<<"another key">>, 3.0e152}, {<<"last key">>, 14}]}],
|
|
|
|
[0,1,2,3,4,5],
|
|
|
|
[[{<<"a">>, <<"a">>}, {<<"b">>, <<"b">>}], [{<<"c">>, <<"c">>}, {<<"d">>, <<"d">>}]],
|
|
|
|
[true, false, null],
|
|
|
|
[{}],
|
|
|
|
[],
|
|
|
|
[[{}], [{}]],
|
|
|
|
[{<<"key">>, []}, {<<"another key">>, [{}]}]
|
2011-11-24 06:33:15 -08:00
|
|
|
].
|
2011-11-28 15:24:44 -08:00
|
|
|
|
|
|
|
atom_labels_test_() ->
|
2012-03-16 15:34:57 -07:00
|
|
|
{"atom labels test", ?_assertEqual(to_term(comp_json(), [{labels, atom}]), atom_term())}.
|
2011-11-28 15:24:44 -08:00
|
|
|
|
|
|
|
atom_term() ->
|
|
|
|
[
|
|
|
|
[{'a key', [{'a key', -17.346}, {'another key', 3.0e152}, {'last key', 14}]}],
|
|
|
|
[0,1,2,3,4,5],
|
|
|
|
[[{a, <<"a">>}, {b, <<"b">>}], [{'c', <<"c">>}, {'d', <<"d">>}]],
|
|
|
|
[true, false, null],
|
|
|
|
[{}],
|
|
|
|
[],
|
|
|
|
[[{}], [{}]],
|
|
|
|
[{key, []}, {'another key', [{}]}]
|
|
|
|
].
|
2011-11-28 17:22:54 -08:00
|
|
|
|
|
|
|
naked_test_() ->
|
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked integer", ?_assertEqual(to_term(<<"123">>, []), 123)},
|
|
|
|
{"naked float", ?_assertEqual(to_term(<<"-4.32e-17">>, []), -4.32e-17)},
|
|
|
|
{"naked literal", ?_assertEqual(to_term(<<"true">>, []), true)},
|
|
|
|
{"naked string", ?_assertEqual(to_term(<<"\"string\"">>, []), <<"string">>)}
|
2011-11-28 17:22:54 -08:00
|
|
|
].
|
2011-11-24 06:33:15 -08:00
|
|
|
|
2012-03-25 00:26:12 -07:00
|
|
|
-endif.
|