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) ->
|
decode(JSON) ->
|
||||||
P = jsx:decoder({{jsx_parser, event}, []}, []),
|
P = jsx:decoder({{jsx_parser, event}, []}, []),
|
||||||
try
|
case P(JSON) of
|
||||||
{Result, _} = P(JSON),
|
{incomplete, _} ->
|
||||||
|
{error, badjson}
|
||||||
|
; {error, badjson} ->
|
||||||
|
{error, badjosn}
|
||||||
|
; {Result, _} ->
|
||||||
Result
|
Result
|
||||||
catch
|
|
||||||
_:_ -> throw(badarg)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,7 +73,7 @@ event({key, Key}, [Object|Stack]) ->
|
||||||
[{key, Key}] ++ [Object] ++ Stack;
|
[{key, Key}] ++ [Object] ++ Stack;
|
||||||
|
|
||||||
%% this is kind of a dirty hack, but erlang will interpret atoms when applied to (Args)
|
%% 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
|
%% allow the following shortcut
|
||||||
|
|
||||||
event({Type, Value}, [{key, Key}, Object|Stack]) ->
|
event({Type, Value}, [{key, Key}, Object|Stack]) ->
|
||||||
|
@ -88,7 +90,7 @@ event(end_of_stream, [Stack]) ->
|
||||||
insert(Key, Val, Dict) ->
|
insert(Key, Val, Dict) ->
|
||||||
case dict:is_key(Key, Dict) of
|
case dict:is_key(Key, Dict) of
|
||||||
false -> dict:store(Key, Val, Dict)
|
false -> dict:store(Key, Val, Dict)
|
||||||
; true -> exit(badarg)
|
; true -> erlang:error(badjson)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,11 +35,10 @@
|
||||||
pretty(JSON, Opts) ->
|
pretty(JSON, Opts) ->
|
||||||
Init = init(parse_opts(Opts, #opts{})),
|
Init = init(parse_opts(Opts, #opts{})),
|
||||||
P = jsx:decoder({{jsx_prettify, jsx_event}, Init}, []),
|
P = jsx:decoder({{jsx_prettify, jsx_event}, Init}, []),
|
||||||
try
|
case P(JSON) of
|
||||||
{Result, _} = P(JSON),
|
{incomplete, _} -> {error, badjson}
|
||||||
Result
|
; {error, badjson} -> {error, badjson}
|
||||||
catch
|
; {Result, _} -> Result
|
||||||
_:_ -> throw(badarg)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -28,13 +28,14 @@
|
||||||
|
|
||||||
decoder(Opts) ->
|
decoder(Opts) ->
|
||||||
Decoder = jsx:decoder({{jsx_stream_parser, event}, 0}, Opts),
|
Decoder = jsx:decoder({{jsx_stream_parser, event}, 0}, Opts),
|
||||||
fun(Stream) ->
|
fun(Stream) -> try
|
||||||
try Decoder(Stream) of
|
case Decoder(Stream) of
|
||||||
F when is_function(F) -> F
|
{incomplete, F} -> {incomplete, F}
|
||||||
|
; {error, badjson} -> {error, badjson}
|
||||||
|
end
|
||||||
catch
|
catch
|
||||||
throw:{ok, Result} -> Result
|
throw:{ok, Result} -> {ok, Result}
|
||||||
; throw:not_found -> not_found
|
; throw:not_found -> {error, not_found}
|
||||||
; _:_ -> throw(badarg)
|
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue