-
Notifications
You must be signed in to change notification settings - Fork 219
/
jsx_verify.erl
121 lines (89 loc) · 3.95 KB
/
jsx_verify.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
%% The MIT License
%% Copyright (c) 2010-2013 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]).
-export([init/1, handle_event/2]).
-type config() :: proplists:proplist().
-spec is_json(Source::binary(), Config::jsx_config:options()) -> true | false | {incomplete, jsx:decoder()}.
is_json(Source, Config) when is_list(Config) ->
try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
catch error:badarg -> false
end.
-spec is_term(Source::jsx:json_term() | end_stream | end_json,
Config::jsx_config:options()) -> true | false | {incomplete, jsx:encoder()}.
is_term(Source, Config) when is_list(Config) ->
try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
catch error:badarg -> false
end.
parse_config(Config) -> parse_config(Config, []).
%% ignore deprecated flags
parse_config([no_repeated_keys|Rest], Config) ->
parse_config(Rest, Config);
parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
parse_config(Rest, Config);
parse_config([repeated_keys|Rest], Config) ->
parse_config(Rest, Config);
parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_config:valid_flags()) of
true -> parse_config(Rest, Config);
false -> erlang:error(badarg, [Options, Config])
end;
parse_config([K|Rest] = Options, Config) ->
case lists:member(K, jsx_config:valid_flags()) of
true -> parse_config(Rest, Config);
false -> erlang:error(badarg, [Options, Config])
end;
parse_config([], Config) ->
Config.
%% we don't actually need any state for this
-type state() :: [].
-spec init(Config::config()) -> state().
init(Config) -> parse_config(Config).
-spec handle_event(Event::any(), State::state()) -> state().
handle_event(end_json, _) -> true;
handle_event(_, State) -> State.
%% eunit tests
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
config_test_() ->
[
{"empty config", ?_assertEqual([], parse_config([]))},
{"no repeat keys", ?_assertEqual([], parse_config([no_repeated_keys]))},
{"bare repeated keys", ?_assertEqual([], parse_config([repeated_keys]))},
{"repeated keys true", ?_assertEqual(
[],
parse_config([{repeated_keys, true}])
)},
{"repeated keys false", ?_assertEqual(
[],
parse_config([{repeated_keys, false}])
)},
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
].
handle_event_test_() ->
Data = jsx:test_cases() ++ jsx:special_test_cases(),
[
{
Title, ?_assertEqual(
true,
lists:foldl(fun handle_event/2, [], Events ++ [end_json])
)
} || {Title, _, _, Events} <- Data
].
-endif.