example modules using new api
This commit is contained in:
parent
2bc885f3c4
commit
e24d7e7a3e
3 changed files with 25 additions and 23 deletions
|
@ -33,11 +33,13 @@
|
|||
|
||||
decode(JSON) ->
|
||||
P = jsx:decoder({{jsx_parser, event}, []}, []),
|
||||
try
|
||||
{Result, _} = P(JSON),
|
||||
case P(JSON) of
|
||||
{incomplete, _} ->
|
||||
{error, badjson}
|
||||
; {error, badjson} ->
|
||||
{error, badjosn}
|
||||
; {Result, _} ->
|
||||
Result
|
||||
catch
|
||||
_:_ -> throw(badarg)
|
||||
end.
|
||||
|
||||
|
||||
|
@ -71,7 +73,7 @@ event({key, Key}, [Object|Stack]) ->
|
|||
[{key, Key}] ++ [Object] ++ Stack;
|
||||
|
||||
%% this is kind of a dirty hack, but erlang will interpret atoms when applied to (Args)
|
||||
%% as a function. so naming out formatting functions string, number and literal will
|
||||
%% as a function. so naming our formatting functions string, number and literal will
|
||||
%% allow the following shortcut
|
||||
|
||||
event({Type, Value}, [{key, Key}, Object|Stack]) ->
|
||||
|
@ -88,7 +90,7 @@ event(end_of_stream, [Stack]) ->
|
|||
insert(Key, Val, Dict) ->
|
||||
case dict:is_key(Key, Dict) of
|
||||
false -> dict:store(Key, Val, Dict)
|
||||
; true -> exit(badarg)
|
||||
; true -> erlang:error(badjson)
|
||||
end.
|
||||
|
||||
|
||||
|
|
|
@ -35,11 +35,10 @@
|
|||
pretty(JSON, Opts) ->
|
||||
Init = init(parse_opts(Opts, #opts{})),
|
||||
P = jsx:decoder({{jsx_prettify, jsx_event}, Init}, []),
|
||||
try
|
||||
{Result, _} = P(JSON),
|
||||
Result
|
||||
catch
|
||||
_:_ -> throw(badarg)
|
||||
case P(JSON) of
|
||||
{incomplete, _} -> {error, badjson}
|
||||
; {error, badjson} -> {error, badjson}
|
||||
; {Result, _} -> Result
|
||||
end.
|
||||
|
||||
|
||||
|
|
|
@ -28,13 +28,14 @@
|
|||
|
||||
decoder(Opts) ->
|
||||
Decoder = jsx:decoder({{jsx_stream_parser, event}, 0}, Opts),
|
||||
fun(Stream) ->
|
||||
try Decoder(Stream) of
|
||||
F when is_function(F) -> F
|
||||
fun(Stream) -> try
|
||||
case Decoder(Stream) of
|
||||
{incomplete, F} -> {incomplete, F}
|
||||
; {error, badjson} -> {error, badjson}
|
||||
end
|
||||
catch
|
||||
throw:{ok, Result} -> Result
|
||||
; throw:not_found -> not_found
|
||||
; _:_ -> throw(badarg)
|
||||
throw:{ok, Result} -> {ok, Result}
|
||||
; throw:not_found -> {error, not_found}
|
||||
end
|
||||
end.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue