jsx/src/jsx_verify.erl

229 lines
6.8 KiB
Erlang
Raw Normal View History

2011-11-23 21:52:14 -08:00
%% The MIT License
%% Copyright (c) 2010 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.
-module(jsx_verify).
-export([is_json/2, is_term/2]).
2011-11-23 21:52:14 -08:00
-export([init/1, handle_event/2]).
-record(opts, {
repeated_keys = true
}).
-type opts() :: [].
-spec is_json(Source::binary(), Opts::opts()) -> true | false.
2012-11-21 23:04:14 -08:00
is_json(Source, Opts) when is_list(Opts) ->
2012-03-05 19:53:55 -08:00
try (jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source)
2011-11-23 21:52:14 -08:00
catch error:badarg -> false
end.
-spec is_term(Source::any(), Opts::opts()) -> true | false.
is_term(Source, Opts) when is_list(Opts) ->
2012-03-05 19:53:55 -08:00
try (jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source)
catch error:badarg -> false
end.
2011-11-23 21:52:14 -08:00
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
parse_opts([{repeated_keys, Val}|Rest], Opts) when Val == true; Val == false ->
parse_opts(Rest, Opts#opts{repeated_keys = Val});
parse_opts([repeated_keys|Rest], Opts) ->
parse_opts(Rest, Opts#opts{repeated_keys = true});
parse_opts([_|Rest], Opts) ->
parse_opts(Rest, Opts);
parse_opts([], Opts) ->
Opts.
init(Opts) -> {parse_opts(Opts), []}.
2011-11-23 21:52:14 -08:00
handle_event(end_json, _) -> true;
handle_event(_, {Opts, _} = State) when Opts#opts.repeated_keys == true -> State;
handle_event(start_object, {Opts, Keys}) -> {Opts, [dict:new()] ++ Keys};
handle_event(end_object, {Opts, [_|Keys]}) -> {Opts, Keys};
handle_event({key, Key}, {Opts, [CurrentKeys|Keys]}) ->
case dict:is_key(Key, CurrentKeys) of
true -> erlang:error(badarg)
; false -> {Opts, [dict:store(Key, blah, CurrentKeys)|Keys]}
end;
handle_event(_, State) -> State.
%% eunit tests
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
2012-03-05 20:15:01 -08:00
json_true_test_() ->
2011-11-23 21:52:14 -08:00
[
2012-03-05 20:15:01 -08:00
{"empty object", ?_assert(is_json(<<"{}">>, []))},
{"empty array", ?_assert(is_json(<<"[]">>, []))},
2012-11-21 23:04:14 -08:00
{"whitespace",
?_assert(is_json(<<" \n \t \r [true] \t \n\r ">>,
2011-11-23 21:52:14 -08:00
[]
2012-03-05 20:15:01 -08:00
)
2011-11-23 21:52:14 -08:00
)
},
2012-11-21 23:04:14 -08:00
{"nested terms",
2011-11-23 21:52:14 -08:00
?_assert(is_json(
2012-11-21 23:04:14 -08:00
<<"[{ \"x\": [ {}, {}, {} ], \"y\": [{}] }, {}, [[[]]]]">>,
2011-11-23 21:52:14 -08:00
[]
2012-03-05 20:15:01 -08:00
)
2011-11-23 21:52:14 -08:00
)
},
2012-11-21 23:04:14 -08:00
{"numbers",
2011-11-23 21:52:14 -08:00
?_assert(is_json(
2012-11-21 23:04:14 -08:00
<<"[ -1.0, -1, -0, 0, 1e-1, 1, 1.0, 1e1 ]">>,
2011-11-23 21:52:14 -08:00
[]
2012-03-05 20:15:01 -08:00
)
2011-11-23 21:52:14 -08:00
)
},
2012-11-21 23:04:14 -08:00
{"strings",
2011-11-23 21:52:14 -08:00
?_assert(is_json(
2012-11-21 23:04:14 -08:00
<<"[ \"a\", \"string\", \"in\", \"multiple\", \"acts\" ]">>,
2011-11-23 21:52:14 -08:00
[]
2012-03-05 20:15:01 -08:00
)
2011-11-23 21:52:14 -08:00
)
},
2012-11-21 23:04:14 -08:00
{"literals",
2012-03-05 20:15:01 -08:00
?_assert(is_json(<<"[ true, false, null ]">>, []))
2011-11-23 21:52:14 -08:00
},
2012-11-21 23:04:14 -08:00
{"nested objects",
2012-03-05 20:15:01 -08:00
?_assert(is_json(<<"{\"key\": { \"key\": true}}">>, []))
2012-11-21 23:04:14 -08:00
},
2012-03-05 20:15:01 -08:00
{"repeated key ok", ?_assert(is_json(
<<"{\"key\": true, \"key\": true}">>,
[repeated_keys]
)
)
},
{"nested repeated key", ?_assert(is_json(
<<"{\"key\": { \"key\": true }}">>,
[{repeated_keys, false}]
)
)
2011-11-23 21:52:14 -08:00
}
].
2012-03-05 20:15:01 -08:00
json_false_test_() ->
2011-11-23 21:52:14 -08:00
[
2012-03-05 20:15:01 -08:00
{"unbalanced list", ?_assertNot(is_json(<<"[]]">>, []))},
2012-11-21 23:04:14 -08:00
{"trailing comma",
2012-03-05 20:15:01 -08:00
?_assertNot(is_json(<<"[ true, false, null, ]">>, []))
2011-11-23 21:52:14 -08:00
},
2012-03-05 20:15:01 -08:00
{"unquoted key", ?_assertNot(is_json(<<"{ key: false }">>, []))},
{"repeated key", ?_assertNot(is_json(
<<"{\"key\": true, \"key\": true}">>,
[{repeated_keys, false}])
)
2011-11-23 21:52:14 -08:00
},
2012-03-05 20:15:01 -08:00
{"nested repeated key", ?_assertNot(is_json(
<<"{\"key\": { \"a\": true, \"a\": false }}">>,
[{repeated_keys, false}])
)
2011-11-23 21:52:14 -08:00
}
].
2011-12-14 21:32:17 -08:00
2012-03-05 20:15:01 -08:00
json_incomplete_test_() ->
2011-12-14 21:32:17 -08:00
[
{"incomplete test", ?_assertMatch({incomplete, _}, is_json(<<"[">>, []))}
].
2012-03-05 20:15:01 -08:00
term_true_test_() ->
[
{"empty object", ?_assert(is_term([{}], []))},
{"empty array", ?_assert(is_term([], []))},
{"whitespace", ?_assert(is_term([ true ], []))},
2012-11-21 23:04:14 -08:00
{"nested terms",
?_assert(is_term([[{x, [[{}], [{}], [{}]]}, {y, [{}]}], [{}], [[[]]]], []))
2012-03-05 20:15:01 -08:00
},
2012-11-21 23:04:14 -08:00
{"numbers",
2012-03-05 20:15:01 -08:00
?_assert(is_term([-1.0, -1, -0, 0, 1.0e-1, 1, 1.0, 1.0e1], []))
},
2012-11-21 23:04:14 -08:00
{"strings",
2012-03-05 20:15:01 -08:00
?_assert(is_term(
2012-11-21 23:04:14 -08:00
[<<"a">>, <<"string">>, <<"in">>, <<"multiple">>, <<"acts">>],
2012-03-05 20:15:01 -08:00
[]
)
)
},
{"literals", ?_assert(is_term([ true, false, null ], []))},
2012-11-21 23:04:14 -08:00
{"nested objects", ?_assert(is_term([{key, [{key, true}]}], []))},
2012-03-05 20:15:01 -08:00
{"repeated key ok", ?_assert(is_term(
[{key, true}, {key, true}],
[]
)
)
},
{"nested repeated key", ?_assert(is_term(
[{key, [{key, true}]}],
[{repeated_keys, false}]
)
)
}
].
term_false_test_() ->
[
{"repeated key", ?_assertNot(is_term(
[{<<"key">>, true}, {<<"key">>, true}],
[{repeated_keys, false}]
)
)
},
{"repeated key alternate representation", ?_assertNot(is_term(
[{<<"key">>, true}, {key, true}],
[{repeated_keys, false}]
)
)
},
{"repeated key alternate representation two", ?_assertNot(is_term(
[{key, true}, {'key', true}],
[{repeated_keys, false}]
)
)
},
{"nested repeated key", ?_assertNot(is_term(
[{key, [{a, true}, {a, false}]}],
[{repeated_keys, false}]
)
)
}
].
2012-11-21 23:04:14 -08:00
2011-11-23 21:52:14 -08:00
-endif.