updated examples to match new decoder api
This commit is contained in:
parent
41292d4077
commit
537ad61223
3 changed files with 42 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
39
examples/jsx_stream_parser.erl
Normal file
39
examples/jsx_stream_parser.erl
Normal file
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue