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).
|
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
-export([to_json/1, to_json/2]).
|
|
|
|
-export([to_term/1, to_term/2]).
|
2012-03-04 18:40:00 -08:00
|
|
|
-export([is_json/1, is_json/2, is_term/1, is_term/2]).
|
2012-05-12 23:28:48 +00:00
|
|
|
-export([format/1, format/2, minify/1, prettify/1]).
|
2012-03-04 15:46:41 -08:00
|
|
|
-export([encoder/3, decoder/3]).
|
2011-11-29 19:56:00 -08:00
|
|
|
%% old api
|
2012-01-31 20:44:35 -08:00
|
|
|
-export([term_to_json/1, term_to_json/2, json_to_term/1, json_to_term/2]).
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
%% test handler
|
|
|
|
-ifdef(TEST).
|
|
|
|
-export([init/1, handle_event/2]).
|
|
|
|
-endif.
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2012-05-22 21:51:23 -07:00
|
|
|
-type json() :: list({binary(), json()})
|
|
|
|
| list(json())
|
|
|
|
| true
|
|
|
|
| false
|
|
|
|
| null
|
|
|
|
| integer()
|
|
|
|
| float()
|
|
|
|
| binary().
|
|
|
|
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2012-05-22 21:51:23 -07:00
|
|
|
-spec to_json(Source::json()) -> binary().
|
|
|
|
-spec to_json(Source::json(), Opts::jsx_to_json:opts()) -> binary().
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
to_json(Source) -> to_json(Source, []).
|
|
|
|
|
2012-03-02 22:53:23 -08:00
|
|
|
to_json(Source, Opts) -> jsx_to_json:to_json(Source, Opts).
|
2012-01-31 20:44:35 -08:00
|
|
|
|
|
|
|
%% old api, alias for to_json/x
|
|
|
|
|
|
|
|
term_to_json(Source) -> to_json(Source, []).
|
2012-03-05 20:37:22 -08:00
|
|
|
|
2012-01-31 20:44:35 -08:00
|
|
|
term_to_json(Source, Opts) -> to_json(Source, Opts).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
2011-10-24 22:51:39 -07:00
|
|
|
|
2011-11-29 19:56:00 -08:00
|
|
|
-spec format(Source::binary()) -> binary().
|
|
|
|
-spec format(Source::binary(), Opts::jsx_to_json:opts()) -> binary().
|
|
|
|
|
|
|
|
format(Source) -> format(Source, []).
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
format(Source, Opts) -> jsx_to_json:format(Source, Opts).
|
2011-11-29 19:56:00 -08:00
|
|
|
|
|
|
|
|
2012-05-12 23:28:48 +00:00
|
|
|
-spec minify(Source::binary()) -> binary().
|
|
|
|
|
|
|
|
minify(Source) -> format(Source, []).
|
|
|
|
|
|
|
|
|
|
|
|
-spec prettify(Source::binary()) -> binary().
|
|
|
|
|
|
|
|
prettify(Source) -> format(Source, [space, {indent, 2}]).
|
|
|
|
|
|
|
|
|
2012-05-22 21:51:23 -07:00
|
|
|
-spec to_term(Source::binary()) -> json().
|
|
|
|
-spec to_term(Source::binary(), Opts::jsx_to_term:opts()) -> json().
|
2012-03-25 00:26:12 -07:00
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
to_term(Source) -> to_term(Source, []).
|
|
|
|
|
2012-03-02 22:53:23 -08:00
|
|
|
to_term(Source, Opts) -> jsx_to_term:to_term(Source, Opts).
|
2011-11-29 19:56:00 -08:00
|
|
|
|
2012-01-31 20:44:35 -08:00
|
|
|
%% old api, alias for to_term/x
|
2011-11-29 19:56:00 -08:00
|
|
|
|
2012-01-31 20:44:35 -08:00
|
|
|
json_to_term(Source) -> to_term(Source, []).
|
2012-03-05 20:37:22 -08:00
|
|
|
|
2012-01-31 20:44:35 -08:00
|
|
|
json_to_term(Source, Opts) -> to_term(Source, Opts).
|
2011-11-29 19:56:00 -08:00
|
|
|
|
|
|
|
|
2012-05-22 21:51:23 -07:00
|
|
|
-spec is_json(Source::any()) -> true | false.
|
|
|
|
-spec is_json(Source::any(), Opts::jsx_verify:opts()) -> true | false.
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
is_json(Source) -> is_json(Source, []).
|
|
|
|
|
|
|
|
is_json(Source, Opts) -> jsx_verify:is_json(Source, Opts).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
-spec is_term(Source::any()) -> true | false.
|
|
|
|
-spec is_term(Source::any(), Opts::jsx_verify:opts()) -> true | false.
|
|
|
|
|
|
|
|
is_term(Source) -> is_term(Source, []).
|
|
|
|
|
|
|
|
is_term(Source, Opts) -> jsx_verify:is_term(Source, Opts).
|
|
|
|
|
|
|
|
|
2012-03-04 15:46:41 -08:00
|
|
|
-spec decoder(Handler::module(), State::any(), Opts::list()) -> fun().
|
|
|
|
|
|
|
|
decoder(Handler, State, Opts) -> jsx_decoder:decoder(Handler, State, Opts).
|
|
|
|
|
|
|
|
|
|
|
|
-spec encoder(Handler::module(), State::any(), Opts::list()) -> fun().
|
|
|
|
|
|
|
|
encoder(Handler, State, Opts) -> jsx_encoder:encoder(Handler, State, Opts).
|
|
|
|
|
|
|
|
|
2011-10-21 18:16:16 -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_() ->
|
2012-03-14 06:36:36 -07:00
|
|
|
jsx_decoder_gen(load_tests(code:lib_dir(jsx, priv) ++ "/test_cases/")).
|
2011-08-13 07:22:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
encoder_decoder_equiv_test_() ->
|
|
|
|
[
|
|
|
|
{"encoder/decoder equivalency",
|
2012-01-31 20:56:08 -08:00
|
|
|
?_assert((jsx_decoder:decoder(?MODULE, [], []))(
|
2011-08-13 07:22:46 -07:00
|
|
|
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>
|
2012-01-31 20:56:08 -08:00
|
|
|
) =:= (jsx_encoder:encoder(?MODULE, [], []))([<<"a">>, 17, 3.14, true, [{<<"k">>, false}], []])
|
2011-08-13 07:22:46 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
].
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2012-03-31 21:58:18 -07:00
|
|
|
single_quoted_strings_test_() ->
|
2012-03-14 23:01:59 -07:00
|
|
|
[
|
|
|
|
{"single quoted keys",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"{'key':true}">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[{<<"key">>, true}]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"multiple single quoted keys",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"{'key':true, 'another key':true}">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[{<<"key">>, true}, {<<"another key">>, true}]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"nested single quoted keys",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"{'key': {'key':true, 'another key':true}}">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[{<<"key">>, [{<<"key">>, true}, {<<"another key">>, true}]}]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"single quoted string",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"['string']">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[<<"string">>]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"single quote in double quoted string",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"[\"a single quote: '\"]">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[<<"a single quote: '">>]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"escaped single quote in single quoted string",
|
|
|
|
?_assertEqual(
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"['a single quote: \\'']">>, [single_quoted_strings]),
|
2012-03-14 23:01:59 -07:00
|
|
|
[<<"a single quote: '">>]
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"escaped single quote when single quotes are disallowed",
|
|
|
|
?_assertError(
|
|
|
|
badarg,
|
|
|
|
to_term(<<"[\"a single quote: \\'\"]">>)
|
|
|
|
)
|
2012-03-14 23:01:59 -07:00
|
|
|
},
|
|
|
|
{"mismatched quotes",
|
|
|
|
?_assertError(
|
|
|
|
badarg,
|
2012-03-31 21:58:18 -07:00
|
|
|
to_term(<<"['mismatched\"]">>, [single_quoted_strings])
|
2012-03-14 23:01:59 -07:00
|
|
|
)
|
2012-03-14 23:01:59 -07:00
|
|
|
}
|
|
|
|
].
|
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
%% test handler
|
|
|
|
init([]) -> [].
|
|
|
|
|
|
|
|
handle_event(end_json, State) -> lists:reverse([end_json] ++ State);
|
|
|
|
handle_event(Event, State) -> [Event] ++ State.
|
2010-09-21 08:05:08 -07:00
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
|
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
|
2012-03-02 22:53:23 -08:00
|
|
|
case (jsx_decoder:decoder(?MODULE, [], Flags))(JSON) of
|
2011-11-23 20:54:10 -08:00
|
|
|
{incomplete, More} ->
|
2011-10-21 18:16:16 -07:00
|
|
|
case More(<<" ">>) of
|
2011-11-23 20:54:10 -08:00
|
|
|
{incomplete, _} -> {error, badjson}
|
|
|
|
; Events -> Events
|
2011-10-21 18:16:16 -07:00
|
|
|
end
|
2011-11-23 20:54:10 -08:00
|
|
|
; Events -> Events
|
2011-08-31 18:52:01 -07:00
|
|
|
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) ->
|
2012-03-02 22:53:23 -08:00
|
|
|
P = jsx_decoder:decoder(?MODULE, [], Flags ++ [explicit_end]),
|
2011-10-21 18:16:16 -07:00
|
|
|
try incremental_decode_loop(P(C), Rest)
|
2012-03-26 22:10:09 -07:00
|
|
|
catch error:badarg -> {error, badjson}
|
2011-08-31 19:35:09 -07:00
|
|
|
end.
|
|
|
|
|
2011-10-21 18:16:16 -07:00
|
|
|
incremental_decode_loop({incomplete, More}, <<>>) ->
|
|
|
|
case More(end_stream) of
|
2011-11-23 20:54:10 -08:00
|
|
|
{incomplete, _} -> {error, badarg}
|
|
|
|
; X -> X
|
2011-08-31 19:35:09 -07:00
|
|
|
end;
|
2011-10-21 18:16:16 -07:00
|
|
|
incremental_decode_loop({incomplete, More}, <<C:1/binary, Rest/binary>>) ->
|
|
|
|
incremental_decode_loop(More(C), Rest).
|
2010-09-21 08:05:08 -07:00
|
|
|
|
|
|
|
|
2012-03-25 00:26:12 -07:00
|
|
|
-endif.
|