diff --git a/include/jsx_decoder.hrl b/include/jsx_decoder.hrl index 397c789..7362646 100644 --- a/include/jsx_decoder.hrl +++ b/include/jsx_decoder.hrl @@ -83,39 +83,29 @@ ). -%% 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). --define(partial_codepoint(Bin), byte_size(Bin) >= 1). +-define(partial_codepoint(Bin), byte_size(Bin) < 1). -endif. -ifdef(utf16). -define(encoding, utf16). --define(partial_codepoint(Bin), byte_size(Bin) >= 2). +-define(partial_codepoint(Bin), byte_size(Bin) < 2). -endif. -ifdef(utf16le). -define(encoding, utf16-little). --define(partial_codepoint(Bin), byte_size(Bin) >= 2). +-define(partial_codepoint(Bin), byte_size(Bin) < 2). -endif. -ifdef(utf32). -define(encoding, utf32). --define(partial_codepoint(Bin), byte_size(Bin) >= 4). +-define(partial_codepoint(Bin), byte_size(Bin) < 4). -endif. -ifdef(utf32le). -define(encoding, utf32-little). --define(partial_codepoint(Bin), byte_size(Bin) >= 4). +-define(partial_codepoint(Bin), byte_size(Bin) < 4). -endif. \ No newline at end of file diff --git a/include/jsx_types.hrl b/include/jsx_types.hrl index b21ab50..07d9adb 100644 --- a/include/jsx_types.hrl +++ b/include/jsx_types.hrl @@ -58,5 +58,6 @@ -type jsx_parser() :: fun((json()) -> jsx_parser_result()). -type jsx_parser_result() :: {event, jsx_event(), fun(() -> jsx_parser_result())} - | {incomplete, jsx_parser(), fun(() -> jsx_parser_result())} - | {error, badjson}. \ No newline at end of file + | {incomplete, jsx_parser()} + | {error, badjson} + | ok. \ No newline at end of file diff --git a/priv/jsx_decoder.erl b/priv/jsx_decoder.erl index f884135..757bd37 100644 --- a/priv/jsx_decoder.erl +++ b/priv/jsx_decoder.erl @@ -71,10 +71,15 @@ start(<>, Stack, Opts) when ?is_nonzero(S) -> start(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> start(Resume, Stack, Opts) end); start(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> start(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> start(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. maybe_done(<>, Stack, Opts) when ?is_whitespace(S) -> maybe_done(Rest, Stack, Opts); @@ -91,28 +96,32 @@ maybe_done(<>, Stack, ?comments_enabled(Opts)) maybe_done(Rest, [], ?multi_term(Opts)) -> {event, end_json, fun() -> start(Rest, [], Opts) end}; maybe_done(Rest, [], Opts) -> - {event, end_json, fun() -> - {incomplete, - fun(Stream) -> done(<>, Opts) end, - fun() -> done(Rest, Opts) end - } - end - }; + {event, end_json, fun() -> done(Rest, Opts) end}; maybe_done(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> maybe_done(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> case Bin == <<>> of true -> ok; false -> {error, badjson} end + ; (Stream) -> maybe_done(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. done(<>, Opts) when ?is_whitespace(S) -> done(Rest, Opts); done(<>, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> done(Resume, Opts) end); done(Bin, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> done(<>, Opts) end, - fun() -> done(Bin, Opts) end - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> case Bin == <<>> of true -> ok; false -> {error, badjson} end + ; (Stream) -> done(<>, Opts) + end + } + ; false -> {error, badjson} + end. object(<>, Stack, Opts) when ?is_whitespace(S) -> @@ -124,10 +133,15 @@ object(<>, [key|Stack], Opts) -> object(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> object(Resume, Stack, Opts) end); object(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> object(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> object(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. array(<>, Stack, Opts) when ?is_whitespace(S) -> @@ -155,10 +169,15 @@ array(<>, [array|Stack], Opts) -> array(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> array(Resume, Stack, Opts) end); array(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> array(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> array(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. value(<>, Stack, Opts) when ?is_whitespace(S) -> @@ -184,10 +203,15 @@ value(<>, Stack, Opts) -> value(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> value(Resume, Stack, Opts) end); value(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> value(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> value(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. colon(<>, Stack, Opts) when ?is_whitespace(S) -> @@ -197,10 +221,15 @@ colon(<>, [key|Stack], Opts) -> colon(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> colon(Resume, Stack, Opts) end); colon(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> colon(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> colon(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. key(<>, Stack, Opts) when ?is_whitespace(S) -> @@ -210,10 +239,15 @@ key(<>, Stack, Opts) -> key(<>, Stack, ?comments_enabled(Opts)) -> maybe_comment(Rest, fun(Resume) -> key(Resume, Stack, Opts) end); key(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> key(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> key(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. %% string has an additional parameter, an accumulator (Acc) used to hold the intermediate @@ -234,8 +268,13 @@ string(<>, Stack, Opts, Acc) when ?is_noncontrol(S) -> string(Rest, Stack, Opts, [S] ++ Acc); string(Bin, Stack, Opts, Acc) -> case partial_utf(Bin) of - false -> {error, badjson} - ; _ -> {incomplete, fun(Stream) -> string(<>, Stack, Opts, Acc) end, ?ferror} + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> string(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} end. @@ -307,10 +346,15 @@ escape(<>, Stack, Opts, Acc) when S =:= ?quote; S =:= ?solidus; S =:= ?rsolidus -> string(Rest, Stack, Opts, [S] ++ Acc); escape(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> escape(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> escape(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. %% this code is ugly and unfortunate, but so is json's handling of escaped unicode @@ -357,10 +401,15 @@ escaped_unicode(<>, Stack, Opts, String, [C, B, A]) wh escaped_unicode(<>, Stack, Opts, String, Acc) when ?is_hex(S) -> escaped_unicode(Rest, Stack, Opts, String, [S] ++ Acc); escaped_unicode(Bin, Stack, Opts, String, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> escaped_unicode(<>, Stack, Opts, String, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> escaped_unicode(<>, Stack, Opts, String, Acc) + end + } + ; false -> {error, badjson} + end. %% upon encountering a low pair json/hex encoded value, check to see if there's a high %% value already in the accumulator @@ -390,10 +439,15 @@ negative(<<$0/?encoding, Rest/binary>>, Stack, Opts, Acc) -> negative(<>, Stack, Opts, Acc) when ?is_nonzero(S) -> integer(Rest, Stack, Opts, [S] ++ Acc); negative(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> negative(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> negative(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. zero(<>, [object|Stack], Opts, Acc) -> @@ -416,14 +470,21 @@ zero(<>, Stack, ?comments_enabled(Opts), Acc) - maybe_comment(Rest, fun(Resume) -> zero(Resume, Stack, Opts, Acc) end); zero(<<>>, [], Opts, Acc) -> {incomplete, - fun(Stream) -> zero(Stream, [], Opts, Acc) end, - fun() -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end + fun + (end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} + ; (Stream) -> zero(Stream, [], Opts, Acc) + end }; zero(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> zero(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> zero(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. integer(<>, Stack, Opts, Acc) when ?is_nonzero(S) -> @@ -454,14 +515,21 @@ integer(<>, Stack, ?comments_enabled(Opts), Acc maybe_comment(Rest, fun(Resume) -> integer(Resume, Stack, Opts, Acc) end); integer(<<>>, [], Opts, Acc) -> {incomplete, - fun(Stream) -> integer(Stream, [], Opts, Acc) end, - fun() -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end + fun + (end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} + ; (Stream) -> integer(Stream, [], Opts, Acc) + end }; integer(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> integer(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> integer(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. initial_decimal(<>, Stack, Opts, Acc) when ?is_nonzero(S) -> @@ -469,10 +537,15 @@ initial_decimal(<>, Stack, Opts, Acc) when ?is_nonzero initial_decimal(<>, Stack, Opts, Acc) -> decimal(Rest, Stack, Opts, [?zero] ++ Acc); initial_decimal(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> initial_decimal(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> initial_decimal(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. decimal(<>, Stack, Opts, Acc) when ?is_nonzero(S) -> @@ -501,14 +574,21 @@ decimal(<>, Stack, ?comments_enabled(Opts), Acc maybe_comment(Rest, fun(Resume) -> decimal(Resume, Stack, Opts, Acc) end); decimal(<<>>, [], Opts, Acc) -> {incomplete, - fun(Stream) -> decimal(Stream, [], Opts, Acc) end, - fun() -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end + fun + (end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} + ; (Stream) -> decimal(Stream, [], Opts, Acc) + end }; decimal(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> decimal(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> decimal(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. e(<>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero(S) -> @@ -516,19 +596,29 @@ e(<>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero( e(<>, Stack, Opts, Acc) when S =:= ?positive; S =:= ?negative -> ex(Rest, Stack, Opts, [S] ++ Acc); e(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> e(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> e(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. ex(<>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero(S) -> exp(Rest, Stack, Opts, [S] ++ Acc); ex(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> ex(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> ex(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. exp(<>, Stack, Opts, Acc) when ?is_nonzero(S) -> @@ -553,104 +643,161 @@ exp(<>, Stack, ?comments_enabled(Opts), Acc) -> maybe_comment(Rest, fun(Resume) -> exp(Resume, Stack, Opts, Acc) end); exp(<<>>, [], Opts, Acc) -> {incomplete, - fun(Stream) -> exp(Stream, [], Opts, Acc) end, - fun() -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end + fun + (end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} + ; (Stream) -> exp(Stream, [], Opts, Acc) + end }; exp(Bin, Stack, Opts, Acc) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> exp(<>, Stack, Opts, Acc) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> exp(<>, Stack, Opts, Acc) + end + } + ; false -> {error, badjson} + end. tr(<<$r/?encoding, Rest/binary>>, Stack, Opts) -> tru(Rest, Stack, Opts); tr(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> tr(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> tr(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. tru(<<$u/?encoding, Rest/binary>>, Stack, Opts) -> true(Rest, Stack, Opts); tru(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> tru(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> tru(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. true(<<$e/?encoding, Rest/binary>>, Stack, Opts) -> {event, {literal, true}, fun() -> maybe_done(Rest, Stack, Opts) end}; true(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> true(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> true(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. fa(<<$a/?encoding, Rest/binary>>, Stack, Opts) -> fal(Rest, Stack, Opts); fa(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> fa(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> fa(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. fal(<<$l/?encoding, Rest/binary>>, Stack, Opts) -> fals(Rest, Stack, Opts); fal(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> fal(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> fal(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. fals(<<$s/?encoding, Rest/binary>>, Stack, Opts) -> false(Rest, Stack, Opts); fals(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> fals(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> fals(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. false(<<$e/?encoding, Rest/binary>>, Stack, Opts) -> {event, {literal, false}, fun() -> maybe_done(Rest, Stack, Opts) end}; false(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> false(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> false(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. nu(<<$u/?encoding, Rest/binary>>, Stack, Opts) -> nul(Rest, Stack, Opts); nu(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> nu(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> nu(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. nul(<<$l/?encoding, Rest/binary>>, Stack, Opts) -> null(Rest, Stack, Opts); nul(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> nul(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> nul(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. null(<<$l/?encoding, Rest/binary>>, Stack, Opts) -> {event, {literal, null}, fun() -> maybe_done(Rest, Stack, Opts) end}; null(Bin, Stack, Opts) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> null(<>, Stack, Opts) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> null(<>, Stack, Opts) + end + } + ; false -> {error, badjson} + end. %% comments are c style, ex: /* blah blah */ @@ -662,10 +809,15 @@ null(Bin, Stack, Opts) -> maybe_comment(<>, Resume) -> comment(Rest, Resume); maybe_comment(Bin, Resume) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> maybe_comment(<>, Resume) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> maybe_comment(<>, Resume) + end + } + ; false -> {error, badjson} + end. comment(<>, Resume) -> @@ -673,10 +825,15 @@ comment(<>, Resume) -> comment(<<_/?encoding, Rest/binary>>, Resume) -> comment(Rest, Resume); comment(Bin, Resume) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> comment(<>, Resume) end, - ?ferror - ). + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> comment(<>, Resume) + end + } + ; false -> {error, badjson} + end. maybe_comment_done(<>, Resume) -> @@ -684,7 +841,12 @@ maybe_comment_done(<>, Resume) -> maybe_comment_done(<<_/?encoding, Rest/binary>>, Resume) -> comment(Rest, Resume); maybe_comment_done(Bin, Resume) -> - ?incomplete(?partial_codepoint(Bin), - fun(Stream) -> maybe_comment_done(<>, Resume) end, - ?ferror - ). \ No newline at end of file + case ?partial_codepoint(Bin) of + true -> + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> maybe_comment_done(<>, Resume) + end + } + ; false -> {error, badjson} + end. \ No newline at end of file diff --git a/priv/jsx_test.escript b/priv/jsx_test.escript index 69bd8e4..267ccca 100755 --- a/priv/jsx_test.escript +++ b/priv/jsx_test.escript @@ -80,10 +80,14 @@ decode(JSON, Flags) -> P = jsx:parser(Flags), decode_loop(P(JSON), []). -decode_loop({incomplete, _Next, Force}, Acc) -> - decode_loop(Force(), Acc); -decode_loop({event, end_json, _}, Acc) -> - lists:reverse(Acc); +decode_loop({incomplete, Next}, Acc) -> + case Next(end_stream) of + {error, badjson} -> {error, badjson} + ; ok -> lists:reverse(Acc) + ; X -> decode_loop(X, Acc) + end; +decode_loop({event, end_json, Next}, Acc) -> + decode_loop(Next(), Acc); decode_loop({event, E, Next}, Acc) -> decode_loop(Next(), [E] ++ Acc). @@ -92,16 +96,16 @@ incremental_decode(<>, Flags) -> P = jsx:parser(Flags), incremental_decode_loop(P(C), Rest, []). -incremental_decode_loop({incomplete, Next, _}, <>, Acc) -> +incremental_decode_loop({incomplete, Next}, <>, Acc) -> incremental_decode_loop(Next(C), Rest, Acc); -incremental_decode_loop({incomplete, _Next, Force}, <<>>, Acc) -> - case Force() of +incremental_decode_loop({incomplete, Next}, <<>>, Acc) -> + case Next(end_stream) of {error, badjson} -> {error, badjson} - ; {incomplete, _, _} -> Acc - ; _ -> incremental_decode_loop(Force(), <<>>, Acc) + ; ok -> lists:reverse(Acc) + ; X -> incremental_decode_loop(X, <<>>, Acc) end; incremental_decode_loop({event, end_json, Next}, Rest, Acc) -> - incremental_decode_loop(Next(), Rest, lists:reverse(Acc)); + incremental_decode_loop(Next(), Rest, Acc); incremental_decode_loop({event, Event, Next}, Rest, Acc) -> incremental_decode_loop(Next(), Rest, [Event] ++ Acc). @@ -110,7 +114,7 @@ multi_decode(JSON, Flags) -> P = jsx:parser(Flags ++ [{multi_term, true}]), multi_decode_loop(P(JSON), [[]]). -multi_decode_loop({incomplete, _Next, _Force}, [[]|Acc]) -> +multi_decode_loop({incomplete, _Next}, [[]|Acc]) -> lists:reverse(Acc); multi_decode_loop({event, end_json, Next}, [S|Acc]) -> multi_decode_loop(Next(), [[]|[lists:reverse(S)] ++ Acc]); diff --git a/src/jsx.erl b/src/jsx.erl index 438aec6..8aa7411 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -118,39 +118,43 @@ detect_encoding(<> = JSON, Opts) when X =/= 0, Y =/= 0 -> %% to conclusively determine the encoding correctly. below is an attempt to solve %% the problem detect_encoding(<>, Opts) when X =/= 0 -> - {incomplete, - fun(Stream) -> detect_encoding(<>, Opts) end, - fun() -> try - {incomplete, _, Force} = jsx_utf8:parse(<>, Opts), - Force() - catch error:function_clause -> {error, badjson} - end + {incomplete, + fun(end_stream) -> + try + {incomplete, Next} = jsx_utf8:parse(<>, Opts), + Next(end_stream) + catch error:function_clause -> {error, badjson} + end + ; (Stream) -> detect_encoding(<>, Opts) end }; detect_encoding(<<0, X>>, Opts) when X =/= 0 -> - {incomplete, - fun(Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts) end, - fun() -> try - {incomplete, _, Force} = jsx_utf16:parse(<<0, X>>, Opts), - Force() - catch error:function_clause -> {error, badjson} - end + {incomplete, + fun(end_stream) -> + try + {incomplete, Next} = jsx_utf16:parse(<<0, X>>, Opts), + Next(end_stream) + catch error:function_clause -> {error, badjson} + end + ; (Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts) end }; detect_encoding(<>, Opts) when X =/= 0 -> - {incomplete, - fun(Stream) -> detect_encoding(<>, Opts) end, - fun() -> try - {incomplete, _, Force} = jsx_utf16le:parse(<>, Opts), - Force() - catch error:function_clause -> {error, badjson} - end + {incomplete, + fun(end_stream) -> + try + {incomplete, Next} = jsx_utf16le:parse(<>, Opts), + Next(end_stream) + catch error:function_clause -> {error, badjson} + end + ; (Stream) -> detect_encoding(<>, Opts) end }; %% not enough input, request more detect_encoding(Bin, Opts) -> - {incomplete, - fun(Stream) -> detect_encoding(<>, Opts) end, - fun() -> {error, badjson} end + {incomplete, + fun(end_stream) -> {error, badjson} + ; (Stream) -> detect_encoding(<>, Opts) + end }. \ No newline at end of file