repaired jsx:fold and moved some defines to jsx_decoder.hrl

This commit is contained in:
alisdair sullivan 2010-06-20 18:11:06 -07:00
parent cff4d17561
commit 5309030233
4 changed files with 26 additions and 19 deletions

View file

@ -91,17 +91,17 @@ is_json(JSON, Opts) ->
-spec fold(F::fun((jsx_event(), any()) -> any()), -spec fold(F::fun((jsx_event(), any()) -> any()),
Acc::any(), Acc::any(),
JSON::json()) -> 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()), -spec fold(F::fun((jsx_event(), any()) -> any()),
Acc::any(), Acc::any(),
JSON::json(), JSON::json(),
Opts::jsx_opts()) -> 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()), ; (F::fun((jsx_event(), any()) -> any()),
Acc::any(), Acc::any(),
JSON::json(), JSON::json(),
Parser::jsx_parser()) -> 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) -> fold(F, Acc, JSON) ->
P = jsx:parser(), P = jsx:parser(),
@ -113,11 +113,18 @@ fold(F, Acc, JSON, Opts) when is_list(Opts) ->
fold(F, Acc, JSON, P) -> fold(F, Acc, JSON, P) ->
fold_loop(F, Acc, P(JSON)). fold_loop(F, Acc, P(JSON)).
fold_loop(F, Acc, {incomplete, Next}) -> fold_loop(F, Acc, {incomplete, Next, Force}) ->
{incomplete, fun(Bin) -> fold_loop(F, Acc, Next(Bin)) end}; 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(_, _, {error, Error}) -> {error, Error};
fold_loop(F, Acc, {end_json, _}) -> {ok, F(end_json, Acc)}; fold_loop(F, Acc, {event, 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, Event, Next}) -> fold_loop(F, F(Event, Acc), Next()).

View file

@ -48,17 +48,6 @@ parse(JSON, Opts) ->
%% anyways, they are horrible and contrary to the spec %% 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(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
start(Rest, Stack, Opts); start(Rest, Stack, Opts);
start(<<?start_object/?encoding, Rest/binary>>, Stack, Opts) -> start(<<?start_object/?encoding, Rest/binary>>, Stack, Opts) ->

View file

@ -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 %% compilation macros for unified decoder
-ifdef(utf8). -ifdef(utf8).
-define(encoding, utf8). -define(encoding, utf8).

View file

@ -59,5 +59,5 @@
-type jsx_parser_result() :: {event, jsx_event(), fun(() -> jsx_parser_result())} -type jsx_parser_result() :: {event, jsx_event(), fun(() -> jsx_parser_result())}
| {incomplete, jsx_parser()} | {incomplete, jsx_parser(), fun(() -> jsx_parser_result())}
| {error, badjson}. | {error, badjson}.