diff --git a/examples/jsx_parser.erl b/examples/jsx_parser.erl index 46ffc1f..44ccea6 100644 --- a/examples/jsx_parser.erl +++ b/examples/jsx_parser.erl @@ -32,12 +32,14 @@ %% also is not streaming, though it could be modified to parse partial objects/lists. decode(JSON) -> - P = jsx:decoder({{jsx_parser, event}, []}, []), - try - {Result, _} = P(JSON), - Result - catch - _:_ -> throw(badarg) + P = jsx:decoder({{jsx_parser, event}, []}, []), + 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. diff --git a/examples/jsx_prettify.erl b/examples/jsx_prettify.erl index 7c30c00..bb4e333 100644 --- a/examples/jsx_prettify.erl +++ b/examples/jsx_prettify.erl @@ -35,12 +35,11 @@ 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) - end. + case P(JSON) of + {incomplete, _} -> {error, badjson} + ; {error, badjson} -> {error, badjson} + ; {Result, _} -> Result + end. parse_opts([{indent, Val}|Rest], Opts) -> diff --git a/examples/jsx_stream_parser.erl b/examples/jsx_stream_parser.erl index 77212a0..ec673df 100644 --- a/examples/jsx_stream_parser.erl +++ b/examples/jsx_stream_parser.erl @@ -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;