189 lines
5.5 KiB
Erlang
189 lines
5.5 KiB
Erlang
%% 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]).
|
|
|
|
|
|
-include("jsx_common.hrl").
|
|
|
|
|
|
|
|
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false
|
|
; (F::jsx_iterator(), Opts::verify_opts()) -> true | false.
|
|
|
|
is_json(JSON, OptsList) when is_binary(JSON) ->
|
|
P = jsx:decoder(extract_parser_opts(OptsList)),
|
|
is_json(fun() -> P(JSON) end, OptsList);
|
|
is_json(F, OptsList) when is_function(F) ->
|
|
case proplists:get_value(strict, OptsList, false) of
|
|
true -> collect_strict(F(), [[]])
|
|
; false -> collect(F(), [[]])
|
|
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, [encoding]) of
|
|
true -> [{K,V}] ++ Acc
|
|
; false -> extract_parser_opts(Rest, Acc)
|
|
end;
|
|
extract_parser_opts([K|Rest], Acc) ->
|
|
case lists:member(K, [encoding]) 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);
|
|
|
|
|
|
%% needed to parse numbers that don't have trailing whitespace in less strict
|
|
%% mode
|
|
collect({incomplete, More}, Keys) ->
|
|
collect(More(end_stream), Keys);
|
|
|
|
|
|
collect(_, _) ->
|
|
false.
|
|
|
|
|
|
|
|
%% eunit tests
|
|
-ifdef(TEST).
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
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)
|
|
}
|
|
].
|
|
|
|
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)
|
|
},
|
|
{"unbalanced list", ?_assert(is_json(<<"[[[]]">>, []) =:= false)},
|
|
{"trailing comma",
|
|
?_assert(is_json(<<"[ true, false, null, ]">>, []) =:= false)
|
|
},
|
|
{"repeated key",
|
|
?_assert(is_json(
|
|
<<"{\"key\": true, \"key\": true}">>,
|
|
[]
|
|
) =:= 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
|
|
)
|
|
}
|
|
].
|
|
|
|
-endif.
|
|
|
|
|
|
|