From b2d923cbcbd942c0e6c9d7e135c7b3863cede5f5 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 26 Apr 2011 23:40:12 -0700 Subject: [PATCH] api change, parser/0,1 deprecated, replaced with decoder/0,1 --- README.markdown | 2 +- extras/jsx_ex.erl | 2 +- include/jsx_common.hrl | 8 ++++---- include/jsx_decoder.hrl | 6 +++--- src/jsx.erl | 40 ++++++++++++++++++++++++++-------------- src/jsx_eep0018.erl | 2 +- src/jsx_format.erl | 2 +- src/jsx_verify.erl | 2 +- 8 files changed, 38 insertions(+), 26 deletions(-) diff --git a/README.markdown b/README.markdown index 8d4dcc2..413cf04 100644 --- a/README.markdown +++ b/README.markdown @@ -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) -> diff --git a/extras/jsx_ex.erl b/extras/jsx_ex.erl index 90f4350..8a317a3 100644 --- a/extras/jsx_ex.erl +++ b/extras/jsx_ex.erl @@ -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) -> diff --git a/include/jsx_common.hrl b/include/jsx_common.hrl index d2d4351..20787ae 100644 --- a/include/jsx_common.hrl +++ b/include/jsx_common.hrl @@ -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()}}. diff --git a/include/jsx_decoder.hrl b/include/jsx_decoder.hrl index 4237ec6..c046223 100644 --- a/include/jsx_decoder.hrl +++ b/include/jsx_decoder.hrl @@ -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 diff --git a/src/jsx.erl b/src/jsx.erl index 55f7c53..0f54758 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -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(). -parser(OptsList) -> +-spec parser(OptsList::jsx_opts()) -> jsx_decoder(). + +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(<>, 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]) -> diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index 28cefba..e2b6e93 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -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) diff --git a/src/jsx_format.erl b/src/jsx_format.erl index f6c0a12..d20cf1b 100644 --- a/src/jsx_format.erl +++ b/src/jsx_format.erl @@ -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{}), diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index 9bcdb12..dd8daa0 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -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), [[]])