api change, parser/0,1 deprecated, replaced with decoder/0,1
This commit is contained in:
parent
6cc28573b6
commit
b2d923cbcb
8 changed files with 38 additions and 26 deletions
|
@ -22,7 +22,7 @@ the following module
|
||||||
-export([simple_decode/1]).
|
-export([simple_decode/1]).
|
||||||
|
|
||||||
simple_decode(JSON) when is_binary(JSON) ->
|
simple_decode(JSON) when is_binary(JSON) ->
|
||||||
P = jsx:parser(),
|
P = jsx:decoder(),
|
||||||
decode(P(JSON), []).
|
decode(P(JSON), []).
|
||||||
|
|
||||||
decode({event, end_json, _Next}, Acc) ->
|
decode({event, end_json, _Next}, Acc) ->
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
-export([simple_decode/1]).
|
-export([simple_decode/1]).
|
||||||
|
|
||||||
simple_decode(JSON) when is_binary(JSON) ->
|
simple_decode(JSON) when is_binary(JSON) ->
|
||||||
P = jsx:parser(),
|
P = jsx:decoder(),
|
||||||
decode(P(JSON), []).
|
decode(P(JSON), []).
|
||||||
|
|
||||||
decode({event, end_json, _Next}, Acc) ->
|
decode({event, end_json, _Next}, Acc) ->
|
||||||
|
|
|
@ -64,11 +64,11 @@
|
||||||
|
|
||||||
|
|
||||||
%% this probably doesn't work properly
|
%% 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() ::
|
-type jsx_decoder_result() ::
|
||||||
{event, jsx_event(), fun(() -> jsx_parser_result())}
|
{event, jsx_event(), fun(() -> jsx_decoder_result())}
|
||||||
| {incomplete, jsx_parser()}
|
| {incomplete, jsx_decoder()}
|
||||||
| {error, {badjson, binary()}}.
|
| {error, {badjson, binary()}}.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
%% this file should take that into account
|
%% 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
|
%% opts record for decoder
|
||||||
-record(opts, {
|
-record(opts, {
|
||||||
|
@ -121,11 +121,11 @@
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
|
|
||||||
-export([parser/1]).
|
-export([decoder/1]).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parser(OptsList) ->
|
decoder(OptsList) ->
|
||||||
case parse_opts(OptsList) of
|
case parse_opts(OptsList) of
|
||||||
{error, badopt} -> {error, badopt}
|
{error, badopt} -> {error, badopt}
|
||||||
; Opts -> fun(JSON) -> start(JSON, [], Opts) end
|
; Opts -> fun(JSON) -> start(JSON, [], Opts) end
|
||||||
|
|
40
src/jsx.erl
40
src/jsx.erl
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
%% the core parser api
|
%% the core parser api
|
||||||
-export([parser/0, parser/1]).
|
-export([parser/0, parser/1]).
|
||||||
|
-export([decoder/0, decoder/1]).
|
||||||
-export([term_to_json/1, term_to_json/2]).
|
-export([term_to_json/1, term_to_json/2]).
|
||||||
-export([json_to_term/1, json_to_term/2]).
|
-export([json_to_term/1, json_to_term/2]).
|
||||||
-export([is_json/1, is_json/2]).
|
-export([is_json/1, is_json/2]).
|
||||||
|
@ -40,21 +41,32 @@
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
|
|
||||||
-spec parser() -> jsx_parser().
|
-spec parser() -> jsx_decoder().
|
||||||
|
|
||||||
parser() ->
|
parser() -> decoder([]).
|
||||||
parser([]).
|
|
||||||
|
|
||||||
|
|
||||||
-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
|
case proplists:get_value(encoding, OptsList, auto) of
|
||||||
utf8 -> jsx_utf8:parser(OptsList)
|
utf8 -> jsx_utf8:decoder(OptsList)
|
||||||
; utf16 -> jsx_utf16:parser(OptsList)
|
; utf16 -> jsx_utf16:decoder(OptsList)
|
||||||
; utf32 -> jsx_utf32:parser(OptsList)
|
; utf32 -> jsx_utf32:decoder(OptsList)
|
||||||
; {utf16, little} -> jsx_utf16le:parser(OptsList)
|
; {utf16, little} -> jsx_utf16le:decoder(OptsList)
|
||||||
; {utf32, little} -> jsx_utf32le:parser(OptsList)
|
; {utf32, little} -> jsx_utf32le:decoder(OptsList)
|
||||||
; auto -> jsx_utils:detect_encoding(OptsList)
|
; auto -> jsx_utils:detect_encoding(OptsList)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -196,7 +208,7 @@ parse_tests([], _Dir, Acc) ->
|
||||||
|
|
||||||
|
|
||||||
decode(JSON, Flags) ->
|
decode(JSON, Flags) ->
|
||||||
P = jsx:parser(Flags),
|
P = jsx:decoder(Flags),
|
||||||
decode_loop(P(JSON), []).
|
decode_loop(P(JSON), []).
|
||||||
|
|
||||||
decode_loop({event, end_json, _Next}, Acc) ->
|
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) ->
|
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(P(C), Rest, []).
|
||||||
|
|
||||||
incremental_decode_loop({incomplete, Next}, <<>>, Acc) ->
|
incremental_decode_loop({incomplete, Next}, <<>>, Acc) ->
|
||||||
|
@ -230,7 +242,7 @@ multi_decode_test_() ->
|
||||||
|
|
||||||
|
|
||||||
multi_decode(JSON, Flags) ->
|
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(P(JSON), [[]]).
|
||||||
|
|
||||||
multi_decode_loop({incomplete, _Next}, [[]|Acc]) ->
|
multi_decode_loop({incomplete, _Next}, [[]|Acc]) ->
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
-spec json_to_term(JSON::binary(), Opts::decoder_opts()) -> eep0018().
|
-spec json_to_term(JSON::binary(), Opts::decoder_opts()) -> eep0018().
|
||||||
|
|
||||||
json_to_term(JSON, Opts) ->
|
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
|
case proplists:get_value(strict, Opts, false) of
|
||||||
true -> collect_strict(P(JSON), [[]], Opts)
|
true -> collect_strict(P(JSON), [[]], Opts)
|
||||||
; false -> collect(P(JSON), [[]], Opts)
|
; false -> collect(P(JSON), [[]], Opts)
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist().
|
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist().
|
||||||
|
|
||||||
format(JSON, Opts) when is_binary(JSON) ->
|
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(fun() -> P(JSON) end, Opts);
|
||||||
format(F, OptsList) when is_function(F) ->
|
format(F, OptsList) when is_function(F) ->
|
||||||
Opts = parse_opts(OptsList, #format_opts{}),
|
Opts = parse_opts(OptsList, #format_opts{}),
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
|
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
|
||||||
|
|
||||||
is_json(JSON, Opts) ->
|
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
|
case proplists:get_value(strict, Opts, false) of
|
||||||
true -> collect_strict(P(JSON), [[]])
|
true -> collect_strict(P(JSON), [[]])
|
||||||
; false -> collect(P(JSON), [[]])
|
; false -> collect(P(JSON), [[]])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue