From 5309030233145d844821c0871bd04169cefcf5b5 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 20 Jun 2010 18:11:06 -0700 Subject: [PATCH] repaired jsx:fold and moved some defines to jsx_decoder.hrl --- src/jsx.erl | 21 ++++++++++++++------- src/jsx_decoder.erl | 11 ----------- src/jsx_decoder.hrl | 11 +++++++++++ src/jsx_types.hrl | 2 +- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/jsx.erl b/src/jsx.erl index ce78e4f..a8c28d2 100644 --- a/src/jsx.erl +++ b/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()). diff --git a/src/jsx_decoder.erl b/src/jsx_decoder.erl index 0e93ef9..f086588 100644 --- a/src/jsx_decoder.erl +++ b/src/jsx_decoder.erl @@ -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(<>, Stack, Opts) when ?is_whitespace(S) -> start(Rest, Stack, Opts); start(<>, Stack, Opts) -> diff --git a/src/jsx_decoder.hrl b/src/jsx_decoder.hrl index b4f9c88..20ba02d 100644 --- a/src/jsx_decoder.hrl +++ b/src/jsx_decoder.hrl @@ -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). diff --git a/src/jsx_types.hrl b/src/jsx_types.hrl index d85a23b..b0baa7d 100644 --- a/src/jsx_types.hrl +++ b/src/jsx_types.hrl @@ -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}. \ No newline at end of file