From 91f503fc07cf75523d9a00cc9f89300de7d4498e Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Mon, 28 Feb 2011 12:44:21 -0800 Subject: [PATCH] test for streaming eep0018 parser --- doc/_layouts/default.html | 0 src/jsx_eep0018.erl | 34 ++++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) delete mode 100644 doc/_layouts/default.html diff --git a/doc/_layouts/default.html b/doc/_layouts/default.html deleted file mode 100644 index e69de29..0000000 diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index 18a21ac..e5c6c30 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -79,8 +79,12 @@ extract_parser_opts(Opts) -> collect_strict({event, Start, Next}, Acc, Opts) when Start =:= start_object; Start =:= start_array -> collect(Next(), [[]|Acc], Opts); -collect_strict(_, _, _) -> - erlang:error(badarg). +collect_strict({incomplete, More}, Acc, Opts) -> + case proplists:get_value(stream, Opts, false) of + true -> {incomplete, fun(JSON) -> collect(More(JSON), Acc, Opts) end} + ; false -> erlang:error(badarg) + end; +collect_strict(_, _, _) -> erlang:error(badarg). %% collect decoder events and convert to eep0018 format @@ -123,19 +127,23 @@ collect({event, Event, Next}, [Current|Rest], Opts) when is_list(Current) -> collect(Next(), [[event(Event, Opts)] ++ Current] ++ Rest, Opts); collect({event, Event, Next}, [Key, Current|Rest], Opts) -> collect(Next(), [[{Key, event(Event, Opts)}] ++ Current] ++ Rest, Opts); -%% if our first returned event is {incomplete, ...} try to force end and return +%% if our returned event is {incomplete, ...} try to force end and return %% the Event if one is returned -collect({incomplete, More}, [[]], Opts) -> +collect({incomplete, More}, Acc, Opts) -> case More(end_stream) of {event, Event, _Next} -> event(Event, Opts) - ; _ -> + ; _ -> + io:format("hi"), case proplists:get_value(stream, Opts, false) of - true -> {incomplete, More} - ; false -> erlang:error(badarg) + true -> + {incomplete, + fun(JSON) -> collect(More(JSON), Acc, Opts) end + } + ; false -> erlang:error(badarg), io:format("hello!") end end; %% any other event is an error -collect(_, _, _) -> erlang:error(badarg). +collect(_, _, _) -> io:format(":("), erlang:error(badarg). %% helper functions for converting jsx events to eep0018 formats @@ -452,5 +460,15 @@ escape_test_() -> ) } ]. + +stream_test_() -> + [ + {"streaming mode", + ?_assert(begin + {incomplete, F} = json_to_term(<<"{">>, [{stream, true}]), + F(<<"}">>) + end =:= [{}]) + } + ]. -endif. \ No newline at end of file