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
|
2010-06-28 18:14:14 -07:00
|
|
|
-export([parser/0, parser/1]).
|
2011-04-26 23:40:12 -07:00
|
|
|
-export([decoder/0, decoder/1]).
|
2011-08-10 00:33:52 -07:00
|
|
|
-export([encoder/0, encoder/1]).
|
2010-08-03 14:16:56 -07:00
|
|
|
-export([term_to_json/1, term_to_json/2]).
|
|
|
|
-export([json_to_term/1, json_to_term/2]).
|
|
|
|
-export([is_json/1, is_json/2]).
|
|
|
|
-export([format/1, format/2]).
|
2010-06-09 00:57:15 -07:00
|
|
|
|
2010-08-05 21:05:08 -07:00
|
|
|
|
2011-08-16 22:45:32 -07:00
|
|
|
-include("../include/jsx_common.hrl").
|
2010-06-26 07:27:38 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2011-04-26 23:40:12 -07:00
|
|
|
-spec parser() -> jsx_decoder().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2011-04-26 23:40:12 -07:00
|
|
|
parser() -> decoder([]).
|
|
|
|
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
-spec parser(OptsList::jsx_opts()) -> jsx_decoder().
|
2011-04-26 23:40:12 -07:00
|
|
|
|
|
|
|
parser(OptsList) -> decoder(OptsList).
|
|
|
|
|
|
|
|
|
|
|
|
-spec decoder() -> jsx_decoder().
|
|
|
|
|
|
|
|
decoder() -> decoder([]).
|
|
|
|
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
-spec decoder(OptsList::jsx_opts()) -> jsx_decoder().
|
2010-05-31 02:35:49 -07:00
|
|
|
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2011-04-26 23:40:12 -07:00
|
|
|
decoder(OptsList) ->
|
2011-08-10 01:28:25 -07:00
|
|
|
case parse_opts(OptsList) of
|
|
|
|
{error, badarg} -> {error, badarg}
|
|
|
|
; Opts ->
|
|
|
|
case Opts#opts.encoding of
|
|
|
|
utf8 -> jsx_utf8:decoder(Opts)
|
|
|
|
; utf16 -> jsx_utf16:decoder(Opts)
|
|
|
|
; utf32 -> jsx_utf32:decoder(Opts)
|
|
|
|
; {utf16, little} -> jsx_utf16le:decoder(Opts)
|
|
|
|
; {utf32, little} -> jsx_utf32le:decoder(Opts)
|
|
|
|
; auto -> jsx_utils:detect_encoding(Opts)
|
|
|
|
; _ -> {error, badarg}
|
|
|
|
end
|
2010-08-03 14:16:56 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-17 01:25:22 -07:00
|
|
|
-spec encoder() -> jsx_encoder().
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
encoder() -> encoder([]).
|
|
|
|
|
|
|
|
|
2011-08-10 01:28:25 -07:00
|
|
|
-spec encoder(OptsList::jsx_opts()) -> jsx_encoder().
|
2011-07-21 06:14:48 -07:00
|
|
|
|
2011-08-10 01:28:25 -07:00
|
|
|
encoder(OptsList) ->
|
|
|
|
case parse_opts(OptsList) of
|
|
|
|
{error, badarg} -> {error, badarg}
|
|
|
|
; Opts -> jsx_encoder:encoder(Opts)
|
|
|
|
end.
|
2011-07-17 01:25:22 -07:00
|
|
|
|
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
-spec json_to_term(JSON::binary()) -> jsx_term().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
json_to_term(JSON) ->
|
2010-09-27 14:27:39 -07:00
|
|
|
try json_to_term(JSON, [])
|
2010-09-28 12:52:58 -07:00
|
|
|
%% rethrow exception so internals aren't confusingly exposed to users
|
2011-07-04 21:07:50 -07:00
|
|
|
catch error:badarg -> erlang:error(badarg, [JSON])
|
2010-09-27 14:27:39 -07:00
|
|
|
end.
|
2010-08-19 23:30:22 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
-spec json_to_term(JSON::binary(), Opts::decoder_opts()) -> jsx_term().
|
2011-03-07 14:02:51 -08:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
json_to_term(JSON, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
jsx_terms:json_to_term(JSON, Opts).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
-spec term_to_json(JSON::jsx_term()) -> binary().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-19 23:30:22 -07:00
|
|
|
term_to_json(JSON) ->
|
2010-09-27 14:27:39 -07:00
|
|
|
try term_to_json(JSON, [])
|
2010-09-28 12:52:58 -07:00
|
|
|
%% rethrow exception so internals aren't confusingly exposed to users
|
2011-07-04 21:07:50 -07:00
|
|
|
catch error:badarg -> erlang:error(badarg, [JSON])
|
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
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
-spec term_to_json(JSON::jsx_term(), Opts::encoder_opts()) -> binary().
|
2010-08-19 23:30:22 -07:00
|
|
|
|
|
|
|
term_to_json(JSON, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
try jsx_terms:term_to_json(JSON, Opts)
|
2010-09-28 12:52:58 -07:00
|
|
|
%% rethrow exception so internals aren't confusingly exposed to users
|
2011-07-04 21:07:50 -07:00
|
|
|
catch error:badarg -> erlang:error(badarg, [JSON, Opts])
|
2010-09-27 14:27:39 -07:00
|
|
|
end.
|
2010-08-19 23:30:22 -07:00
|
|
|
|
|
|
|
|
2011-07-17 20:53:33 -07:00
|
|
|
-spec is_json(JSON::binary()) -> true | false
|
|
|
|
; (Terms::list(jsx_encodeable())) -> true | false.
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
is_json(JSON) ->
|
2010-08-19 23:30:22 -07:00
|
|
|
is_json(JSON, []).
|
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2011-07-17 20:53:33 -07:00
|
|
|
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false
|
|
|
|
; (Terms::list(jsx_encodeable()), Opts::verify_opts()) -> true | false.
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
is_json(JSON, Opts) ->
|
2010-08-03 20:29:49 -07:00
|
|
|
jsx_verify:is_json(JSON, Opts).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
|
2011-07-17 20:53:33 -07:00
|
|
|
-spec format(JSON::binary()) -> binary() | iolist()
|
|
|
|
; (Terms::list(jsx_encodeable())) -> binary() | iolist().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
format(JSON) ->
|
2010-08-19 23:30:22 -07:00
|
|
|
format(JSON, []).
|
2011-03-22 19:02:28 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2011-07-17 20:53:33 -07:00
|
|
|
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist()
|
|
|
|
; (Terms::list(jsx_encodeable()), Opts::format_opts()) ->
|
|
|
|
binary() | iolist().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
format(JSON, Opts) ->
|
2010-08-12 20:08:39 -07:00
|
|
|
jsx_format:format(JSON, Opts).
|
|
|
|
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-09-21 08:05:08 -07:00
|
|
|
|
2011-08-10 01:28:25 -07:00
|
|
|
parse_opts(Opts) ->
|
|
|
|
parse_opts(Opts, #opts{}).
|
|
|
|
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts;
|
|
|
|
parse_opts([loose_unicode|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{loose_unicode=true});
|
|
|
|
parse_opts([iterate|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{iterate=true});
|
|
|
|
parse_opts([escape_forward_slash|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{escape_forward_slash=true});
|
|
|
|
parse_opts([{encoding, Encoding}|Rest], Opts)
|
|
|
|
when Encoding =:= utf8; Encoding =:= utf16; Encoding =:= utf32;
|
|
|
|
Encoding =:= {utf16,little}; Encoding =:= {utf32,little};
|
|
|
|
Encoding =:= auto ->
|
|
|
|
parse_opts(Rest, Opts#opts{encoding=Encoding});
|
|
|
|
parse_opts(_, _) ->
|
|
|
|
{error, badarg}.
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
?_assert(begin {jsx, X, _} = (jsx:decoder())(
|
|
|
|
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>
|
|
|
|
), X end =:= begin {jsx, Y, _} = (jsx:encoder())(
|
|
|
|
[start_array,
|
|
|
|
{string, <<"a">>},
|
|
|
|
{integer, 17},
|
|
|
|
{float, 3.14},
|
|
|
|
{literal, true},
|
|
|
|
start_object,
|
|
|
|
{key, <<"k">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
start_array,
|
|
|
|
end_array,
|
|
|
|
end_array]
|
|
|
|
), Y end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
].
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
jsx_decoder_gen([]) -> [];
|
2011-02-07 23:43:15 -08:00
|
|
|
jsx_decoder_gen(Tests) ->
|
|
|
|
jsx_decoder_gen(Tests, [utf8,
|
|
|
|
utf16,
|
|
|
|
{utf16, little},
|
|
|
|
utf32,
|
|
|
|
{utf32, little}
|
|
|
|
]).
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
jsx_decoder_gen([_Test|Rest], []) ->
|
|
|
|
jsx_decoder_gen(Rest);
|
|
|
|
jsx_decoder_gen([Test|_] = Tests, [Encoding|Encodings]) ->
|
2011-02-07 23:43:15 -08:00
|
|
|
Name = lists:flatten(proplists:get_value(name, Test) ++ " :: " ++
|
|
|
|
io_lib:format("~p", [Encoding])
|
|
|
|
),
|
|
|
|
JSON = unicode:characters_to_binary(proplists:get_value(json, Test),
|
|
|
|
unicode,
|
|
|
|
Encoding
|
|
|
|
),
|
2010-09-21 08:05:08 -07:00
|
|
|
JSX = proplists:get_value(jsx, Test),
|
|
|
|
Flags = proplists:get_value(jsx_flags, Test, []),
|
|
|
|
{generator,
|
|
|
|
fun() ->
|
2011-08-09 17:49:42 -07:00
|
|
|
[{Name ++ " iterative",
|
|
|
|
?_assertEqual(iterative_decode(JSON, Flags), JSX)}
|
|
|
|
| {generator,
|
2011-07-26 19:46:31 -07:00
|
|
|
fun() -> [{Name ++ " incremental", ?_assertEqual(
|
|
|
|
incremental_decode(JSON, Flags), JSX)
|
2011-08-09 17:49:42 -07:00
|
|
|
} | {generator,
|
|
|
|
fun() ->
|
|
|
|
[{Name, ?_assertEqual(
|
|
|
|
decode(JSON, Flags), JSX)
|
|
|
|
} | jsx_decoder_gen(Tests, Encodings)]
|
|
|
|
end}
|
|
|
|
]
|
|
|
|
end}
|
2010-09-21 08:05:08 -07:00
|
|
|
]
|
|
|
|
end
|
|
|
|
}.
|
|
|
|
|
|
|
|
|
|
|
|
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-03 00:15:18 -07:00
|
|
|
erlang:error(Test)
|
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)
|
|
|
|
; _ -> erlang:error(badarg)
|
|
|
|
end;
|
|
|
|
parse_tests([KV|Rest], Dir, Acc) ->
|
|
|
|
parse_tests(Rest, Dir, [KV] ++ Acc);
|
|
|
|
parse_tests([], _Dir, Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
|
|
|
|
decode(JSON, Flags) ->
|
2011-04-26 23:40:12 -07:00
|
|
|
P = jsx:decoder(Flags),
|
2011-08-09 17:49:42 -07:00
|
|
|
case P(JSON) of
|
|
|
|
{error, {badjson, _}} -> {error, badjson}
|
|
|
|
; {jsx, incomplete, More} ->
|
|
|
|
case More(end_stream) of
|
|
|
|
{error, {badjson, _}} -> {error, badjson}
|
|
|
|
; {jsx, T, _} -> T
|
|
|
|
end
|
|
|
|
; {jsx, T, _} -> T
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
iterative_decode(JSON, Flags) ->
|
|
|
|
P = jsx:decoder([iterate] ++ Flags),
|
|
|
|
iterative_decode_loop(P(JSON), []).
|
2010-09-21 08:05:08 -07:00
|
|
|
|
2011-08-09 17:49:42 -07:00
|
|
|
iterative_decode_loop({jsx, end_json, _Next}, Acc) ->
|
2010-09-21 08:05:08 -07:00
|
|
|
lists:reverse([end_json] ++ Acc);
|
2011-08-09 17:49:42 -07:00
|
|
|
iterative_decode_loop({jsx, incomplete, More}, Acc) ->
|
|
|
|
iterative_decode_loop(More(end_stream), Acc);
|
|
|
|
iterative_decode_loop({jsx, E, Next}, Acc) ->
|
|
|
|
iterative_decode_loop(Next(), [E] ++ Acc);
|
|
|
|
iterative_decode_loop({error, {badjson, _Error}}, _Acc) ->
|
2011-07-26 19:28:41 -07:00
|
|
|
{error, badjson}.
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
|
2011-08-09 17:49:42 -07:00
|
|
|
P = jsx:decoder([iterate] ++ Flags),
|
2010-09-21 08:05:08 -07:00
|
|
|
incremental_decode_loop(P(C), Rest, []).
|
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
incremental_decode_loop({jsx, incomplete, Next}, <<>>, Acc) ->
|
2010-09-21 08:05:08 -07:00
|
|
|
incremental_decode_loop(Next(end_stream), <<>>, Acc);
|
2011-07-26 00:35:17 -07:00
|
|
|
incremental_decode_loop({jsx, incomplete, Next}, <<C:1/binary, Rest/binary>>, Acc) ->
|
2010-09-21 08:05:08 -07:00
|
|
|
incremental_decode_loop(Next(C), Rest, Acc);
|
2011-07-26 00:35:17 -07:00
|
|
|
incremental_decode_loop({jsx, end_json, _Next}, _Rest, Acc) ->
|
2010-09-21 08:05:08 -07:00
|
|
|
lists:reverse([end_json] ++ Acc);
|
2011-07-26 00:35:17 -07:00
|
|
|
incremental_decode_loop({jsx, Event, Next}, Rest, Acc) ->
|
2011-07-26 19:58:48 -07:00
|
|
|
incremental_decode_loop(Next(), Rest, [Event] ++ Acc);
|
|
|
|
incremental_decode_loop({error, {badjson, _Error}}, _Rest, _Acc) ->
|
|
|
|
{error, badjson}.
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
2011-07-04 21:07:50 -07:00
|
|
|
-endif.
|