diff --git a/examples/jsx_parser.erl b/examples/jsx_parser.erl index 873c731..70b02e4 100644 --- a/examples/jsx_parser.erl +++ b/examples/jsx_parser.erl @@ -1,6 +1,7 @@ -module(jsx_parser). -export([decode/2, event/2]). +-export([literal/1, string/1, number/1]). %% this is a strict parser, no comments, no naked values and only one key per object. it @@ -8,7 +9,7 @@ decode(JSON, Opts) -> P = jsx:decoder({{jsx_parser, event}, []}, Opts), - {{_, Result}, Rest} = P(JSON), + {Result, Rest} = P(JSON), case jsx:tail_clean(Rest) of true -> Result ; _ -> exit(badarg) diff --git a/examples/jsx_prettify.erl b/examples/jsx_prettify.erl index a713a7c..5004663 100644 --- a/examples/jsx_prettify.erl +++ b/examples/jsx_prettify.erl @@ -9,7 +9,7 @@ pretty(JSON, Opts) -> Init = init(parse_opts(Opts, #opts{})), - {{_, Result}, Rest} = (jsx:decoder({{pretty_printer, jsx_event}, Init}, []))(JSON), + {Result, Rest} = (jsx:decoder({{jsx_prettify, jsx_event}, Init}, []))(JSON), case jsx:tail_clean(Rest) of true -> Result ; _ -> exit(badarg) diff --git a/examples/jsx_stream_parser.erl b/examples/jsx_stream_parser.erl new file mode 100644 index 0000000..46fa83e --- /dev/null +++ b/examples/jsx_stream_parser.erl @@ -0,0 +1,39 @@ +-module(jsx_stream_parser). + +-export([decoder/1, event/2]). + +decoder(Opts) -> + Decoder = jsx:decoder({{jsx_stream_parser, event}, 0}, Opts), + fun(Stream) -> + try Decoder(Stream) of + F when is_function(F) -> F + catch + throw:{ok, Result} -> Result + ; throw:not_found -> not_found + end + end. + +event(start_object, Level) -> + Level + 1; + +event(start_array, 0) -> + throw(not_found); +event(start_array, Level) -> + Level + 1; + +event(end_object, Level) -> + Level - 1; +event(end_array, Level) -> + Level - 1; + +event({key, "_id"}, 1) -> + capture; + +event({string, String}, capture) -> + throw({ok, String}); + +event(eof, _) -> + throw(not_found); + +event(_, Level) -> + Level. \ No newline at end of file