jsx/src/jsx_verify.erl

207 lines
6 KiB
Erlang
Raw Normal View History

%% 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.
2010-08-19 23:30:22 -07:00
-module(jsx_verify).
-export([is_json/2]).
2011-05-09 15:33:23 -03:00
-include("jsx_common.hrl").
2010-08-23 19:59:23 -07:00
-ifdef(TEST).
2010-08-07 18:09:07 -07:00
-include_lib("eunit/include/eunit.hrl").
-endif.
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
is_json(JSON, Opts) ->
P = jsx:decoder(extract_parser_opts(Opts)),
case proplists:get_value(strict, Opts, false) of
true -> collect_strict(P(JSON), [[]])
; false -> collect(P(JSON), [[]])
end.
extract_parser_opts(Opts) ->
extract_parser_opts(Opts, []).
extract_parser_opts([], Acc) -> Acc;
extract_parser_opts([{K,V}|Rest], Acc) ->
case lists:member(K, [comments, encoding, unquoted_keys]) of
true -> [{K,V}] ++ Acc
; false -> extract_parser_opts(Rest, Acc)
end;
extract_parser_opts([K|Rest], Acc) ->
case lists:member(K, [comments, encoding, unquoted_keys]) of
true -> [K] ++ Acc
; false -> extract_parser_opts(Rest, Acc)
end.
%% enforce only arrays and objects at top level
collect_strict({event, start_object, Next}, Keys) ->
collect(Next(), Keys);
collect_strict({event, start_array, Next}, Keys) ->
collect(Next(), Keys);
collect_strict(_, _) ->
false.
collect({event, end_json, _Next}, _Keys) ->
true;
%% allocate new key accumulator at start_object, discard it at end_object
collect({event, start_object, Next}, Keys) -> collect(Next(), [[]|Keys]);
collect({event, end_object, Next}, [_|Keys]) -> collect(Next(), [Keys]);
%% check to see if key has already been encountered, if not add it to the key
%% accumulator and continue, else return false
collect({event, {key, Key}, Next}, [Current|Keys]) ->
case lists:member(Key, Current) of
true -> false
; false -> collect(Next(), [[Key] ++ Current] ++ Keys)
end;
collect({event, _, Next}, Keys) ->
collect(Next(), Keys);
2010-08-07 18:09:07 -07:00
%% needed to parse numbers that don't have trailing whitespace in less strict
%% mode
2010-08-07 18:09:07 -07:00
collect({incomplete, More}, Keys) ->
collect(More(end_stream), Keys);
2010-08-07 18:09:07 -07:00
collect(_, _) ->
false.
2010-08-07 18:09:07 -07:00
%% eunit tests
2010-08-23 19:59:23 -07:00
-ifdef(TEST).
2010-08-07 18:09:07 -07:00
true_test_() ->
[
{"empty object", ?_assert(is_json(<<"{}">>, []) =:= true)},
{"empty array", ?_assert(is_json(<<"[]">>, []) =:= true)},
{"whitespace",
?_assert(is_json(<<" \n \t \r [true] \t \n\r ">>,
[]
) =:= true
)
},
{"nested terms",
?_assert(is_json(
<<"[{ \"x\": [ {}, {}, {} ], \"y\": [{}] }, {}, [[[]]]]">>,
[]
) =:= true
)
},
{"numbers",
?_assert(is_json(
<<"[ -1.0, -1, -0, 0, 1e-1, 1, 1.0, 1e1 ]">>,
[]
) =:= true
)
},
{"strings",
?_assert(is_json(
<<"[ \"a\", \"string\", \"in\", \"multiple\", \"acts\" ]">>,
[]
) =:= true
)
},
{"literals",
?_assert(is_json(<<"[ true, false, null ]">>, []) =:= true)
},
{"nested objects",
?_assert(is_json(<<"{\"key\": { \"key\": true}}">>, []) =:= true)
}
2010-08-07 18:09:07 -07:00
].
false_test_() ->
[
{"naked true", ?_assert(is_json(<<"true">>, []) =:= true)},
{"naked number", ?_assert(is_json(<<"1">>, []) =:= true)},
{"naked string",
?_assert(is_json(<<"\"i am not really json\"">>, []) =:= true)
},
2010-08-07 18:09:07 -07:00
{"unbalanced list", ?_assert(is_json(<<"[[[]]">>, []) =:= false)},
{"trailing comma",
?_assert(is_json(<<"[ true, false, null, ]">>, []) =:= false)
},
2010-08-07 18:09:07 -07:00
{"unquoted key", ?_assert(is_json(<<"{ key: false }">>, []) =:= false)},
{"repeated key",
?_assert(is_json(
<<"{\"key\": true, \"key\": true}">>,
[]
) =:= false
)
},
2010-08-07 18:09:07 -07:00
{"comments", ?_assert(is_json(<<"[ /* a comment */ ]">>, []) =:= false)}
].
less_strict_test_() ->
[
{"naked true",
?_assert(is_json(<<"true">>, [{strict, false}]) =:= true)
},
{"naked number",
?_assert(is_json(<<"1">>, [{strict, false}]) =:= true)
},
{"naked string",
?_assert(is_json(
<<"\"i am not json\"">>,
[{strict, false}]
) =:= true
)
},
{"comments",
?_assert(is_json(
<<"[ /* a comment */ ]">>,
[{comments, true}]
) =:= true
)
},
{"unquoted keys",
?_assert(is_json(
<<"{unquotedkey : true}">>,
[{unquoted_keys, true}]
) =:= true
)
}
].
2010-08-07 18:09:07 -07:00
-endif.