api change, parser/0,1 deprecated, replaced with decoder/0,1

This commit is contained in:
alisdair sullivan 2011-04-26 23:40:12 -07:00
parent 6cc28573b6
commit b2d923cbcb
8 changed files with 38 additions and 26 deletions

View file

@ -22,7 +22,7 @@ the following module
-export([simple_decode/1]).
simple_decode(JSON) when is_binary(JSON) ->
P = jsx:parser(),
P = jsx:decoder(),
decode(P(JSON), []).
decode({event, end_json, _Next}, Acc) ->

View file

@ -25,7 +25,7 @@
-export([simple_decode/1]).
simple_decode(JSON) when is_binary(JSON) ->
P = jsx:parser(),
P = jsx:decoder(),
decode(P(JSON), []).
decode({event, end_json, _Next}, Acc) ->

View file

@ -64,11 +64,11 @@
%% this probably doesn't work properly
-type jsx_parser() :: fun((binary()) -> jsx_parser_result()).
-type jsx_decoder() :: fun((binary()) -> jsx_decoder_result()).
-type jsx_parser_result() ::
{event, jsx_event(), fun(() -> jsx_parser_result())}
| {incomplete, jsx_parser()}
-type jsx_decoder_result() ::
{event, jsx_event(), fun(() -> jsx_decoder_result())}
| {incomplete, jsx_decoder()}
| {error, {badjson, binary()}}.

View file

@ -26,7 +26,7 @@
%% this file should take that into account
-spec parser(OptsList::jsx_opts()) -> jsx_parser().
-spec decoder(OptsList::jsx_opts()) -> jsx_decoder().
%% opts record for decoder
-record(opts, {
@ -121,11 +121,11 @@
-endif.
-export([parser/1]).
-export([decoder/1]).
parser(OptsList) ->
decoder(OptsList) ->
case parse_opts(OptsList) of
{error, badopt} -> {error, badopt}
; Opts -> fun(JSON) -> start(JSON, [], Opts) end

View file

@ -26,6 +26,7 @@
%% the core parser api
-export([parser/0, parser/1]).
-export([decoder/0, decoder/1]).
-export([term_to_json/1, term_to_json/2]).
-export([json_to_term/1, json_to_term/2]).
-export([is_json/1, is_json/2]).
@ -40,21 +41,32 @@
-endif.
-spec parser() -> jsx_parser().
-spec parser() -> jsx_decoder().
parser() ->
parser([]).
parser() -> decoder([]).
-spec parser(OptsList::jsx_opts()) -> jsx_parser().
-spec parser(OptsList::jsx_opts()) -> jsx_decoder().
parser(OptsList) ->
parser(OptsList) -> decoder(OptsList).
-spec decoder() -> jsx_decoder().
decoder() -> decoder([]).
-spec decoder(OptsList::jsx_opts()) -> jsx_decoder().
decoder(OptsList) ->
case proplists:get_value(encoding, OptsList, auto) of
utf8 -> jsx_utf8:parser(OptsList)
; utf16 -> jsx_utf16:parser(OptsList)
; utf32 -> jsx_utf32:parser(OptsList)
; {utf16, little} -> jsx_utf16le:parser(OptsList)
; {utf32, little} -> jsx_utf32le:parser(OptsList)
utf8 -> jsx_utf8:decoder(OptsList)
; utf16 -> jsx_utf16:decoder(OptsList)
; utf32 -> jsx_utf32:decoder(OptsList)
; {utf16, little} -> jsx_utf16le:decoder(OptsList)
; {utf32, little} -> jsx_utf32le:decoder(OptsList)
; auto -> jsx_utils:detect_encoding(OptsList)
end.
@ -196,7 +208,7 @@ parse_tests([], _Dir, Acc) ->
decode(JSON, Flags) ->
P = jsx:parser(Flags),
P = jsx:decoder(Flags),
decode_loop(P(JSON), []).
decode_loop({event, end_json, _Next}, Acc) ->
@ -208,7 +220,7 @@ decode_loop({event, E, Next}, Acc) ->
incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
P = jsx:parser(Flags),
P = jsx:decoder(Flags),
incremental_decode_loop(P(C), Rest, []).
incremental_decode_loop({incomplete, Next}, <<>>, Acc) ->
@ -230,7 +242,7 @@ multi_decode_test_() ->
multi_decode(JSON, Flags) ->
P = jsx:parser(Flags ++ [{multi_term, true}]),
P = jsx:decoder(Flags ++ [{multi_term, true}]),
multi_decode_loop(P(JSON), [[]]).
multi_decode_loop({incomplete, _Next}, [[]|Acc]) ->

View file

@ -39,7 +39,7 @@
-spec json_to_term(JSON::binary(), Opts::decoder_opts()) -> eep0018().
json_to_term(JSON, Opts) ->
P = jsx:parser(extract_parser_opts(Opts)),
P = jsx:decoder(extract_parser_opts(Opts)),
case proplists:get_value(strict, Opts, false) of
true -> collect_strict(P(JSON), [[]], Opts)
; false -> collect(P(JSON), [[]], Opts)

View file

@ -41,7 +41,7 @@
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist().
format(JSON, Opts) when is_binary(JSON) ->
P = jsx:parser(extract_parser_opts(Opts)),
P = jsx:decoder(extract_parser_opts(Opts)),
format(fun() -> P(JSON) end, Opts);
format(F, OptsList) when is_function(F) ->
Opts = parse_opts(OptsList, #format_opts{}),

View file

@ -40,7 +40,7 @@
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
is_json(JSON, Opts) ->
P = jsx:parser(extract_parser_opts(Opts)),
P = jsx:decoder(extract_parser_opts(Opts)),
case proplists:get_value(strict, Opts, false) of
true -> collect_strict(P(JSON), [[]])
; false -> collect(P(JSON), [[]])