2011-11-23 21:52:14 -08:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
|
2011-11-23 21:52:14 -08: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.
|
|
|
|
|
|
|
|
|
|
|
|
-module(jsx_verify).
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
-export([is_json/2, is_term/2]).
|
2011-11-23 21:52:14 -08:00
|
|
|
-export([init/1, handle_event/2]).
|
|
|
|
|
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
-spec is_json(Source::binary(), Config::jsx_config:config()) -> true | false | {incomplete, jsx:decoder()}.
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
is_json(Source, Config) when is_list(Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
2011-11-23 21:52:14 -08:00
|
|
|
catch error:badarg -> false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
-spec is_term(Source::any(), Config::jsx_config:config()) -> true | false | {incomplete, jsx:encoder()}.
|
2012-03-04 18:40:00 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
is_term(Source, Config) when is_list(Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
2012-03-04 18:40:00 -08:00
|
|
|
catch error:badarg -> false
|
|
|
|
end.
|
|
|
|
|
2011-11-23 21:52:14 -08:00
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
parse_config(Config) -> parse_config(Config, []).
|
2011-11-23 21:52:14 -08:00
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
%% ignore deprecated flags
|
2013-03-04 20:34:04 -08:00
|
|
|
parse_config([no_repeated_keys|Rest], Config) ->
|
2014-12-07 07:59:51 +00:00
|
|
|
parse_config(Rest, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
|
2014-12-07 07:59:51 +00:00
|
|
|
parse_config(Rest, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([repeated_keys|Rest], Config) ->
|
2014-12-07 07:59:51 +00:00
|
|
|
parse_config(Rest, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{K, _}|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-03-04 20:34:04 -08:00
|
|
|
true -> parse_config(Rest, Config);
|
|
|
|
false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([K|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-03-04 20:34:04 -08:00
|
|
|
true -> parse_config(Rest, Config);
|
|
|
|
false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([], Config) ->
|
|
|
|
Config.
|
2011-11-23 21:52:14 -08:00
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
|
|
|
|
%% we don't actually need any state for this
|
|
|
|
-type state() :: [].
|
2013-09-19 17:04:38 +02:00
|
|
|
-spec init(Config::proplists:proplist()) -> state().
|
2011-11-23 21:52:14 -08:00
|
|
|
|
2014-12-07 07:59:51 +00:00
|
|
|
init(Config) -> parse_config(Config).
|
2011-11-29 19:39:01 -08:00
|
|
|
|
2011-11-23 21:52:14 -08:00
|
|
|
|
2013-09-19 17:04:38 +02:00
|
|
|
-spec handle_event(Event::any(), State::state()) -> state().
|
|
|
|
|
2011-11-23 21:52:14 -08:00
|
|
|
handle_event(end_json, _) -> true;
|
|
|
|
|
|
|
|
handle_event(_, State) -> State.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%% eunit tests
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2012-03-05 20:15:01 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
config_test_() ->
|
2012-03-05 20:15:01 -08:00
|
|
|
[
|
2014-12-07 07:59:51 +00:00
|
|
|
{"empty config", ?_assertEqual([], parse_config([]))},
|
|
|
|
{"no repeat keys", ?_assertEqual([], parse_config([no_repeated_keys]))},
|
|
|
|
{"bare repeated keys", ?_assertEqual([], parse_config([repeated_keys]))},
|
2013-02-04 23:08:44 -08:00
|
|
|
{"repeated keys true", ?_assertEqual(
|
2014-12-07 07:59:51 +00:00
|
|
|
[],
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{repeated_keys, true}])
|
2013-02-04 23:08:44 -08:00
|
|
|
)},
|
|
|
|
{"repeated keys false", ?_assertEqual(
|
2014-12-07 07:59:51 +00:00
|
|
|
[],
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{repeated_keys, false}])
|
2013-02-04 23:15:50 -08:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
|
|
|
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
|
2012-03-05 20:15:01 -08:00
|
|
|
].
|
2012-11-21 23:04:14 -08:00
|
|
|
|
|
|
|
|
2013-02-07 13:30:20 -08:00
|
|
|
handle_event_test_() ->
|
2013-12-18 02:13:50 +00:00
|
|
|
Data = jsx:test_cases() ++ jsx:special_test_cases(),
|
2013-02-07 13:30:20 -08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
Title, ?_assertEqual(
|
|
|
|
true,
|
2014-12-07 07:59:51 +00:00
|
|
|
lists:foldl(fun handle_event/2, [], Events ++ [end_json])
|
2013-02-07 13:30:20 -08:00
|
|
|
)
|
|
|
|
} || {Title, _, _, Events} <- Data
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-09-19 17:04:38 +02:00
|
|
|
-endif.
|