2011-10-21 18:16:16 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
|
2011-10-21 18:16:16 -07: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_decoder).
|
|
|
|
|
2014-12-02 06:57:16 +00:00
|
|
|
%% inline handle_event, format_number and maybe_replace
|
2013-03-02 15:12:29 -08:00
|
|
|
-compile({inline, [handle_event/3]}).
|
|
|
|
-compile({inline, [format_number/1]}).
|
2013-03-05 17:39:29 -08:00
|
|
|
-compile({inline, [maybe_replace/2]}).
|
2013-03-10 18:15:55 -07:00
|
|
|
-compile({inline, [doublequote/5, singlequote/5]}).
|
2013-03-02 15:12:29 -08:00
|
|
|
|
2013-03-05 18:21:34 -08:00
|
|
|
-export([decoder/3, resume/6]).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2013-11-22 19:22:01 -08:00
|
|
|
-spec decoder(Handler::module(), State::any(), Config::list()) -> jsx:decoder().
|
2011-11-23 20:54:10 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
decoder(Handler, State, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
fun(JSON) -> start(JSON, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2013-03-05 19:40:57 -08:00
|
|
|
%% resume allows continuation from interrupted decoding without having to explicitly export
|
|
|
|
%% all states
|
|
|
|
-spec resume(
|
|
|
|
Rest::binary(),
|
|
|
|
State::atom(),
|
|
|
|
Handler::{atom(), any()},
|
|
|
|
Acc::any(),
|
|
|
|
Stack::list(atom()),
|
|
|
|
Config::jsx:config()
|
2013-11-22 19:22:01 -08:00
|
|
|
) -> jsx:decoder() | {incomplete, jsx:decoder()}.
|
2013-03-05 19:40:57 -08:00
|
|
|
|
|
|
|
resume(Rest, State, Handler, Acc, Stack, Config) ->
|
|
|
|
case State of
|
|
|
|
start -> start(Rest, Handler, Stack, Config);
|
|
|
|
value -> value(Rest, Handler, Stack, Config);
|
|
|
|
object -> object(Rest, Handler, Stack, Config);
|
|
|
|
array -> array(Rest, Handler, Stack, Config);
|
|
|
|
colon -> colon(Rest, Handler, Stack, Config);
|
|
|
|
key -> key(Rest, Handler, Stack, Config);
|
|
|
|
string -> string(Rest, Handler, Acc, Stack, Config);
|
|
|
|
integer -> integer(Rest, Handler, Acc, Stack, Config);
|
|
|
|
decimal -> decimal(Rest, Handler, Acc, Stack, Config);
|
2014-12-08 04:05:03 +00:00
|
|
|
e -> e(Rest, Handler, Acc, Stack, Config);
|
|
|
|
ex -> ex(Rest, Handler, Acc, Stack, Config);
|
2013-03-05 19:40:57 -08:00
|
|
|
exp -> exp(Rest, Handler, Acc, Stack, Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
zero -> zero(Rest, Handler, Acc, Stack, Config);
|
2013-03-05 19:40:57 -08:00
|
|
|
true -> true(Rest, Handler, Stack, Config);
|
|
|
|
false -> false(Rest, Handler, Stack, Config);
|
|
|
|
null -> null(Rest, Handler, Stack, Config);
|
|
|
|
comment -> comment(Rest, Handler, Acc, Stack, Config);
|
|
|
|
maybe_done -> maybe_done(Rest, Handler, Stack, Config);
|
|
|
|
done -> done(Rest, Handler, Stack, Config)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-include("jsx_config.hrl").
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
%% whitespace
|
|
|
|
-define(space, 16#20).
|
|
|
|
-define(tab, 16#09).
|
|
|
|
-define(cr, 16#0D).
|
|
|
|
-define(newline, 16#0A).
|
|
|
|
|
|
|
|
%% object delimiters
|
|
|
|
-define(start_object, 16#7B).
|
|
|
|
-define(end_object, 16#7D).
|
|
|
|
|
|
|
|
%% array delimiters
|
|
|
|
-define(start_array, 16#5B).
|
|
|
|
-define(end_array, 16#5D).
|
|
|
|
|
|
|
|
%% kv seperator
|
|
|
|
-define(comma, 16#2C).
|
2012-03-14 23:01:59 -07:00
|
|
|
-define(doublequote, 16#22).
|
|
|
|
-define(singlequote, 16#27).
|
2011-08-31 18:52:01 -07:00
|
|
|
-define(colon, 16#3A).
|
|
|
|
|
|
|
|
%% string escape sequences
|
|
|
|
-define(rsolidus, 16#5C).
|
|
|
|
-define(solidus, 16#2F).
|
|
|
|
|
|
|
|
%% math
|
|
|
|
-define(zero, 16#30).
|
|
|
|
-define(decimalpoint, 16#2E).
|
|
|
|
-define(negative, 16#2D).
|
|
|
|
-define(positive, 16#2B).
|
|
|
|
|
2012-03-19 14:34:07 -07:00
|
|
|
%% comments
|
|
|
|
-define(star, 16#2A).
|
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
|
|
|
|
%% some useful guards
|
|
|
|
-define(is_hex(Symbol),
|
2013-02-27 19:41:22 -08:00
|
|
|
(Symbol >= $a andalso Symbol =< $f) orelse
|
|
|
|
(Symbol >= $A andalso Symbol =< $F) orelse
|
2012-03-31 17:51:20 -07:00
|
|
|
(Symbol >= $0 andalso Symbol =< $9)
|
2011-08-31 18:52:01 -07:00
|
|
|
).
|
|
|
|
|
|
|
|
-define(is_nonzero(Symbol),
|
|
|
|
Symbol >= $1 andalso Symbol =< $9
|
|
|
|
).
|
|
|
|
|
|
|
|
|
2013-03-05 18:50:00 -08:00
|
|
|
%% error is a macro so the stack trace shows the error site when possible
|
2011-08-31 18:52:01 -07:00
|
|
|
-ifndef(error).
|
2013-03-04 17:00:34 -08:00
|
|
|
-define(error(State, Bin, Handler, Acc, Stack, Config),
|
|
|
|
case Config#config.error_handler of
|
|
|
|
false -> erlang:error(badarg);
|
2013-03-06 01:37:08 -08:00
|
|
|
F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config))
|
2013-03-04 17:00:34 -08:00
|
|
|
end
|
2013-03-03 15:23:17 -08:00
|
|
|
).
|
2013-03-04 17:00:34 -08:00
|
|
|
-define(error(State, Bin, Handler, Stack, Config),
|
|
|
|
?error(State, Bin, Handler, null, Stack, Config)
|
2011-08-31 18:52:01 -07:00
|
|
|
).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
2013-07-13 02:05:16 +00:00
|
|
|
incomplete(State, Rest, Handler, Stack, Config = #config{stream=false}) ->
|
|
|
|
?error(State, Rest, Handler, Stack, Config);
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(State, Rest, Handler, Stack, Config) ->
|
|
|
|
incomplete(State, Rest, Handler, unused, Stack, Config).
|
|
|
|
|
2013-07-13 02:05:16 +00:00
|
|
|
|
|
|
|
incomplete(State, Rest, Handler, Acc, Stack, Config = #config{stream=false}) ->
|
|
|
|
?error(State, Rest, Handler, Acc, Stack, Config);
|
|
|
|
incomplete(State, Rest, Handler, Acc, Stack, Config = #config{incomplete_handler=false}) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
{incomplete, fun(Stream) when is_binary(Stream) ->
|
|
|
|
resume(<<Rest/binary, Stream/binary>>, State, Handler, Acc, Stack, Config);
|
2014-08-13 23:00:35 -07:00
|
|
|
(End) when End == end_stream; End == end_json ->
|
2013-07-10 05:41:19 +00:00
|
|
|
case resume(<<Rest/binary, ?space/utf8>>, State, Handler, Acc, Stack, Config#config{stream=false}) of
|
2013-03-05 18:50:00 -08:00
|
|
|
{incomplete, _} -> ?error(State, Rest, Handler, Acc, Stack, Config);
|
|
|
|
Else -> Else
|
2013-02-25 17:24:06 -08:00
|
|
|
end
|
2013-03-05 18:50:00 -08:00
|
|
|
end
|
|
|
|
};
|
2013-07-13 02:05:16 +00:00
|
|
|
incomplete(State, Rest, Handler, Acc, Stack, Config = #config{incomplete_handler=F}) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config)).
|
2011-08-31 18:52:01 -07:00
|
|
|
|
|
|
|
|
2014-02-10 04:06:13 +00:00
|
|
|
handle_event(Event, {Handler, State}, _Config) -> {Handler, Handler:handle_event(Event, State)}.
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-03-04 17:36:19 -08:00
|
|
|
start(<<16#ef, 16#bb, 16#bf, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
|
|
|
start(<<16#ef, 16#bb>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(start, <<16#ef, 16#bb>>, Handler, Stack, Config);
|
2013-03-04 17:36:19 -08:00
|
|
|
start(<<16#ef>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(start, <<16#ef>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
start(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(start, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
start(Bin, Handler, Stack, Config) ->
|
|
|
|
value(Bin, Handler, Stack, Config).
|
|
|
|
|
|
|
|
|
|
|
|
value(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
value(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
|
|
|
value(<<?start_object, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
object(Rest, handle_event(start_object, Handler, Config), [key|Stack], Config);
|
|
|
|
value(<<?start_array, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
array(Rest, handle_event(start_array, Handler, Config), [array|Stack], Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<$t, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-25 21:38:07 -08:00
|
|
|
true(Rest, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<$f, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-25 21:38:07 -08:00
|
|
|
false(Rest, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<$n, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-25 21:38:07 -08:00
|
|
|
null(Rest, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<?negative, Rest/binary>>, Handler, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
negative(Rest, Handler, [$-], Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<?zero, Rest/binary>>, Handler, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
zero(Rest, Handler, [$0], Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
value(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
|
|
|
value(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
|
|
|
value(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
|
|
|
value(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
|
|
|
|
string(Rest, Handler, [singlequote|Stack], Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<S, Rest/binary>>, Handler, Stack, Config) when ?is_nonzero(S) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
integer(Rest, Handler, [S], Stack, Config);
|
2014-08-19 17:47:44 -07:00
|
|
|
value(<<?end_array, _/binary>> = Rest, Handler, Stack, Config=#config{strict_commas=false}) ->
|
|
|
|
maybe_done(Rest, Handler, Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
value(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(value, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
value(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, value, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
value(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, value, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
value(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(value, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(value, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(value, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
object(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
object(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
object(Rest, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
object(<<?end_object, Rest/binary>>, Handler, [key|Stack], Config) ->
|
|
|
|
maybe_done(Rest, handle_event(end_object, Handler, Config), Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
object(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
object(Rest, Handler, Stack, Config);
|
|
|
|
object(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
object(Rest, Handler, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
object(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
object(Rest, Handler, Stack, Config);
|
|
|
|
object(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
|
|
|
|
string(Rest, Handler, [singlequote|Stack], Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
object(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(object, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
object(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, object, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
object(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, object, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
object(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(object, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
object(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(object, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
object(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(object, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
array(<<?end_array, Rest/binary>>, Handler, [array|Stack], Config) ->
|
|
|
|
maybe_done(Rest, handle_event(end_array, Handler, Config), Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
array(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
array(Rest, Handler, Stack, Config);
|
|
|
|
array(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
array(Rest, Handler, Stack, Config);
|
|
|
|
array(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
array(Rest, Handler, Stack, Config);
|
|
|
|
array(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
array(Rest, Handler, Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
array(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
value(<<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
array(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, array, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
array(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, array, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
array(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(array, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
array(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(array, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
array(Bin, Handler, Stack, Config) ->
|
|
|
|
value(Bin, Handler, Stack, Config).
|
|
|
|
|
|
|
|
|
|
|
|
colon(<<?colon, Rest/binary>>, Handler, [key|Stack], Config) ->
|
|
|
|
value(Rest, Handler, [object|Stack], Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
colon(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
colon(Rest, Handler, Stack, Config);
|
|
|
|
colon(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
colon(Rest, Handler, Stack, Config);
|
|
|
|
colon(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
colon(Rest, Handler, Stack, Config);
|
|
|
|
colon(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
colon(Rest, Handler, Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
colon(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(colon, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
colon(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, colon, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
colon(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, colon, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
colon(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(colon, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
colon(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(colon, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
colon(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(colon, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
key(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
key(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
key(Rest, Handler, Stack, Config);
|
2014-08-19 17:47:44 -07:00
|
|
|
key(<<?end_object, Rest/binary>>, Handler, [key|Stack], Config=#config{strict_commas=false}) ->
|
|
|
|
maybe_done(<<?end_object, Rest/binary>>, Handler, [object|Stack], Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
key(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
key(Rest, Handler, Stack, Config);
|
|
|
|
key(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
key(Rest, Handler, Stack, Config);
|
|
|
|
key(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
key(Rest, Handler, Stack, Config);
|
|
|
|
key(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
|
|
|
|
string(Rest, Handler, [singlequote|Stack], Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
key(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(key, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
key(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, key, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
key(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, key, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
key(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(key, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
key(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(key, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
key(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(key, Bin, Handler, Stack, Config).
|
2011-09-19 23:06:37 -07:00
|
|
|
|
|
|
|
|
2013-03-05 19:40:57 -08:00
|
|
|
%% explicitly whitelist ascii set for faster parsing. really? really. someone should
|
|
|
|
%% submit a patch that unrolls simple guards
|
|
|
|
%% note that if you encounter an error from string and you can't find the clause that
|
|
|
|
%% caused it here, it might be in unescape below
|
2014-12-02 06:57:16 +00:00
|
|
|
|
|
|
|
string(Bin, Handler, Stack, Config) ->
|
|
|
|
string(Bin, Handler, [], Stack, Config).
|
|
|
|
|
|
|
|
|
2013-08-22 07:07:26 +00:00
|
|
|
string(<<?doublequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
|
|
|
doublequote(Rest, Handler, Acc, Stack, Config);
|
|
|
|
string(<<?singlequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
|
|
|
singlequote(Rest, Handler, Acc, Stack, Config);
|
|
|
|
string(<<?solidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace(?solidus, Config)], Stack, Config);
|
2013-08-22 07:07:26 +00:00
|
|
|
string(<<?rsolidus/utf8, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
|
|
|
unescape(Rest, Handler, Acc, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
string(<<X/utf8, Rest/binary>>, Handler, Acc, Stack, Config=#config{uescape=true}) ->
|
|
|
|
case X of
|
|
|
|
X when X < 16#80 -> string(Rest, Handler, [Acc, X], Stack, Config);
|
|
|
|
X -> string(Rest, Handler, [Acc, json_escape_sequence(X)], Stack, Config)
|
|
|
|
end;
|
|
|
|
%% u+2028
|
|
|
|
string(<<226, 128, 168, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
|
|
|
string(Rest, Handler, [Acc, maybe_replace(16#2028, Config)], Stack, Config);
|
|
|
|
%% u+2029
|
|
|
|
string(<<226, 128, 169, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
|
|
|
string(Rest, Handler, [Acc, maybe_replace(16#2029, Config)], Stack, Config);
|
2014-12-02 06:57:16 +00:00
|
|
|
string(<<_/utf8, _/binary>> = Bin, Handler, Acc, Stack, Config) ->
|
|
|
|
Size = count(Bin, 0, Config),
|
|
|
|
<<Clean:Size/binary, Rest/binary>> = Bin,
|
|
|
|
string(Rest, Handler, [Acc, Clean], Stack, Config);
|
|
|
|
%% really, really dirty strings. if there's no valid utf8 we never reach `count`
|
|
|
|
%% and things get replaced instead of ignored
|
|
|
|
string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
|
|
|
|
string(Rest, Handler, [Acc, X], Stack, Config);
|
2014-12-01 09:20:17 +00:00
|
|
|
%% u+fffe and u+ffff for R14BXX (subsequent runtimes will happily match preceeding
|
|
|
|
string(<<239, 191, 190, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#fffe/utf8>>], Stack, Config);
|
2014-12-01 09:20:17 +00:00
|
|
|
string(<<239, 191, 191, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#ffff/utf8>>], Stack, Config);
|
2014-12-01 09:20:17 +00:00
|
|
|
%% partial utf8 codepoints
|
2013-08-27 05:02:37 +00:00
|
|
|
string(<<>>, Handler, Acc, Stack, Config) ->
|
|
|
|
incomplete(string, <<>>, Handler, Acc, Stack, Config);
|
2014-12-01 09:20:17 +00:00
|
|
|
string(<<X>>, Handler, Acc, Stack, Config) when X >= 2#11000000 ->
|
2013-08-27 05:02:37 +00:00
|
|
|
incomplete(string, <<X>>, Handler, Acc, Stack, Config);
|
2014-12-01 09:20:17 +00:00
|
|
|
string(<<X, Y>>, Handler, Acc, Stack, Config) when X >= 2#11100000, Y >= 2#10000000 ->
|
2013-08-27 05:02:37 +00:00
|
|
|
incomplete(string, <<X, Y>>, Handler, Acc, Stack, Config);
|
|
|
|
string(<<X, Y, Z>>, Handler, Acc, Stack, Config)
|
2014-12-01 09:20:17 +00:00
|
|
|
when X >= 2#11100000, Y >= 2#10000000, Z >= 2#10000000 ->
|
2013-10-20 03:16:56 +00:00
|
|
|
incomplete(string, <<X, Y, Z>>, Handler, Acc, Stack, Config);
|
2013-02-25 17:24:06 -08:00
|
|
|
%% surrogates
|
2013-10-20 03:16:56 +00:00
|
|
|
string(<<237, X, _, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
|
2013-02-25 17:24:06 -08:00
|
|
|
when X >= 160 ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
|
2013-02-25 17:24:06 -08:00
|
|
|
%% overlong encodings and missing continuations of a 2 byte sequence
|
2013-10-20 03:16:56 +00:00
|
|
|
string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
|
2013-02-25 17:24:06 -08:00
|
|
|
when X >= 192, X =< 223 ->
|
|
|
|
strip_continuations(Rest, Handler, Acc, Stack, Config, 1);
|
|
|
|
%% overlong encodings and missing continuations of a 3 byte sequence
|
2013-10-20 03:16:56 +00:00
|
|
|
string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
|
2013-02-25 17:24:06 -08:00
|
|
|
when X >= 224, X =< 239 ->
|
|
|
|
strip_continuations(Rest, Handler, Acc, Stack, Config, 2);
|
|
|
|
%% overlong encodings and missing continuations of a 4 byte sequence
|
2013-10-20 03:16:56 +00:00
|
|
|
string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
|
2013-02-25 17:24:06 -08:00
|
|
|
when X >= 240, X =< 247 ->
|
|
|
|
strip_continuations(Rest, Handler, Acc, Stack, Config, 3);
|
|
|
|
%% incompletes and unexpected bytes, including orphan continuations
|
2013-10-20 03:16:56 +00:00
|
|
|
string(<<_, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false}) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
|
2013-10-20 03:16:56 +00:00
|
|
|
string(Bin, Handler, Acc, Stack, Config) -> ?error(string, Bin, Handler, Acc, Stack, Config).
|
2013-02-25 17:24:06 -08:00
|
|
|
|
2013-03-10 17:34:50 -07:00
|
|
|
|
2014-12-03 23:09:21 -08:00
|
|
|
count(<<0, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<1, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<2, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<3, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<4, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<5, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<6, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<7, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<8, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<9, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<10, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<11, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<12, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<13, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<14, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<15, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<16, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<17, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<18, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<19, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<20, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<21, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<22, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<23, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<24, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<25, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<26, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<27, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<28, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<29, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<30, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<31, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
2014-12-02 06:57:16 +00:00
|
|
|
count(<<32, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<33, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<?doublequote, _/binary>>, N, _) -> N;
|
|
|
|
count(<<35, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<36, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<37, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<38, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<?singlequote, _/binary>>, N, _) -> N;
|
|
|
|
count(<<40, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<41, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<42, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<43, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<44, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<45, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<46, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<?solidus, _/binary>>, N, _) -> N;
|
|
|
|
count(<<48, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<49, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<50, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<51, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<52, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<53, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<54, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<55, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<56, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<57, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<58, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<59, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<60, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<61, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<62, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<63, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<64, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<65, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<66, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<67, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<68, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<69, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<70, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<71, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<72, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<73, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<74, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<75, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<76, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<77, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<78, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<79, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<80, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<81, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<82, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<83, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<84, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<85, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<86, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<87, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<88, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<89, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<90, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<91, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<?rsolidus, _/binary>>, N, _) -> N;
|
|
|
|
count(<<93, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<94, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<95, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<96, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<97, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<98, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<99, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<100, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<101, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<102, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<103, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<104, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<105, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<106, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<107, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<108, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<109, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<110, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<111, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<112, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<113, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<114, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<115, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<116, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<117, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<118, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<119, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<120, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<121, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<122, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<123, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<124, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<125, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<126, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<127, Rest/binary>>, N, Config) ->
|
|
|
|
count(Rest, N + 1, Config);
|
|
|
|
count(<<_, Rest/binary>>, N, Config=#config{dirty_strings=true}) ->
|
|
|
|
count(Rest, N + 1, Config);
|
2014-12-03 23:02:02 -08:00
|
|
|
count(<<_/utf8, _/binary>>, N, #config{uescape=true}) -> N;
|
2014-12-07 16:31:29 -08:00
|
|
|
%% u+2028
|
|
|
|
count(<<226, 128, 168, _/binary>>, N, _) -> N;
|
|
|
|
%% u+2029
|
|
|
|
count(<<226, 128, 169, _/binary>>, N, _) -> N;
|
2014-12-07 19:12:43 -08:00
|
|
|
count(<<X/utf8, Rest/binary>>, N, Config) ->
|
|
|
|
case X of
|
|
|
|
X when X < 16#800 -> count(Rest, N + 2, Config);
|
|
|
|
X when X < 16#10000 -> count(Rest, N + 3, Config);
|
|
|
|
_ -> count(Rest, N + 4, Config)
|
|
|
|
end;
|
2014-12-02 06:57:16 +00:00
|
|
|
count(_, N, _) -> N.
|
|
|
|
|
|
|
|
|
2013-10-20 03:23:38 +00:00
|
|
|
doublequote(Rest, Handler, Acc, [key|_] = Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
colon(Rest, handle_event({key, iolist_to_binary(Acc)}, Handler, Config), Stack, Config);
|
2013-10-20 03:23:38 +00:00
|
|
|
doublequote(Rest, Handler, Acc, [singlequote|_] = Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace(?doublequote, Config)], Stack, Config);
|
2013-03-10 19:58:13 -07:00
|
|
|
doublequote(<<>>, Handler, Acc, [singlequote|_] = Stack, Config) ->
|
2013-03-10 17:34:50 -07:00
|
|
|
incomplete(string, <<?doublequote>>, Handler, Acc, Stack, Config);
|
2013-10-20 03:23:38 +00:00
|
|
|
doublequote(Rest, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
maybe_done(Rest, handle_event({string, iolist_to_binary(Acc)}, Handler, Config), Stack, Config).
|
2013-03-10 17:34:50 -07:00
|
|
|
|
2013-03-10 19:49:46 -07:00
|
|
|
|
2013-10-20 03:23:38 +00:00
|
|
|
singlequote(Rest, Handler, Acc, [singlequote, key|Stack], Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
colon(Rest, handle_event({key, iolist_to_binary(Acc)}, Handler, Config), [key|Stack], Config);
|
2013-10-20 03:23:38 +00:00
|
|
|
singlequote(Rest, Handler, Acc, [singlequote|Stack], Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
maybe_done(Rest, handle_event({string, iolist_to_binary(Acc)}, Handler, Config), Stack, Config);
|
2013-10-20 03:23:38 +00:00
|
|
|
singlequote(Rest, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, ?singlequote], Stack, Config).
|
2013-03-10 17:34:50 -07:00
|
|
|
|
|
|
|
|
2013-02-24 01:07:16 -08:00
|
|
|
%% strips continuation bytes after bad utf bytes, guards against both too short
|
|
|
|
%% and overlong sequences. N is the maximum number of bytes to strip
|
2013-03-10 18:05:18 -07:00
|
|
|
strip_continuations(<<Rest/binary>>, Handler, Acc, Stack, Config, 0) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
|
2013-02-25 17:24:06 -08:00
|
|
|
strip_continuations(<<X, Rest/binary>>, Handler, Acc, Stack, Config, N) when X >= 128, X =< 191 ->
|
|
|
|
strip_continuations(Rest, Handler, Acc, Stack, Config, N - 1);
|
2013-03-05 19:40:57 -08:00
|
|
|
%% if end of input is reached before stripping the max number of continuations
|
|
|
|
%% possible magic numbers are reinserted into the stream that get us back to
|
|
|
|
%% the same state without complicated machinery
|
2013-02-25 17:24:06 -08:00
|
|
|
strip_continuations(<<>>, Handler, Acc, Stack, Config, N) ->
|
|
|
|
case N of
|
2013-03-05 18:50:00 -08:00
|
|
|
1 -> incomplete(string, <<192>>, Handler, Acc, Stack, Config);
|
|
|
|
2 -> incomplete(string, <<224>>, Handler, Acc, Stack, Config);
|
|
|
|
3 -> incomplete(string, <<240>>, Handler, Acc, Stack, Config)
|
2013-02-25 17:24:06 -08:00
|
|
|
end;
|
2013-03-05 19:40:57 -08:00
|
|
|
%% not a continuation byte, insert a replacement character for sequence thus
|
|
|
|
%% far and dispatch back to string
|
2013-03-10 18:05:18 -07:00
|
|
|
strip_continuations(<<Rest/binary>>, Handler, Acc, Stack, Config, _) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config).
|
2013-02-25 17:24:06 -08:00
|
|
|
|
|
|
|
|
2013-03-05 19:40:57 -08:00
|
|
|
%% this all gets really gross and should probably eventually be folded into
|
|
|
|
%% but for now it fakes being part of string on incompletes and errors
|
2013-10-20 21:16:03 +00:00
|
|
|
unescape(<<?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(<<?rsolidus, Rest/binary>>, Handler, [Acc, ?rsolidus], Stack, Config);
|
2013-03-10 19:49:06 -07:00
|
|
|
unescape(<<C, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, ?rsolidus, C], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<$b, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\b, Config)], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<$f, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\f, Config)], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<$n, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\n, Config)], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<$r, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\r, Config)], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<$t, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\t, Config)], Stack, Config);
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(<<?doublequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\", Config)], Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
unescape(<<?singlequote, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_single_quotes=false}) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, ?singlequote], Stack, Config);
|
2013-03-06 02:22:16 -08:00
|
|
|
unescape(<<?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($\\, Config)], Stack, Config);
|
2013-03-06 02:22:16 -08:00
|
|
|
unescape(<<?solidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace($/, Config)], Stack, Config);
|
2014-12-07 15:30:37 -08:00
|
|
|
unescape(<<$u, F, A, B, C, ?rsolidus, $u, G, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config)
|
|
|
|
when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
|
|
|
|
(X == $c orelse X == $d orelse X == $e orelse X == $f orelse X == $C orelse X == $D orelse X == $E orelse X == $F),
|
|
|
|
(F == $d orelse F == $D),
|
|
|
|
(G == $d orelse G == $D),
|
|
|
|
?is_hex(B), ?is_hex(C), ?is_hex(Y), ?is_hex(Z)
|
2013-02-27 19:41:22 -08:00
|
|
|
->
|
|
|
|
High = erlang:list_to_integer([$d, A, B, C], 16),
|
|
|
|
Low = erlang:list_to_integer([$d, X, Y, Z], 16),
|
|
|
|
Codepoint = (High - 16#d800) * 16#400 + (Low - 16#dc00) + 16#10000,
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, <<Codepoint/utf8>>], Stack, Config);
|
2014-12-07 15:30:37 -08:00
|
|
|
unescape(<<$u, F, A, B, C, ?rsolidus, $u, W, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config)
|
|
|
|
when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
|
|
|
|
(F == $d orelse F == $D),
|
|
|
|
?is_hex(B), ?is_hex(C), ?is_hex(W), ?is_hex(X), ?is_hex(Y), ?is_hex(Z)
|
2013-02-27 19:41:22 -08:00
|
|
|
->
|
2013-10-20 03:16:56 +00:00
|
|
|
case Config#config.strict_utf8 of
|
|
|
|
true -> ?error(<<$u, $d, A, B, C, ?rsolidus, $u, W, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config);
|
2014-12-02 06:57:16 +00:00
|
|
|
false -> string(Rest, Handler, [Acc, <<16#fffd/utf8>>, <<16#fffd/utf8>>], Stack, Config)
|
2013-03-04 14:40:07 -08:00
|
|
|
end;
|
2014-12-07 15:30:37 -08:00
|
|
|
unescape(<<$u, F, A, B, C, ?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config)
|
|
|
|
when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
|
|
|
|
(F == $d orelse F == $D),
|
|
|
|
?is_hex(B), ?is_hex(C)
|
2013-02-27 19:41:22 -08:00
|
|
|
->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(string, <<?rsolidus, $u, $d, A, B, C, ?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config);
|
2014-12-07 15:30:37 -08:00
|
|
|
unescape(<<$u, F, A, B, C>>, Handler, Acc, Stack, Config)
|
|
|
|
when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
|
|
|
|
(F == $d orelse F == $D),
|
|
|
|
?is_hex(B), ?is_hex(C)
|
2013-02-27 19:41:22 -08:00
|
|
|
->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(string, <<?rsolidus, $u, $d, A, B, C>>, Handler, Acc, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
unescape(<<$u, A, B, C, D, Rest/binary>>, Handler, Acc, Stack, Config)
|
2013-02-24 01:07:16 -08:00
|
|
|
when ?is_hex(A), ?is_hex(B), ?is_hex(C), ?is_hex(D) ->
|
|
|
|
case erlang:list_to_integer([A, B, C, D], 16) of
|
2013-02-27 19:41:22 -08:00
|
|
|
Codepoint when Codepoint < 16#d800; Codepoint > 16#dfff ->
|
2014-12-02 06:57:16 +00:00
|
|
|
string(Rest, Handler, [Acc, maybe_replace(Codepoint, Config)], Stack, Config);
|
2013-10-20 03:16:56 +00:00
|
|
|
_ when Config#config.strict_utf8 ->
|
|
|
|
?error(string, <<?rsolidus, $u, A, B, C, D, Rest/binary>>, Handler, Acc, Stack, Config);
|
2014-12-02 06:57:16 +00:00
|
|
|
_ -> string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config)
|
2013-02-24 01:07:16 -08:00
|
|
|
end;
|
2013-02-27 19:41:22 -08:00
|
|
|
unescape(Bin, Handler, Acc, Stack, Config) ->
|
2013-02-24 01:07:16 -08:00
|
|
|
case is_partial_escape(Bin) of
|
2013-03-05 18:50:00 -08:00
|
|
|
true -> incomplete(string, <<?rsolidus/utf8, Bin/binary>>, Handler, Acc, Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
false -> case Config#config.strict_escapes of
|
|
|
|
true -> ?error(string, <<?rsolidus, Bin/binary>>, Handler, Acc, Stack, Config);
|
2014-12-02 06:57:16 +00:00
|
|
|
false -> string(Bin, Handler, [Acc, ?rsolidus], Stack, Config)
|
2013-10-20 03:28:22 +00:00
|
|
|
end
|
2012-03-27 14:44:02 -07:00
|
|
|
end.
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2012-03-27 14:44:02 -07:00
|
|
|
|
2013-02-27 19:41:22 -08:00
|
|
|
is_partial_escape(<<$u, A, B, C>>) when ?is_hex(A), ?is_hex(B), ?is_hex(C) -> true;
|
|
|
|
is_partial_escape(<<$u, A, B>>) when ?is_hex(A), ?is_hex(B) -> true;
|
|
|
|
is_partial_escape(<<$u, A>>) when ?is_hex(A) -> true;
|
2013-02-25 17:24:06 -08:00
|
|
|
is_partial_escape(<<$u>>) -> true;
|
2013-02-24 01:07:16 -08:00
|
|
|
is_partial_escape(<<>>) -> true;
|
|
|
|
is_partial_escape(_) -> false.
|
|
|
|
|
|
|
|
|
2013-03-10 19:49:06 -07:00
|
|
|
maybe_replace(C, #config{dirty_strings=true}) -> C;
|
2013-02-24 01:07:16 -08:00
|
|
|
maybe_replace($\b, #config{escaped_strings=true}) -> [$\\, $b];
|
|
|
|
maybe_replace($\t, #config{escaped_strings=true}) -> [$\\, $t];
|
|
|
|
maybe_replace($\n, #config{escaped_strings=true}) -> [$\\, $n];
|
|
|
|
maybe_replace($\f, #config{escaped_strings=true}) -> [$\\, $f];
|
|
|
|
maybe_replace($\r, #config{escaped_strings=true}) -> [$\\, $r];
|
|
|
|
maybe_replace($\", #config{escaped_strings=true}) -> [$\\, $\"];
|
|
|
|
maybe_replace($/, Config=#config{escaped_strings=true}) ->
|
|
|
|
case Config#config.escaped_forward_slashes of
|
|
|
|
true -> [$\\, $/]
|
2013-03-10 19:49:06 -07:00
|
|
|
; false -> $/
|
2013-02-24 01:07:16 -08:00
|
|
|
end;
|
|
|
|
maybe_replace($\\, #config{escaped_strings=true}) -> [$\\, $\\];
|
|
|
|
maybe_replace(X, Config=#config{escaped_strings=true}) when X == 16#2028; X == 16#2029 ->
|
|
|
|
case Config#config.unescaped_jsonp of
|
2014-12-02 06:57:16 +00:00
|
|
|
true -> <<X/utf8>>
|
2013-03-06 01:37:08 -08:00
|
|
|
; false -> json_escape_sequence(X)
|
2013-02-24 01:07:16 -08:00
|
|
|
end;
|
2014-12-02 06:57:16 +00:00
|
|
|
maybe_replace(X, #config{escaped_strings=true}) when X < 32 ->
|
|
|
|
json_escape_sequence(X);
|
|
|
|
maybe_replace(X, _Config) -> <<X/utf8>>.
|
2012-03-31 20:38:51 -07:00
|
|
|
|
|
|
|
|
2013-03-06 01:37:08 -08:00
|
|
|
%% convert a codepoint to it's \uXXXX equiv.
|
2014-12-02 06:57:16 +00:00
|
|
|
json_escape_sequence(X) when X < 65536 ->
|
2013-03-06 01:37:08 -08:00
|
|
|
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
2014-12-03 23:02:02 -08:00
|
|
|
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))];
|
|
|
|
json_escape_sequence(X) ->
|
|
|
|
Adjusted = X - 16#10000,
|
|
|
|
<<A:10, B:10>> = <<Adjusted:20>>,
|
|
|
|
json_escape_sequence(A + 16#d800) ++ json_escape_sequence(B + 16#dc00).
|
2013-03-06 01:37:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
%% ascii "1" is [49], "2" is [50], etc...
|
|
|
|
to_hex(10) -> $a;
|
|
|
|
to_hex(11) -> $b;
|
|
|
|
to_hex(12) -> $c;
|
|
|
|
to_hex(13) -> $d;
|
|
|
|
to_hex(14) -> $e;
|
|
|
|
to_hex(15) -> $f;
|
|
|
|
to_hex(X) -> X + 48.
|
|
|
|
|
|
|
|
|
2013-03-05 19:40:57 -08:00
|
|
|
%% like in strings, there's some pseudo states in here that will never
|
2014-12-08 04:05:03 +00:00
|
|
|
%% show up in incompletes. some show up in value, some show up in
|
|
|
|
%% integer, decimal or exp
|
2013-03-02 14:50:40 -08:00
|
|
|
negative(<<$0, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
zero(Rest, Handler, [Acc, $0], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
negative(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
integer(Rest, Handler, [Acc, S], Stack, Config);
|
2013-03-03 15:23:17 -08:00
|
|
|
negative(<<>>, Handler, [?negative], Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(value, <<?negative>>, Handler, Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
negative(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
?error(negative, Bin, Handler, Acc, Stack, Config).
|
2013-03-02 14:50:40 -08:00
|
|
|
|
|
|
|
|
|
|
|
zero(<<?decimalpoint, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
decimal(Rest, Handler, [Acc, ?decimalpoint], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
zero(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= $e; S =:= $E ->
|
2014-12-08 04:05:03 +00:00
|
|
|
e(Rest, Handler, [Acc, ".0e"], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
zero(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
finish_number(Bin, Handler, {zero, lists:flatten(Acc)}, Stack, Config).
|
2013-03-02 14:50:40 -08:00
|
|
|
|
|
|
|
|
|
|
|
integer(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
integer(Rest, Handler, [Acc, S], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
integer(<<?decimalpoint, Rest/binary>>, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
initialdecimal(Rest, Handler, [Acc, ?decimalpoint], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
integer(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= $e; S =:= $E ->
|
2014-12-08 04:05:03 +00:00
|
|
|
e(Rest, Handler, [Acc, ".0e"], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
integer(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
finish_number(Bin, Handler, {integer, lists:flatten(Acc)}, Stack, Config).
|
2013-03-02 14:50:40 -08:00
|
|
|
|
|
|
|
|
2013-03-10 19:02:20 -07:00
|
|
|
initialdecimal(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
decimal(Rest, Handler, [Acc, S], Stack, Config);
|
|
|
|
initialdecimal(<<>>, Handler, Acc, Stack, Config) ->
|
|
|
|
[?decimalpoint|Rest] = lists:reverse(Acc),
|
|
|
|
incomplete(integer, <<?decimalpoint>>, Handler, lists:reverse(Rest), Stack, Config);
|
2013-03-10 19:02:20 -07:00
|
|
|
initialdecimal(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
?error(initialdecimal, Bin, Handler, Acc, Stack, Config).
|
2013-03-10 19:02:20 -07:00
|
|
|
|
|
|
|
|
2013-03-10 18:05:18 -07:00
|
|
|
decimal(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
decimal(Rest, Handler, [Acc, S], Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
decimal(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= $e; S =:= $E ->
|
2014-12-08 04:05:03 +00:00
|
|
|
e(Rest, Handler, [Acc, $e], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
decimal(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
finish_number(Bin, Handler, {decimal, lists:flatten(Acc)}, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-03-02 14:50:40 -08:00
|
|
|
e(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
exp(Rest, Handler, [Acc, S], Stack, Config);
|
2013-03-04 17:00:34 -08:00
|
|
|
e(<<Sign, Rest/binary>>, Handler, Acc, Stack, Config) when Sign =:= ?positive; Sign =:= ?negative ->
|
2014-12-08 04:05:03 +00:00
|
|
|
ex(Rest, Handler, [Acc, Sign], Stack, Config);
|
|
|
|
e(<<>>, Handler, Acc, Stack, Config) ->
|
|
|
|
incomplete(e, <<>>, Handler, Acc, Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
e(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
?error(e, Bin, Handler, Acc, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-03-02 14:50:40 -08:00
|
|
|
ex(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
exp(Rest, Handler, [Acc, S], Stack, Config);
|
|
|
|
ex(<<>>, Handler, Acc, Stack, Config) ->
|
|
|
|
incomplete(ex, <<>>, Handler, Acc, Stack, Config);
|
|
|
|
ex(Bin, Handler, Acc, Stack, Config) ->
|
|
|
|
?error(ex, Bin, Handler, Acc, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-03-02 14:50:40 -08:00
|
|
|
exp(<<S, Rest/binary>>, Handler, Acc, Stack, Config) when S =:= ?zero; ?is_nonzero(S) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
exp(Rest, Handler, [Acc, S], Stack, Config);
|
2013-03-02 14:50:40 -08:00
|
|
|
exp(Bin, Handler, Acc, Stack, Config) ->
|
2014-12-08 04:05:03 +00:00
|
|
|
finish_number(Bin, Handler, {exp, lists:flatten(Acc)}, Stack, Config).
|
2014-12-02 06:57:16 +00:00
|
|
|
|
|
|
|
|
2013-07-10 05:41:19 +00:00
|
|
|
finish_number(Rest, Handler, Acc, [], Config=#config{stream=false}) ->
|
2013-03-02 14:50:40 -08:00
|
|
|
maybe_done(Rest, handle_event(format_number(Acc), Handler, Config), [], Config);
|
|
|
|
finish_number(<<>>, Handler, {NumType, Acc}, Stack, Config) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
incomplete(NumType, <<>>, Handler, Acc, Stack, Config);
|
2013-10-21 05:46:53 +00:00
|
|
|
finish_number(Rest, Handler, Acc, Stack, Config) ->
|
|
|
|
maybe_done(Rest, handle_event(format_number(Acc), Handler, Config), Stack, Config).
|
|
|
|
|
2011-08-31 18:52:01 -07:00
|
|
|
|
2014-12-08 04:05:03 +00:00
|
|
|
format_number({zero, Acc}) -> {integer, list_to_integer(Acc)};
|
|
|
|
format_number({integer, Acc}) -> {integer, list_to_integer(Acc)};
|
|
|
|
format_number({decimal, Acc}) -> {float, list_to_float(Acc)};
|
|
|
|
format_number({exp, Acc}) -> {float, list_to_float(Acc)}.
|
2011-08-31 18:52:01 -07:00
|
|
|
|
|
|
|
|
2013-02-25 21:38:07 -08:00
|
|
|
true(<<$r, $u, $e, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(Rest, handle_event({literal, true}, Handler, Config), Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
true(<<$r, $u>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(true, <<$r, $u>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
true(<<$r>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(true, <<$r>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
true(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(true, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
true(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(true, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-02-25 21:38:07 -08:00
|
|
|
false(<<$a, $l, $s, $e, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(Rest, handle_event({literal, false}, Handler, Config), Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
false(<<$a, $l, $s>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(false, <<$a, $l, $s>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
false(<<$a, $l>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(false, <<$a, $l>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
false(<<$a>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(false, <<$a>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
false(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(false, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
false(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(false, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-02-25 21:38:07 -08:00
|
|
|
null(<<$u, $l, $l, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(Rest, handle_event({literal, null}, Handler, Config), Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
null(<<$u, $l>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(null, <<$u, $l>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
null(<<$u>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(null, <<$u>>, Handler, Stack, Config);
|
2013-03-02 15:48:26 -08:00
|
|
|
null(<<>>, Handler, Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(null, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
null(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(null, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(<<?newline, Rest/binary>>, Handler, Resume, [comment|Stack], Config) ->
|
|
|
|
resume(Rest, Resume, Handler, unused, Stack, Config);
|
|
|
|
comment(<<?solidus, ?star, Rest/binary>>, Handler, Resume, Stack, Config) ->
|
|
|
|
comment(Rest, Handler, Resume, [multicomment|Stack], Config);
|
|
|
|
comment(<<?solidus>>, Handler, Resume, [multicomment|_] = Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(comment, <<?solidus>>, Handler, Resume, Stack, Config);
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(<<?star, ?solidus, Rest/binary>>, Handler, Resume, [multicomment|Stack], Config) ->
|
|
|
|
case Stack of
|
|
|
|
[multicomment|_] -> comment(Rest, Handler, Resume, Stack, Config);
|
|
|
|
_ -> resume(Rest, Resume, Handler, unused, Stack, Config)
|
|
|
|
end;
|
|
|
|
comment(<<?star>>, Handler, Resume, [multicomment|_] = Stack, Config) ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(comment, <<?star>>, Handler, Resume, Stack, Config);
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(<<_/utf8, Rest/binary>>, Handler, Resume, Stack, Config) ->
|
|
|
|
comment(Rest, Handler, Resume, Stack, Config);
|
2013-10-20 03:16:56 +00:00
|
|
|
comment(<<_, Rest/binary>>, Handler, Resume, Stack, Config=#config{strict_utf8=false}) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, Resume, Stack, Config);
|
2013-07-10 05:41:19 +00:00
|
|
|
comment(<<>>, Handler, done, [Comment], Config=#config{stream=false})
|
2013-03-05 19:19:16 -08:00
|
|
|
when Comment == comment; Comment == multicomment ->
|
|
|
|
resume(<<>>, done, Handler, unused, [], Config);
|
|
|
|
comment(<<>>, Handler, Resume, Stack, Config) ->
|
|
|
|
incomplete(comment, <<>>, Handler, Resume, Stack, Config);
|
|
|
|
comment(Bin, Handler, Resume, Stack, Config) ->
|
|
|
|
?error(comment, Bin, Handler, Resume, Stack, Config).
|
2012-04-01 17:16:22 -07:00
|
|
|
|
|
|
|
|
2013-03-10 18:05:18 -07:00
|
|
|
maybe_done(<<Rest/binary>>, Handler, [], Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
done(Rest, handle_event(end_json, Handler, Config), [], Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
maybe_done(<<?space, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
maybe_done(Rest, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(<<?end_object, Rest/binary>>, Handler, [object|Stack], Config) ->
|
|
|
|
maybe_done(Rest, handle_event(end_object, Handler, Config), Stack, Config);
|
|
|
|
maybe_done(<<?end_array, Rest/binary>>, Handler, [array|Stack], Config) ->
|
|
|
|
maybe_done(Rest, handle_event(end_array, Handler, Config), Stack, Config);
|
|
|
|
maybe_done(<<?comma, Rest/binary>>, Handler, [object|Stack], Config) ->
|
|
|
|
key(Rest, Handler, [key|Stack], Config);
|
|
|
|
maybe_done(<<?comma, Rest/binary>>, Handler, [array|_] = Stack, Config) ->
|
|
|
|
value(Rest, Handler, Stack, Config);
|
2014-12-07 16:31:29 -08:00
|
|
|
maybe_done(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
maybe_done(Rest, Handler, Stack, Config);
|
|
|
|
maybe_done(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
|
|
|
|
maybe_done(Rest, Handler, Stack, Config);
|
|
|
|
maybe_done(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(Rest, Handler, Stack, Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
maybe_done(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(maybe_done, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
maybe_done(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, maybe_done, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
maybe_done(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, maybe_done, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
maybe_done(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(maybe_done, <<?solidus>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(<<>>, Handler, Stack, Config) when length(Stack) > 0 ->
|
2013-03-05 18:50:00 -08:00
|
|
|
incomplete(maybe_done, <<>>, Handler, Stack, Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_done(Bin, Handler, Stack, Config) ->
|
2013-03-03 15:23:17 -08:00
|
|
|
?error(maybe_done, Bin, Handler, Stack, Config).
|
2013-02-12 11:54:42 -08:00
|
|
|
|
|
|
|
|
2014-12-07 16:31:29 -08:00
|
|
|
done(<<?space, Rest/binary>>, Handler, [], Config) ->
|
|
|
|
done(Rest, Handler, [], Config);
|
|
|
|
done(<<?newline, Rest/binary>>, Handler, [], Config) ->
|
|
|
|
done(Rest, Handler, [], Config);
|
|
|
|
done(<<?tab, Rest/binary>>, Handler, [], Config) ->
|
|
|
|
done(Rest, Handler, [], Config);
|
|
|
|
done(<<?cr, Rest/binary>>, Handler, [], Config) ->
|
2013-02-12 11:54:42 -08:00
|
|
|
done(Rest, Handler, [], Config);
|
2013-10-20 03:28:22 +00:00
|
|
|
done(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
|
2013-10-20 03:20:36 +00:00
|
|
|
?error(done, <<?solidus, Rest/binary>>, Handler, Stack, Config);
|
|
|
|
done(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, done, [comment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
done(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
comment(Rest, Handler, done, [multicomment|Stack], Config);
|
2013-10-20 03:20:36 +00:00
|
|
|
done(<<?solidus>>, Handler, Stack, Config) ->
|
2013-03-05 19:19:16 -08:00
|
|
|
incomplete(done, <<?solidus>>, Handler, Stack, Config);
|
2013-07-10 05:41:19 +00:00
|
|
|
done(<<>>, {Handler, State}, [], Config=#config{stream=true}) ->
|
2013-03-05 19:20:57 -08:00
|
|
|
incomplete(done, <<>>, {Handler, State}, [], Config);
|
2013-02-12 11:54:42 -08:00
|
|
|
done(<<>>, {_Handler, State}, [], _Config) -> State;
|
2013-03-03 15:23:17 -08:00
|
|
|
done(Bin, Handler, Stack, Config) -> ?error(done, Bin, Handler, Stack, Config).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
|
|
|
2013-02-25 20:23:31 -08:00
|
|
|
json_to_bytes(JSON) -> json_to_bytes(JSON, []).
|
|
|
|
|
2013-03-04 14:40:07 -08:00
|
|
|
json_to_bytes(<<>>, Acc) -> [<<>>] ++ lists:reverse(Acc);
|
2013-02-25 20:23:31 -08:00
|
|
|
json_to_bytes(<<X, Rest/binary>>, Acc) -> json_to_bytes(Rest, [<<X>>] ++ Acc).
|
|
|
|
|
|
|
|
|
2013-10-20 21:06:51 +00:00
|
|
|
decode(JSON) -> decode(JSON, []).
|
|
|
|
decode(JSON, Config) -> (decoder(jsx, [], Config))(JSON).
|
|
|
|
|
|
|
|
|
|
|
|
incremental_decode(JSON) -> incremental_decode(JSON, []).
|
|
|
|
incremental_decode(JSON, Config) ->
|
|
|
|
Final = lists:foldl(
|
|
|
|
fun(Byte, Decoder) -> {incomplete, F} = Decoder(Byte), F end,
|
|
|
|
decoder(jsx, [], [stream] ++ Config),
|
|
|
|
json_to_bytes(JSON)
|
|
|
|
),
|
|
|
|
Final(end_stream).
|
2013-02-20 23:57:06 -08:00
|
|
|
|
|
|
|
|
2013-02-13 21:54:09 -08:00
|
|
|
%% all these numbers have different representation in erlang than in javascript and
|
|
|
|
%% do not roundtrip like most integers/floats
|
|
|
|
special_number_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
% {title, test form, json, opt flags}
|
|
|
|
{"-0", [{integer, 0}, end_json], <<"-0">>},
|
|
|
|
{"-0.0", [{float, 0.0}, end_json], <<"-0.0">>},
|
|
|
|
{"0e0", [{float, 0.0}, end_json], <<"0e0">>},
|
|
|
|
{"0e4", [{float, 0.0}, end_json], <<"0e4">>},
|
|
|
|
{"1e0", [{float, 1.0}, end_json], <<"1e0">>},
|
|
|
|
{"-1e0", [{float, -1.0}, end_json], <<"-1e0">>},
|
|
|
|
{"1e4", [{float, 1.0e4}, end_json], <<"1e4">>},
|
|
|
|
{"number terminated by whitespace",
|
2013-03-04 14:25:07 -08:00
|
|
|
[start_array, {integer, 1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1 ]">>
|
|
|
|
},
|
|
|
|
{"number terminated by comma",
|
2013-03-04 14:51:05 -08:00
|
|
|
[start_array, {integer, 1}, {integer, 1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1, 1 ]">>
|
|
|
|
},
|
|
|
|
{"number terminated by comma in object",
|
2013-03-04 14:51:05 -08:00
|
|
|
[start_object, {key, <<"x">>}, {integer, 1}, {key, <<"y">>}, {integer, 1}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{\"x\": 1, \"y\": 1}">>
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual(Events, decode(JSON))}
|
|
|
|
|| {Title, Events, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON))}
|
|
|
|
|| {Title, Events, JSON} <- Cases
|
|
|
|
].
|
2013-02-13 21:54:09 -08:00
|
|
|
|
|
|
|
|
2012-03-19 14:34:07 -07:00
|
|
|
comments_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
% {title, test form, json, opt flags}
|
|
|
|
{"preceeding // comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"// comment ", ?newline, "[]">>
|
|
|
|
},
|
|
|
|
{"preceeding /**/ comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"/* comment */[]">>
|
|
|
|
},
|
|
|
|
{"trailing // comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[]// comment", ?newline>>
|
|
|
|
},
|
|
|
|
{"trailing // comment (no newline)",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[]// comment">>
|
|
|
|
},
|
|
|
|
{"trailing /**/ comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[] /* comment */">>
|
|
|
|
},
|
|
|
|
{"// comment inside array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ // comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment inside array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"// comment at beginning of array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ // comment", ?newline, "true", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment at beginning of array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* comment */ true ]">>
|
|
|
|
},
|
|
|
|
{"// comment at end of array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true // comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment at end of array",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true /* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"// comment midarray (post comma)",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, {literal, false}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true, // comment", ?newline, "false ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment midarray (post comma)",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, {literal, false}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true, /* comment */ false ]">>
|
|
|
|
},
|
|
|
|
{"// comment midarray (pre comma)",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, {literal, false}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true// comment", ?newline, ", false ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment midarray (pre comma)",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, {literal, false}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ true/* comment */, false ]">>
|
|
|
|
},
|
|
|
|
{"// comment inside object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ // comment", ?newline, "}">>
|
|
|
|
},
|
|
|
|
{"/**/ comment inside object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ /* comment */ }">>
|
|
|
|
},
|
|
|
|
{"// comment at beginning of object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ // comment", ?newline, " \"key\": true", ?newline, "}">>
|
|
|
|
},
|
|
|
|
{"/**/ comment at beginning of object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ /* comment */ \"key\": true }">>
|
|
|
|
},
|
|
|
|
{"// comment at end of object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\": true // comment", ?newline, "}">>
|
|
|
|
},
|
|
|
|
{"/**/ comment at end of object",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\": true /* comment */ }">>
|
|
|
|
},
|
|
|
|
{"// comment midobject (post comma)",
|
2012-03-19 14:34:07 -07:00
|
|
|
[
|
|
|
|
start_object,
|
|
|
|
{key, <<"x">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"y">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
end_json
|
2013-02-21 00:13:07 -08:00
|
|
|
],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"x\": true, // comment", ?newline, "\"y\": false }">>
|
|
|
|
},
|
|
|
|
{"/**/ comment midobject (post comma)",
|
2012-03-19 14:34:07 -07:00
|
|
|
[
|
|
|
|
start_object,
|
|
|
|
{key, <<"x">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"y">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
end_json
|
2013-02-21 00:13:07 -08:00
|
|
|
],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"x\": true, /* comment */", ?newline, "\"y\": false }">>
|
|
|
|
},
|
|
|
|
{"// comment midobject (pre comma)",
|
2012-03-19 14:34:07 -07:00
|
|
|
[
|
|
|
|
start_object,
|
|
|
|
{key, <<"x">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"y">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
end_json
|
2013-02-21 00:13:07 -08:00
|
|
|
],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"x\": true// comment", ?newline, ", \"y\": false }">>
|
|
|
|
},
|
|
|
|
{"/**/ comment midobject (pre comma)",
|
2012-03-19 14:34:07 -07:00
|
|
|
[
|
|
|
|
start_object,
|
|
|
|
{key, <<"x">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"y">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
|
|
|
end_json
|
2013-02-21 00:13:07 -08:00
|
|
|
],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"x\": true/* comment */", ?newline, ", \"y\": false }">>
|
|
|
|
},
|
|
|
|
{"// comment precolon",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\" // comment", ?newline, ": true }">>
|
|
|
|
},
|
|
|
|
{"/**/ comment precolon",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\"/* comment */: true }">>
|
|
|
|
},
|
|
|
|
{"// comment postcolon",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\": // comment", ?newline, " true }">>
|
|
|
|
},
|
|
|
|
{"/**/ comment postcolon",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"{ \"key\":/* comment */ true }">>
|
|
|
|
},
|
|
|
|
{"// comment terminating zero",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {integer, 0}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 0// comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"// comment terminating integer",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {integer, 1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1// comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"// comment terminating float",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {float, 1.0}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1.0// comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"// comment terminating exp",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {float, 1.0e1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1e1// comment", ?newline, "]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment terminating zero",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {integer, 0}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 0/* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment terminating integer",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {integer, 1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1/* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment terminating float",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {float, 1.0}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1.0/* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment terminating exp",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {float, 1.0e1}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ 1e1/* comment */ ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment following /**/ comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[/* comment *//* comment */true]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment following // comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[// comment", ?newline, "/* comment */true]">>
|
|
|
|
},
|
|
|
|
{"// comment following /**/ comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[/* comment */// comment", ?newline, "true]">>
|
|
|
|
},
|
|
|
|
{"// comment following // comment",
|
2013-02-21 00:13:07 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[// comment", ?newline, "// comment", ?newline, "true]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment inside /**/ comment",
|
2013-02-26 18:41:53 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* /* comment */ */ true ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment with /",
|
2013-03-03 15:34:45 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* / */ true ]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment with *",
|
2013-03-03 15:34:45 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* * */ true ]">>
|
|
|
|
},
|
|
|
|
{"// comment with badutf",
|
2013-02-26 18:31:44 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ // comment ", 16#00c0, " ", ?newline, "true]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment with badutf",
|
2013-02-26 18:31:44 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* comment ", 16#00c0, " */ true]">>
|
|
|
|
},
|
|
|
|
{"/**/ comment with badutf preceeded by /",
|
2013-03-03 15:34:45 -08:00
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"[ /* comment /", 16#00c0, " */ true]">>
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual(Events, decode(JSON))}
|
|
|
|
|| {Title, Events, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON))}
|
|
|
|
|| {Title, Events, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
% error when `{strict, [comments]}` is present
|
|
|
|
[{Title, ?_assertError(badarg, decode(JSON, [{strict, [comments]}]))}
|
|
|
|
|| {Title, _Events, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
incremental_decode(JSON, [{strict, [comments]}])
|
|
|
|
)} || {Title, _Events, JSON} <- Cases
|
2013-10-20 03:20:36 +00:00
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
no_comments_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
{"// comment with badutf",
|
|
|
|
badarg,
|
|
|
|
<<"[ // comment ", 16#00c0, " ", ?newline, "true]">>,
|
|
|
|
[{strict, [utf8]}]
|
|
|
|
},
|
|
|
|
{"/**/ comment with badutf",
|
|
|
|
badarg,
|
|
|
|
<<"[ /* comment ", 16#00c0, " */ true]">>,
|
|
|
|
[{strict, [utf8]}]
|
|
|
|
},
|
|
|
|
{"/**/ comment with badutf preceeded by /",
|
|
|
|
badarg,
|
|
|
|
<<"[ /* comment /", 16#00c0, " */ true]">>,
|
|
|
|
[{strict, [utf8]}]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertError(Error, decode(JSON, Config))}
|
|
|
|
|| {Title, Error, JSON, Config} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertError(Error, incremental_decode(JSON, Config))}
|
|
|
|
|| {Title, Error, JSON, Config} <- Cases
|
2012-03-19 14:34:07 -07:00
|
|
|
].
|
|
|
|
|
2012-03-28 23:46:18 -07:00
|
|
|
|
2013-10-20 21:06:51 +00:00
|
|
|
% doing the full unicode range takes foreverrrrrrr so just do boundaries
|
|
|
|
% excludes characters that may need escaping
|
2013-02-20 23:57:06 -08:00
|
|
|
codepoints() ->
|
2014-12-03 23:09:21 -08:00
|
|
|
lists:seq(0, 32) ++
|
|
|
|
[32, 33] ++
|
2013-10-20 21:06:51 +00:00
|
|
|
lists:seq(35, 46) ++
|
|
|
|
lists:seq(48, 91) ++
|
|
|
|
lists:seq(93, 127) ++
|
2014-12-01 09:20:17 +00:00
|
|
|
[16#2027, 16#202a, 16#d7ff, 16#e000] ++
|
|
|
|
lists:seq(16#fdd0, 16#ffff) ++
|
|
|
|
[16#10000, 16#20000, 16#30000, 16#40000, 16#50000] ++
|
2013-10-20 21:06:51 +00:00
|
|
|
[16#60000, 16#70000, 16#80000, 16#90000, 16#a0000, 16#b0000] ++
|
|
|
|
[16#c0000, 16#d0000, 16#e0000, 16#f0000, 16#100000].
|
|
|
|
|
|
|
|
surrogates() -> lists:seq(16#d800, 16#dfff).
|
|
|
|
|
2012-03-28 23:46:18 -07:00
|
|
|
|
2012-03-31 12:30:47 -07:00
|
|
|
%% erlang refuses to decode certain codepoints, so fake them all
|
2013-02-21 00:05:12 -08:00
|
|
|
to_fake_utf8(N) when N < 16#0080 -> <<34/utf8, N:8, 34/utf8>>;
|
|
|
|
to_fake_utf8(N) when N < 16#0800 ->
|
2011-10-21 18:16:16 -07:00
|
|
|
<<0:5, Y:5, X:6>> = <<N:16>>,
|
2012-11-21 23:04:14 -08:00
|
|
|
<<34/utf8, 2#110:3, Y:5, 2#10:2, X:6, 34/utf8>>;
|
2013-02-21 00:05:12 -08:00
|
|
|
to_fake_utf8(N) when N < 16#10000 ->
|
2011-10-21 18:16:16 -07:00
|
|
|
<<Z:4, Y:6, X:6>> = <<N:16>>,
|
|
|
|
<<34/utf8, 2#1110:4, Z:4, 2#10:2, Y:6, 2#10:2, X:6, 34/utf8>>;
|
2013-02-21 00:05:12 -08:00
|
|
|
to_fake_utf8(N) ->
|
2011-10-21 18:16:16 -07:00
|
|
|
<<0:3, W:3, Z:6, Y:6, X:6>> = <<N:24>>,
|
|
|
|
<<34/utf8, 2#11110:5, W:3, 2#10:2, Z:6, 2#10:2, Y:6, 2#10:2, X:6, 34/utf8>>.
|
|
|
|
|
|
|
|
|
2013-02-20 23:57:06 -08:00
|
|
|
clean_string_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Clean = codepoints(),
|
2014-12-02 06:57:16 +00:00
|
|
|
Dirty = surrogates(),
|
2013-10-20 21:06:51 +00:00
|
|
|
% clean codepoints
|
|
|
|
[{"clean u+" ++ integer_to_list(Codepoint, 16), ?_assertEqual(
|
|
|
|
[{string, <<Codepoint/utf8>>}, end_json],
|
|
|
|
decode(<<34/utf8, Codepoint/utf8, 34/utf8>>)
|
|
|
|
)} || Codepoint <- Clean
|
|
|
|
] ++
|
|
|
|
% bad codepoints replaced by u+FFFD
|
|
|
|
[{"clean u+" ++ integer_to_list(Codepoint, 16), ?_assertEqual(
|
|
|
|
[{string, <<16#fffd/utf8>>}, end_json],
|
|
|
|
decode(to_fake_utf8(Codepoint))
|
|
|
|
)} || Codepoint <- Dirty
|
|
|
|
] ++
|
|
|
|
% bad codepoints that cause errors
|
|
|
|
[{"dirty u+" ++ integer_to_list(Codepoint, 16), ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(to_fake_utf8(Codepoint), [{strict, [utf8]}])
|
|
|
|
)} || Codepoint <- Dirty
|
2013-02-11 18:22:45 -08:00
|
|
|
].
|
|
|
|
|
2013-02-20 23:57:06 -08:00
|
|
|
|
2013-10-20 21:06:51 +00:00
|
|
|
dirty_string_test_() ->
|
|
|
|
Cases = [
|
|
|
|
{"dirty \\n",
|
|
|
|
[start_array, {string, <<"\\n">>}, end_array, end_json],
|
|
|
|
<<"[\"\\n\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty \\uwxyz",
|
|
|
|
[start_array, {string, <<"\\uwxyz">>}, end_array, end_json],
|
|
|
|
<<"[\"\\uwxyz\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty \\x23",
|
|
|
|
[start_array, {string, <<"\\x23">>}, end_array, end_json],
|
|
|
|
<<"[\"\\x23\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty 0",
|
|
|
|
[start_array, {string, <<0>>}, end_array, end_json],
|
|
|
|
<<"[\"", 0, "\"]">>,
|
2013-10-20 21:16:03 +00:00
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty 0\\\"0",
|
|
|
|
[start_array, {string, <<0, ?rsolidus, ?doublequote, 0>>}, end_array, end_json],
|
|
|
|
<<"[\"", 0, ?rsolidus, ?doublequote, 0, "\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty 0\\\\\"0",
|
|
|
|
[start_array, {string, <<0, ?rsolidus, ?rsolidus, ?doublequote, 0>>}, end_array, end_json],
|
|
|
|
<<"[\"", 0, ?rsolidus, ?rsolidus, ?doublequote, 0, "\"]">>,
|
2013-10-20 21:06:51 +00:00
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty 16#d800",
|
|
|
|
[start_array, {string, <<237, 160, 128>>}, end_array, end_json],
|
|
|
|
<<"[\"", 237, 160, 128, "\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
},
|
|
|
|
{"dirty /",
|
|
|
|
[start_array, {string, <<$/>>}, end_array, end_json],
|
|
|
|
<<"[\"", $/, "\"]">>,
|
|
|
|
[dirty_strings, escaped_forward_slashes]
|
|
|
|
},
|
|
|
|
{"dirty <<194, 129>>",
|
|
|
|
[start_array, {string, <<194, 129>>}, end_array, end_json],
|
|
|
|
<<"[\"", 194, 129, "\"]">>,
|
|
|
|
[dirty_strings]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual(Events, decode(JSON, Config))}
|
|
|
|
|| {Title, Events, JSON, Config} <- Cases
|
|
|
|
] ++
|
|
|
|
% ensure `dirty_strings` and `strict` interact properly
|
|
|
|
[{Title, ?_assertEqual(Events, decode(JSON, Config ++ [strict]))}
|
|
|
|
|| {Title, Events, JSON, Config} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON, Config))}
|
|
|
|
|| {Title, Events, JSON, Config} <- Cases
|
|
|
|
].
|
|
|
|
|
2013-02-24 02:14:47 -08:00
|
|
|
|
|
|
|
bad_utf8_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
{"orphan continuation byte u+0080", <<16#fffd/utf8>>, <<16#0080>>},
|
|
|
|
{"orphan continuation byte u+00bf", <<16#fffd/utf8>>, <<16#00bf>>},
|
|
|
|
{"2 continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, 2),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(binary:copy(<<16#0080>>, 2))/binary>>
|
|
|
|
},
|
|
|
|
{"3 continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, 3),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(binary:copy(<<16#0080>>, 3))/binary>>
|
|
|
|
},
|
|
|
|
{"4 continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, 4),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(binary:copy(<<16#0080>>, 4))/binary>>
|
|
|
|
},
|
|
|
|
{"5 continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, 5),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(binary:copy(<<16#0080>>, 5))/binary>>
|
|
|
|
},
|
|
|
|
{"6 continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, 6),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(binary:copy(<<16#0080>>, 6))/binary>>
|
|
|
|
},
|
|
|
|
{"all continuation bytes",
|
2013-02-24 02:14:47 -08:00
|
|
|
binary:copy(<<16#fffd/utf8>>, length(lists:seq(16#0080, 16#00bf))),
|
2013-10-20 21:06:51 +00:00
|
|
|
<<(list_to_binary(lists:seq(16#0080, 16#00bf)))/binary>>
|
|
|
|
},
|
|
|
|
{"lonely start byte", <<16#fffd/utf8>>, <<16#00c0>>},
|
|
|
|
{"lonely start bytes (2 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#00c0, 32, 16#00df>>
|
|
|
|
},
|
|
|
|
{"lonely start bytes (3 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#00e0, 32, 16#00ef>>
|
|
|
|
},
|
|
|
|
{"lonely start bytes (4 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#00f0, 32, 16#00f7>>
|
|
|
|
},
|
|
|
|
{"missing continuation byte (3 byte)", <<16#fffd/utf8, 32>>, <<224, 160, 32>>},
|
|
|
|
{"missing continuation byte (4 byte missing one)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<240, 144, 128, 32>>
|
|
|
|
},
|
|
|
|
{"missing continuation byte (4 byte missing two)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<240, 144, 32>>
|
|
|
|
},
|
|
|
|
{"overlong encoding of u+002f (2 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#c0, 16#af, 32>>
|
|
|
|
},
|
|
|
|
{"overlong encoding of u+002f (3 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#e0, 16#80, 16#af, 32>>
|
|
|
|
},
|
|
|
|
{"overlong encoding of u+002f (4 byte)",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#f0, 16#80, 16#80, 16#af, 32>>
|
|
|
|
},
|
|
|
|
{"highest overlong 2 byte sequence",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#c1, 16#bf, 32>>
|
|
|
|
},
|
|
|
|
{"highest overlong 3 byte sequence",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#e0, 16#9f, 16#bf, 32>>
|
|
|
|
},
|
|
|
|
{"highest overlong 4 byte sequence",
|
2013-02-24 02:14:47 -08:00
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<16#f0, 16#8f, 16#bf, 16#bf, 32>>
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}])
|
|
|
|
)} || {Title, _, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
incremental_decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}])
|
|
|
|
)} || {Title, _, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " replaced", ?_assertEqual(
|
|
|
|
[{string, Replacement}, end_json],
|
|
|
|
decode(<<34, JSON/binary, 34>>)
|
|
|
|
)} || {Title, Replacement, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " replaced (incremental)", ?_assertEqual(
|
|
|
|
[{string, Replacement}, end_json],
|
|
|
|
incremental_decode(<<34, JSON/binary, 34>>)
|
|
|
|
)} || {Title, Replacement, JSON} <- Cases
|
2013-02-24 02:14:47 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-24 02:28:33 -08:00
|
|
|
unescape_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
{"unescape backspace", <<"\b">>, <<"\\b"/utf8>>},
|
|
|
|
{"unescape tab", <<"\t">>, <<"\\t"/utf8>>},
|
|
|
|
{"unescape newline", <<"\n">>, <<"\\n"/utf8>>},
|
|
|
|
{"unescape formfeed", <<"\f">>, <<"\\f"/utf8>>},
|
|
|
|
{"unescape carriage return", <<"\r">>, <<"\\r"/utf8>>},
|
|
|
|
{"unescape quote", <<"\"">>, <<"\\\""/utf8>>},
|
|
|
|
{"unescape solidus", <<"/">>, <<"\\/"/utf8>>},
|
|
|
|
{"unescape reverse solidus", <<"\\">>, <<"\\\\"/utf8>>},
|
|
|
|
{"unescape control", <<0>>, <<"\\u0000"/utf8>>},
|
|
|
|
{"unescape surrogate pair", <<16#10000/utf8>>, <<"\\ud800\\udc00"/utf8>>},
|
2014-12-07 15:30:37 -08:00
|
|
|
{"unescape surrogate pair", <<16#10000/utf8>>, <<"\\uD800\\uDC00"/utf8>>},
|
2013-10-20 21:06:51 +00:00
|
|
|
{"replace bad high surrogate", <<16#fffd/utf8>>, <<"\\udc00"/utf8>>},
|
2014-12-07 15:30:37 -08:00
|
|
|
{"replace bad high surrogate", <<16#fffd/utf8>>, <<"\\uDC00"/utf8>>},
|
2013-10-20 21:06:51 +00:00
|
|
|
{"replace naked high surrogate",
|
2013-02-27 19:41:22 -08:00
|
|
|
<<16#fffd/utf8, "hello world">>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"\\ud800hello world"/utf8>>
|
|
|
|
},
|
2014-12-07 15:30:37 -08:00
|
|
|
{"replace naked high surrogate",
|
|
|
|
<<16#fffd/utf8, "hello world">>,
|
|
|
|
<<"\\uD800hello world"/utf8>>
|
|
|
|
},
|
2013-10-20 21:06:51 +00:00
|
|
|
{"replace naked low surrogate",
|
2013-02-27 19:41:22 -08:00
|
|
|
<<16#fffd/utf8, "hello world">>,
|
2013-10-20 21:06:51 +00:00
|
|
|
<<"\\udc00hello world"/utf8>>
|
|
|
|
},
|
2014-12-07 15:30:37 -08:00
|
|
|
{"replace naked low surrogate",
|
|
|
|
<<16#fffd/utf8, "hello world">>,
|
|
|
|
<<"\\uDC00hello world"/utf8>>
|
|
|
|
},
|
|
|
|
{"replace bad surrogate pair", <<16#fffd/utf8, 16#fffd/utf8>>, <<"\\ud800\\u0000">>},
|
|
|
|
{"replace bad surrogate pair", <<16#fffd/utf8, 16#fffd/utf8>>, <<"\\uD800\\u0000">>}
|
2013-10-20 21:06:51 +00:00
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual([{string, Escaped}, end_json], decode(<<34, JSON/binary, 34>>))}
|
|
|
|
|| {Title, Escaped, JSON} <- Cases
|
|
|
|
] ++
|
|
|
|
[{Title ++ " (incremental)", ?_assertEqual(
|
|
|
|
[{string, Escaped}, end_json],
|
|
|
|
incremental_decode(<<34, JSON/binary, 34>>)
|
|
|
|
)} || {Title, Escaped, JSON} <- Cases
|
2013-02-24 02:28:33 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-10-20 21:06:51 +00:00
|
|
|
bad_escaped_surrogate_test_() ->
|
|
|
|
Cases = [
|
|
|
|
{"do not unescape bad high surrogate", <<"\\udc00">>},
|
|
|
|
{"do not unescape naked high surrogate", <<"\\ud800hello world">>},
|
|
|
|
{"do not unescape naked low surrogate", <<"\\udc00hello world">>},
|
|
|
|
{"do not unescape bad surrogate pair", <<"\\ud800\\u0000">>}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertError(badarg, decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}]))}
|
|
|
|
|| {Title, JSON} <- Cases
|
|
|
|
].
|
|
|
|
|
2013-02-24 01:50:17 -08:00
|
|
|
|
|
|
|
escape_test_() ->
|
2013-10-20 21:06:51 +00:00
|
|
|
Cases = [
|
|
|
|
{"backspace", <<"\b">>, <<"\\b">>},
|
|
|
|
{"tab", <<"\t">>, <<"\\t">>},
|
|
|
|
{"newline", <<"\n">>, <<"\\n">>},
|
|
|
|
{"formfeed", <<"\f">>, <<"\\f">>},
|
|
|
|
{"carriage return", <<"\r">>, <<"\\r">>},
|
|
|
|
{"quote", <<"\"">>, <<"\\\"">>},
|
|
|
|
{"backslash", <<"\\">>, <<"\\\\">>},
|
|
|
|
{"control", <<0>>, <<"\\u0000">>}
|
|
|
|
],
|
|
|
|
[{"escape " ++ Title, ?_assertEqual(
|
|
|
|
[{string, Escaped}, end_json],
|
|
|
|
decode(<<34, Escaped/binary, 34>>, [escaped_strings])
|
|
|
|
)} || {Title, _Unescaped, Escaped} <- Cases
|
|
|
|
] ++
|
|
|
|
[{"do not escape " ++ Title, ?_assertEqual(
|
|
|
|
[{string, Unescaped}, end_json],
|
|
|
|
decode(<<34, Escaped/binary, 34>>)
|
|
|
|
)} || {Title, Unescaped, Escaped} <- Cases
|
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
special_escape_test_() ->
|
2013-10-21 05:46:53 +00:00
|
|
|
Cases = [
|
|
|
|
{"escape forward slash", <<"\\/">>, <<"/"/utf8>>, [escaped_forward_slashes]},
|
|
|
|
{"do not escape forward slash", <<"/">>, <<"/"/utf8>>, []},
|
|
|
|
{"escape jsonp", <<"\\u2028">>, <<16#2028/utf8>>, []},
|
|
|
|
{"do not escape jsonp", <<16#2028/utf8>>, <<16#2028/utf8>>, [unescaped_jsonp]}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual(
|
|
|
|
[{string, Expect}, end_json],
|
|
|
|
decode(<<34, Raw/binary, 34>>, [escaped_strings] ++ Config)
|
|
|
|
)} || {Title, Expect, Raw, Config} <- Cases
|
2013-02-24 01:50:17 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2014-12-03 23:02:02 -08:00
|
|
|
uescape_test_() ->
|
|
|
|
[
|
|
|
|
{"\"\\u0080\"", ?_assertEqual(
|
|
|
|
[{string, <<"\\u0080">>}, end_json],
|
|
|
|
decode(<<34, 128/utf8, 34>>, [uescape])
|
|
|
|
)},
|
|
|
|
{"\"\\u8ca8\\u5481\\u3002\\u0091\\u0091\"", ?_assertEqual(
|
|
|
|
[{string, <<"\\u8ca8\\u5481\\u3002\\u0091\\u0091">>}, end_json],
|
|
|
|
decode(
|
|
|
|
<<34,232,178,168,229,146,129,227,128,130,194,145,194,145,34>>,
|
|
|
|
[uescape]
|
|
|
|
)
|
|
|
|
)},
|
|
|
|
{"\"\\ud834\\udd1e\"", ?_assertEqual(
|
|
|
|
[{string, <<"\\ud834\\udd1e">>}, end_json],
|
|
|
|
decode(<<34, 240, 157, 132, 158, 34>>, [uescape])
|
|
|
|
)},
|
|
|
|
{"\"\\ud83d\\ude0a\"", ?_assertEqual(
|
|
|
|
[{string, <<"\\ud83d\\ude0a">>}, end_json],
|
|
|
|
decode(<<34, 240, 159, 152, 138, 34>>, [uescape])
|
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-21 00:05:12 -08:00
|
|
|
single_quoted_string_test_() ->
|
2013-10-21 05:46:53 +00:00
|
|
|
Cases = [
|
|
|
|
{"single quoted string", [{string, <<"hello world">>}, end_json], <<39, "hello world", 39>>},
|
|
|
|
{"single quoted string with embedded double quotes",
|
2013-02-21 00:05:12 -08:00
|
|
|
[{string, <<"quoth the raven, \"nevermore\"">>}, end_json],
|
2013-10-21 05:46:53 +00:00
|
|
|
<<39, "quoth the raven, \"nevermore\"", 39>>
|
|
|
|
},
|
|
|
|
{"escaped single quote",
|
2013-02-26 23:16:53 -08:00
|
|
|
[{string, <<"quoth the raven, 'nevermore'">>}, end_json],
|
2013-10-21 05:46:53 +00:00
|
|
|
<<39, "quoth the raven, \\'nevermore\\'", 39>>
|
|
|
|
},
|
|
|
|
{"single quoted key",
|
2013-02-26 23:16:53 -08:00
|
|
|
[start_object,
|
|
|
|
{key, <<"key">>}, {string, <<"value">>},
|
|
|
|
{key, <<"another key">>}, {string, <<"another value">>},
|
|
|
|
end_object, end_json],
|
2013-10-21 05:46:53 +00:00
|
|
|
<<"{'key':'value','another key':'another value'}">>
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[{Title, ?_assertEqual(Expect, decode(Raw, []))} || {Title, Expect, Raw} <- Cases] ++
|
|
|
|
[{Title, ?_assertError(
|
2013-10-20 21:06:51 +00:00
|
|
|
badarg,
|
2013-10-21 05:46:53 +00:00
|
|
|
decode(Raw, [{strict, [single_quotes]}])
|
|
|
|
)} || {Title, _Expect, Raw} <- Cases
|
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
embedded_single_quoted_string_test_() ->
|
|
|
|
[
|
|
|
|
{"string with embedded single quotes", ?_assertEqual(
|
|
|
|
[{string, <<"quoth the raven, 'nevermore'">>}, end_json],
|
|
|
|
decode(<<34, "quoth the raven, 'nevermore'", 34>>, [])
|
|
|
|
)},
|
|
|
|
{"string with embedded single quotes", ?_assertEqual(
|
|
|
|
[{string, <<"quoth the raven, 'nevermore'">>}, end_json],
|
|
|
|
decode(<<34, "quoth the raven, 'nevermore'", 34>>, [{strict, [single_quotes]}])
|
2013-02-21 00:05:12 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-24 01:59:54 -08:00
|
|
|
ignored_bad_escapes_test_() ->
|
|
|
|
[
|
|
|
|
{"ignore unrecognized escape sequence", ?_assertEqual(
|
|
|
|
[{string, <<"\\x25">>}, end_json],
|
2013-10-20 03:28:22 +00:00
|
|
|
decode(<<"\"\\x25\"">>, [])
|
2013-02-24 01:59:54 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-24 20:10:48 -08:00
|
|
|
bom_test_() ->
|
|
|
|
[
|
|
|
|
{"bom", ?_assertEqual(
|
|
|
|
[start_array, end_array, end_json],
|
|
|
|
decode(<<16#ef, 16#bb, 16#bf, "[]"/utf8>>, [])
|
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2014-08-19 17:47:44 -07:00
|
|
|
trailing_comma_test_() ->
|
|
|
|
[
|
|
|
|
{"trailing comma in object", ?_assertEqual(
|
|
|
|
[start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
|
|
|
|
decode(<<"{\"key\": true,}">>, [])
|
|
|
|
)},
|
|
|
|
{"strict trailing comma in object", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"{\"key\": true,}">>, [{strict, [trailing_commas]}])
|
|
|
|
)},
|
|
|
|
{"two trailing commas in object", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"{\"key\": true,,}">>, [])
|
|
|
|
)},
|
|
|
|
{"comma in empty object", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"{,}">>, [])
|
|
|
|
)},
|
|
|
|
{"trailing comma in list", ?_assertEqual(
|
|
|
|
[start_array, {literal, true}, end_array, end_json],
|
|
|
|
decode(<<"[true,]">>, [])
|
|
|
|
)},
|
|
|
|
{"strict trailing comma in list", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"[true,]">>, [{strict, [trailing_commas]}])
|
|
|
|
)},
|
|
|
|
{"two trailing commas in list", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"[true,,]">>, [])
|
|
|
|
)},
|
|
|
|
{"comma in empty list", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
decode(<<"[,]">>, [])
|
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-07-13 02:05:16 +00:00
|
|
|
incomplete_test_() ->
|
|
|
|
[
|
|
|
|
{"stream false", ?_assertError(
|
|
|
|
badarg,
|
2013-10-21 06:25:45 +00:00
|
|
|
decode(<<"{">>)
|
2013-07-13 02:05:16 +00:00
|
|
|
)},
|
2014-02-10 06:39:15 +00:00
|
|
|
{"stream true", ?_assertMatch(
|
|
|
|
{incomplete, _},
|
|
|
|
decode(<<"{">>, [stream])
|
2013-07-13 02:05:16 +00:00
|
|
|
)},
|
2014-02-10 06:39:15 +00:00
|
|
|
{"complete input", ?_assertMatch(
|
|
|
|
{incomplete, _},
|
|
|
|
decode(<<"{}">>, [stream])
|
2013-07-13 02:05:16 +00:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-03-04 14:22:25 -08:00
|
|
|
error_test_() ->
|
2013-10-21 05:46:53 +00:00
|
|
|
Cases = [
|
2013-10-21 06:22:49 +00:00
|
|
|
{"maybe_bom error", <<16#ef, 0>>},
|
|
|
|
{"definitely_bom error", <<16#ef, 16#bb, 0>>},
|
|
|
|
{"object error", <<"{"/utf8, 0>>},
|
|
|
|
{"colon error", <<"{\"\""/utf8, 0>>},
|
|
|
|
{"key error", <<"{\"\":1,"/utf8, 0>>},
|
|
|
|
{"value error", <<0>>},
|
|
|
|
{"negative error", <<"-"/utf8, 0>>},
|
|
|
|
{"zero error", <<"0"/utf8, 0>>},
|
|
|
|
{"integer error", <<"1"/utf8, 0>>},
|
|
|
|
{"decimal error", <<"1.0"/utf8, 0>>},
|
|
|
|
{"e error", <<"1e"/utf8, 0>>},
|
|
|
|
{"ex error", <<"1e+"/utf8, 0>>},
|
|
|
|
{"exp error", <<"1e1"/utf8, 0>>},
|
|
|
|
{"exp error", <<"1.0e1"/utf8, 0>>},
|
|
|
|
{"exp error", <<"1.e"/utf8>>},
|
|
|
|
{"true error", <<"tru"/utf8, 0>>},
|
|
|
|
{"false error", <<"fals"/utf8, 0>>},
|
|
|
|
{"null error", <<"nul"/utf8, 0>>},
|
|
|
|
{"maybe_done error", <<"[[]"/utf8, 0>>},
|
|
|
|
{"done error", <<"[]"/utf8, 0>>}
|
2013-10-21 05:46:53 +00:00
|
|
|
],
|
2013-10-21 06:22:49 +00:00
|
|
|
[{Title, ?_assertError(badarg, decode(State))} || {Title, State} <- Cases].
|
2013-03-04 17:00:34 -08:00
|
|
|
|
|
|
|
|
2013-03-04 23:13:23 -08:00
|
|
|
custom_incomplete_handler_test_() ->
|
|
|
|
[
|
|
|
|
{"custom incomplete handler", ?_assertError(
|
2013-10-21 05:46:53 +00:00
|
|
|
incomplete,
|
|
|
|
decode(<<>>, [{incomplete_handler, fun(_, _, _) -> erlang:error(incomplete) end}, stream])
|
2013-03-04 23:13:23 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-09-19 17:04:38 +02:00
|
|
|
-endif.
|