repaired jsx:fold and moved some defines to jsx_decoder.hrl
This commit is contained in:
parent
cff4d17561
commit
5309030233
4 changed files with 26 additions and 19 deletions
21
src/jsx.erl
21
src/jsx.erl
|
@ -91,17 +91,17 @@ is_json(JSON, Opts) ->
|
|||
-spec fold(F::fun((jsx_event(), any()) -> any()),
|
||||
Acc::any(),
|
||||
JSON::json()) ->
|
||||
{ok, any()} | {incomplete, jsx_parser()} | {error, atom()}.
|
||||
{ok, any()} | {incomplete, jsx_parser(), fun(() -> jsx_parser_result())} | {error, atom()}.
|
||||
-spec fold(F::fun((jsx_event(), any()) -> any()),
|
||||
Acc::any(),
|
||||
JSON::json(),
|
||||
Opts::jsx_opts()) ->
|
||||
{ok, any()} | {incomplete, jsx_parser()} | {error, atom()}
|
||||
{ok, any()} | {incomplete, jsx_parser(), fun(() -> jsx_parser_result())} | {error, atom()}
|
||||
; (F::fun((jsx_event(), any()) -> any()),
|
||||
Acc::any(),
|
||||
JSON::json(),
|
||||
Parser::jsx_parser()) ->
|
||||
{ok, any()} | {incomplete, jsx_parser()} | {error, atom()}.
|
||||
{ok, any()} | {incomplete, jsx_parser(), fun(() -> jsx_parser_result())} | {error, atom()}.
|
||||
|
||||
fold(F, Acc, JSON) ->
|
||||
P = jsx:parser(),
|
||||
|
@ -113,11 +113,18 @@ fold(F, Acc, JSON, Opts) when is_list(Opts) ->
|
|||
fold(F, Acc, JSON, P) ->
|
||||
fold_loop(F, Acc, P(JSON)).
|
||||
|
||||
fold_loop(F, Acc, {incomplete, Next}) ->
|
||||
{incomplete, fun(Bin) -> fold_loop(F, Acc, Next(Bin)) end};
|
||||
fold_loop(F, Acc, {incomplete, Next, Force}) ->
|
||||
case Force() of
|
||||
{event, Val, End} ->
|
||||
case End() of
|
||||
{event, end_json, _} -> {ok, F(end_json, F(Val, Acc))}
|
||||
; _ -> {incomplete, fun(Bin) -> fold_loop(F, Acc, Next(Bin)) end}
|
||||
end
|
||||
; _ -> {incomplete, fun(Bin) -> fold_loop(F, Acc, Next(Bin)) end}
|
||||
end;
|
||||
fold_loop(_, _, {error, Error}) -> {error, Error};
|
||||
fold_loop(F, Acc, {end_json, _}) -> {ok, F(end_json, Acc)};
|
||||
fold_loop(F, Acc, {Event, Next}) -> fold_loop(F, F(Event, Acc), Next()).
|
||||
fold_loop(F, Acc, {event, end_json, _}) -> {ok, F(end_json, Acc)};
|
||||
fold_loop(F, Acc, {event, Event, Next}) -> fold_loop(F, F(Event, Acc), Next()).
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -48,17 +48,6 @@ parse(JSON, Opts) ->
|
|||
%% anyways, they are horrible and contrary to the spec
|
||||
|
||||
|
||||
%% two macros to simplify incomplete handling
|
||||
-define(incomplete(Valid, Incomplete, Finish),
|
||||
case Valid of
|
||||
true -> {error, badjson}
|
||||
; false -> {incomplete, Incomplete, Finish}
|
||||
end
|
||||
).
|
||||
|
||||
-define(ferror, fun() -> {error, badjson} end).
|
||||
|
||||
|
||||
start(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
start(Rest, Stack, Opts);
|
||||
start(<<?start_object/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
|
|
|
@ -83,6 +83,17 @@
|
|||
).
|
||||
|
||||
|
||||
%% two macros to simplify incomplete handling
|
||||
-define(incomplete(Valid, Incomplete, Finish),
|
||||
case Valid of
|
||||
true -> {error, badjson}
|
||||
; false -> {incomplete, Incomplete, Finish}
|
||||
end
|
||||
).
|
||||
|
||||
-define(ferror, fun() -> {error, badjson} end).
|
||||
|
||||
|
||||
%% compilation macros for unified decoder
|
||||
-ifdef(utf8).
|
||||
-define(encoding, utf8).
|
||||
|
|
|
@ -59,5 +59,5 @@
|
|||
|
||||
|
||||
-type jsx_parser_result() :: {event, jsx_event(), fun(() -> jsx_parser_result())}
|
||||
| {incomplete, jsx_parser()}
|
||||
| {incomplete, jsx_parser(), fun(() -> jsx_parser_result())}
|
||||
| {error, badjson}.
|
Loading…
Add table
Add a link
Reference in a new issue