example modules using new api

This commit is contained in:
alisdair sullivan 2010-05-30 16:39:58 -07:00
parent 2bc885f3c4
commit e24d7e7a3e
3 changed files with 25 additions and 23 deletions

View file

@ -33,11 +33,13 @@
decode(JSON) ->
P = jsx:decoder({{jsx_parser, event}, []}, []),
try
{Result, _} = P(JSON),
Result
catch
_:_ -> throw(badarg)
case P(JSON) of
{incomplete, _} ->
{error, badjson}
; {error, badjson} ->
{error, badjosn}
; {Result, _} ->
Result
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.

View file

@ -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.

View file

@ -28,15 +28,16 @@
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
; _:_ -> throw(badarg)
end
end.
fun(Stream) -> try
case Decoder(Stream) of
{incomplete, F} -> {incomplete, F}
; {error, badjson} -> {error, badjson}
end
catch
throw:{ok, Result} -> {ok, Result}
; throw:not_found -> {error, not_found}
end
end.
event(start_object, Level) ->
Level + 1;