first step moving json project into jsx tree, compiles, but not tested
This commit is contained in:
parent
cce66ca01b
commit
4c378791f9
8 changed files with 747 additions and 14 deletions
77
src/jsx_verify.erl
Normal file
77
src/jsx_verify.erl
Normal file
|
@ -0,0 +1,77 @@
|
|||
%% 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).
|
||||
-author("alisdairsullivan@yahoo.ca").
|
||||
|
||||
-export([is_json/2]).
|
||||
|
||||
-include("./include/jsx_types.hrl").
|
||||
|
||||
|
||||
|
||||
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
|
||||
|
||||
is_json(JSON, Opts) ->
|
||||
Encoding = proplists:get_value(encoding, Opts, utf8),
|
||||
P = jsx:parser([{encoding, Encoding}]),
|
||||
case proplists:get_value(strict, Opts, true) of
|
||||
true -> collect_strict(P(JSON), Opts)
|
||||
; false -> collect(P(JSON), Opts)
|
||||
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.
|
||||
|
||||
%% make sure to ensure tail is clean
|
||||
collect({event, end_json, Next}, _Keys) ->
|
||||
case Next() of
|
||||
{incomplete, More} -> case More(end_stream) of
|
||||
ok -> true
|
||||
; _ -> false
|
||||
end
|
||||
; _ -> false
|
||||
end;
|
||||
|
||||
|
||||
%% 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}, Keys) ->
|
||||
case lists:member(Key, Keys) of
|
||||
true -> false
|
||||
; false -> collect(Next(), [Key] ++ Keys)
|
||||
end;
|
||||
|
||||
collect({event, _, Next}, Keys) ->
|
||||
collect(Next(), Keys);
|
||||
collect(_, _) ->
|
||||
false.
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue