2010-05-25 21:26:55 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2010-05-25 21:48:36 -07:00
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
2010-05-25 21:26:55 -07: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.
|
|
|
|
|
|
|
|
|
2010-05-18 13:01:49 -07:00
|
|
|
-module(jsx).
|
|
|
|
|
2010-08-19 18:22:34 -07:00
|
|
|
|
2010-06-20 17:00:08 -07:00
|
|
|
%% the core parser api
|
2011-08-31 18:52:01 -07:00
|
|
|
-export([scanner/0, scanner/1]).
|
2010-06-09 00:57:15 -07:00
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
-include("../include/jsx_types.hrl").
|
2010-08-05 21:05:08 -07:00
|
|
|
|
2010-06-26 07:27:38 -07:00
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
-spec scanner() -> jsx_scanner().
|
|
|
|
scanner() -> scanner([]).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
-spec scanner(OptsList::jsx_opts()) -> jsx_scanner().
|
|
|
|
scanner(OptsList) ->
|
|
|
|
fun(Stream) when is_binary(Stream) ->
|
2011-10-19 06:51:36 -07:00
|
|
|
(jsx_decoder:decoder(OptsList))(Stream)
|
2011-08-31 18:52:01 -07:00
|
|
|
; (Stream) when is_list(Stream); is_tuple(Stream) ->
|
|
|
|
(jsx_tokenizer:tokenizer(OptsList))(Stream)
|
2010-09-27 14:27:39 -07:00
|
|
|
end.
|
2011-03-22 19:02:28 -07:00
|
|
|
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-09-21 08:05:08 -07:00
|
|
|
-ifdef(TEST).
|
2011-08-09 21:02:20 -07:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
jsx_decoder_test_() ->
|
|
|
|
jsx_decoder_gen(load_tests(?eunit_test_path)).
|
2011-08-13 07:22:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
encoder_decoder_equiv_test_() ->
|
|
|
|
[
|
|
|
|
{"encoder/decoder equivalency",
|
2011-08-31 18:52:01 -07:00
|
|
|
?_assert(begin {ok, X, _} = (jsx:scanner())(
|
2011-08-13 07:22:46 -07:00
|
|
|
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>
|
2011-08-31 18:52:01 -07:00
|
|
|
), X end =:= begin {ok, Y, _} = (jsx:scanner())(
|
2011-08-13 07:22:46 -07:00
|
|
|
[start_array,
|
|
|
|
{string, <<"a">>},
|
|
|
|
{integer, 17},
|
|
|
|
{float, 3.14},
|
|
|
|
{literal, true},
|
|
|
|
start_object,
|
|
|
|
{key, <<"k">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
start_array,
|
|
|
|
end_array,
|
2011-08-31 18:52:01 -07:00
|
|
|
end_array,
|
|
|
|
end_json]
|
2011-08-13 07:22:46 -07:00
|
|
|
), Y end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
].
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
jsx_decoder_gen([]) -> [];
|
2011-08-31 18:52:01 -07:00
|
|
|
jsx_decoder_gen([Test|Rest]) ->
|
|
|
|
Name = proplists:get_value(name, Test),
|
|
|
|
JSON = proplists:get_value(json, Test),
|
2010-09-21 08:05:08 -07:00
|
|
|
JSX = proplists:get_value(jsx, Test),
|
|
|
|
Flags = proplists:get_value(jsx_flags, Test, []),
|
2011-08-31 18:52:01 -07:00
|
|
|
{generator, fun() ->
|
2011-08-31 19:35:09 -07:00
|
|
|
[{Name, ?_assertEqual(decode(JSON, Flags), JSX)},
|
|
|
|
{Name ++ " (incremental)",
|
|
|
|
?_assertEqual(incremental_decode(JSON, Flags), JSX)
|
|
|
|
}
|
|
|
|
| jsx_decoder_gen(Rest)
|
|
|
|
]
|
2011-08-31 18:52:01 -07:00
|
|
|
end}.
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
load_tests(Path) ->
|
|
|
|
%% search the specified directory for any files with the .test ending
|
|
|
|
TestSpecs = filelib:wildcard("*.test", Path),
|
|
|
|
load_tests(TestSpecs, Path, []).
|
|
|
|
|
|
|
|
load_tests([], _Dir, Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
load_tests([Test|Rest], Dir, Acc) ->
|
|
|
|
case file:consult(Dir ++ "/" ++ Test) of
|
|
|
|
{ok, TestSpec} ->
|
2011-07-31 18:43:14 -07:00
|
|
|
ParsedTest = parse_tests(TestSpec, Dir),
|
|
|
|
load_tests(Rest, Dir, [ParsedTest] ++ Acc)
|
2010-09-21 08:05:08 -07:00
|
|
|
; {error, _Reason} ->
|
2011-08-31 18:52:01 -07:00
|
|
|
erlang:error(badarg, [Test|Rest], Dir, Acc)
|
2010-09-21 08:05:08 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
parse_tests(TestSpec, Dir) ->
|
|
|
|
parse_tests(TestSpec, Dir, []).
|
|
|
|
|
|
|
|
parse_tests([{json, Path}|Rest], Dir, Acc) when is_list(Path) ->
|
|
|
|
case file:read_file(Dir ++ "/" ++ Path) of
|
|
|
|
{ok, Bin} -> parse_tests(Rest, Dir, [{json, Bin}] ++ Acc)
|
2011-08-31 18:52:01 -07:00
|
|
|
; _ -> erlang:error(badarg, [[{json, Path}|Rest], Dir, Acc])
|
2010-09-21 08:05:08 -07:00
|
|
|
end;
|
|
|
|
parse_tests([KV|Rest], Dir, Acc) ->
|
|
|
|
parse_tests(Rest, Dir, [KV] ++ Acc);
|
|
|
|
parse_tests([], _Dir, Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
|
|
|
|
decode(JSON, Flags) ->
|
2011-08-31 18:52:01 -07:00
|
|
|
try
|
|
|
|
P = jsx:scanner(Flags),
|
|
|
|
{ok, X, More} = P(JSON),
|
|
|
|
{ok, Y, _More} = More(<<" ">>),
|
|
|
|
V = X ++ Y,
|
|
|
|
case lists:reverse(V) of
|
|
|
|
[end_json|_] -> V
|
|
|
|
; _ -> {error, badjson}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
error:badarg -> {error, badjson}
|
2011-08-09 17:49:42 -07:00
|
|
|
end.
|
|
|
|
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
|
2011-08-31 19:35:09 -07:00
|
|
|
P = jsx:scanner(Flags),
|
|
|
|
try incremental_decode_loop(P(C), Rest, [])
|
|
|
|
catch error:badarg -> io:format("~p~n", [erlang:get_stacktrace()]), {error, badjson}
|
|
|
|
end.
|
|
|
|
|
|
|
|
incremental_decode_loop({ok, X, More}, <<>>, Acc) ->
|
|
|
|
{ok, Y, _} = More(<<" ">>), %% clear any naked numbers
|
|
|
|
V = Acc ++ X ++ Y,
|
|
|
|
case lists:reverse(V) of
|
|
|
|
[end_json|_] -> V
|
|
|
|
; _ -> {error, badjson}
|
|
|
|
end;
|
|
|
|
incremental_decode_loop({ok, T, More}, <<C:1/binary, Rest/binary>>, Acc) ->
|
|
|
|
incremental_decode_loop(More(C), Rest, Acc ++ T).
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
2011-07-04 21:07:50 -07:00
|
|
|
-endif.
|