replace all incidences of opts with config

This commit is contained in:
alisdair sullivan 2013-02-12 11:54:42 -08:00
parent 2f47fdd642
commit ff6a83598b
9 changed files with 1436 additions and 1437 deletions

View file

@ -22,7 +22,6 @@
-module(jsx). -module(jsx).
-compile(export_all).
-export([encode/1, encode/2, decode/1, decode/2]). -export([encode/1, encode/2, decode/1, decode/2]).
-export([is_json/1, is_json/2, is_term/1, is_term/2]). -export([is_json/1, is_json/2, is_term/1, is_term/2]).
@ -53,24 +52,24 @@
-spec encode(Source::json_term()) -> json_text() | {incomplete, encoder()}. -spec encode(Source::json_term()) -> json_text() | {incomplete, encoder()}.
-spec encode(Source::json_term(), Opts::jsx_to_json:opts()) -> json_text() | {incomplete, encoder()}. -spec encode(Source::json_term(), Config::jsx_to_json:config()) -> json_text() | {incomplete, encoder()}.
encode(Source) -> encode(Source, []). encode(Source) -> encode(Source, []).
encode(Source, Opts) -> jsx_to_json:to_json(Source, Opts). encode(Source, Config) -> jsx_to_json:to_json(Source, Config).
%% old api, alias for encode/x %% old api, alias for encode/x
to_json(Source) -> encode(Source, []). to_json(Source) -> encode(Source, []).
to_json(Source, Opts) -> encode(Source, Opts). to_json(Source, Config) -> encode(Source, Config).
term_to_json(Source) -> encode(Source, []). term_to_json(Source) -> encode(Source, []).
term_to_json(Source, Opts) -> encode(Source, Opts). term_to_json(Source, Config) -> encode(Source, Config).
-spec format(Source::json_text()) -> json_text() | {incomplete, decoder()}. -spec format(Source::json_text()) -> json_text() | {incomplete, decoder()}.
-spec format(Source::json_text(), Opts::jsx_to_json:opts()) -> json_text() | {incomplete, decoder()}. -spec format(Source::json_text(), Config::jsx_to_json:config()) -> json_text() | {incomplete, decoder()}.
format(Source) -> format(Source, []). format(Source) -> format(Source, []).
format(Source, Opts) -> jsx_to_json:format(Source, Opts). format(Source, Config) -> jsx_to_json:format(Source, Config).
-spec minify(Source::json_text()) -> json_text() | {incomplete, decoder()}. -spec minify(Source::json_text()) -> json_text() | {incomplete, decoder()}.
@ -84,45 +83,45 @@ prettify(Source) -> format(Source, [space, {indent, 2}]).
-spec decode(Source::json_text()) -> json_term() | {incomplete, decoder()}. -spec decode(Source::json_text()) -> json_term() | {incomplete, decoder()}.
-spec decode(Source::json_text(), Opts::jsx_to_term:opts()) -> json_term() | {incomplete, decoder()}. -spec decode(Source::json_text(), Config::jsx_to_term:config()) -> json_term() | {incomplete, decoder()}.
decode(Source) -> decode(Source, []). decode(Source) -> decode(Source, []).
decode(Source, Opts) -> jsx_to_term:to_term(Source, Opts). decode(Source, Config) -> jsx_to_term:to_term(Source, Config).
%% old api, alias for to_term/x %% old api, alias for to_term/x
to_term(Source) -> decode(Source, []). to_term(Source) -> decode(Source, []).
to_term(Source, Opts) -> decode(Source, Opts). to_term(Source, Config) -> decode(Source, Config).
json_to_term(Source) -> decode(Source, []). json_to_term(Source) -> decode(Source, []).
json_to_term(Source, Opts) -> decode(Source, Opts). json_to_term(Source, Config) -> decode(Source, Config).
-spec is_json(Source::any()) -> true | false. -spec is_json(Source::any()) -> true | false.
-spec is_json(Source::any(), Opts::jsx_verify:opts()) -> true | false. -spec is_json(Source::any(), Config::jsx_verify:config()) -> true | false.
is_json(Source) -> is_json(Source, []). is_json(Source) -> is_json(Source, []).
is_json(Source, Opts) -> jsx_verify:is_json(Source, Opts). is_json(Source, Config) -> jsx_verify:is_json(Source, Config).
-spec is_term(Source::any()) -> true | false. -spec is_term(Source::any()) -> true | false.
-spec is_term(Source::any(), Opts::jsx_verify:opts()) -> true | false. -spec is_term(Source::any(), Config::jsx_verify:config()) -> true | false.
is_term(Source) -> is_term(Source, []). is_term(Source) -> is_term(Source, []).
is_term(Source, Opts) -> jsx_verify:is_term(Source, Opts). is_term(Source, Config) -> jsx_verify:is_term(Source, Config).
-type decoder() :: fun((json_text() | end_stream) -> any()). -type decoder() :: fun((json_text() | end_stream) -> any()).
-spec decoder(Handler::module(), State::any(), Opts::list()) -> decoder(). -spec decoder(Handler::module(), State::any(), Config::list()) -> decoder().
decoder(Handler, State, Opts) -> jsx_decoder:decoder(Handler, State, Opts). decoder(Handler, State, Config) -> jsx_decoder:decoder(Handler, State, Config).
-type encoder() :: fun((json_term() | end_stream) -> any()). -type encoder() :: fun((json_term() | end_stream) -> any()).
-spec encoder(Handler::module(), State::any(), Opts::list()) -> encoder(). -spec encoder(Handler::module(), State::any(), Config::list()) -> encoder().
encoder(Handler, State, Opts) -> jsx_encoder:encoder(Handler, State, Opts). encoder(Handler, State, Config) -> jsx_encoder:encoder(Handler, State, Config).
-type token() :: [token()] -type token() :: [token()]
@ -149,6 +148,6 @@ encoder(Handler, State, Opts) -> jsx_encoder:encoder(Handler, State, Opts).
-type parser() :: fun((token() | end_stream) -> any()). -type parser() :: fun((token() | end_stream) -> any()).
-spec parser(Handler::module(), State::any(), Opts::list()) -> parser(). -spec parser(Handler::module(), State::any(), Config::list()) -> parser().
parser(Handler, State, Opts) -> jsx_parser:parser(Handler, State, Opts). parser(Handler, State, Config) -> jsx_parser:parser(Handler, State, Config).

View file

@ -1,4 +1,4 @@
-record(opts, { -record(config, {
replaced_bad_utf8 = false, replaced_bad_utf8 = false,
escaped_forward_slashes = false, escaped_forward_slashes = false,
single_quoted_strings = false, single_quoted_strings = false,

File diff suppressed because it is too large Load diff

View file

@ -25,20 +25,20 @@
-export([encoder/3, pre_encode/2]). -export([encoder/3, pre_encode/2]).
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder(). -spec encoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:encoder().
encoder(Handler, State, Opts) -> encoder(Handler, State, Config) ->
fun(JSON) -> fun(JSON) ->
start( start(
JSON, JSON,
{Handler, Handler:init(State)}, {Handler, Handler:init(State)},
jsx_utils:parse_opts(Opts) jsx_utils:parse_config(Config)
) )
end. end.
-include("jsx_opts.hrl"). -include("jsx_config.hrl").
-ifndef(error). -ifndef(error).
@ -48,87 +48,87 @@ encoder(Handler, State, Opts) ->
-endif. -endif.
start(Term, {Handler, State}, Opts) -> start(Term, {Handler, State}, Config) ->
Handler:handle_event(end_json, value(pre_encode(Term, Opts), {Handler, State}, Opts)). Handler:handle_event(end_json, value(pre_encode(Term, Config), {Handler, State}, Config)).
value(String, {Handler, State}, Opts) when is_binary(String) -> value(String, {Handler, State}, Config) when is_binary(String) ->
Handler:handle_event({string, clean_string(String, Opts)}, State); Handler:handle_event({string, clean_string(String, Config)}, State);
value(Float, {Handler, State}, _Opts) when is_float(Float) -> value(Float, {Handler, State}, _Config) when is_float(Float) ->
Handler:handle_event({float, Float}, State); Handler:handle_event({float, Float}, State);
value(Int, {Handler, State}, _Opts) when is_integer(Int) -> value(Int, {Handler, State}, _Config) when is_integer(Int) ->
Handler:handle_event({integer, Int}, State); Handler:handle_event({integer, Int}, State);
value(Literal, {Handler, State}, _Opts) value(Literal, {Handler, State}, _Config)
when Literal == true; Literal == false; Literal == null -> when Literal == true; Literal == false; Literal == null ->
Handler:handle_event({literal, Literal}, State); Handler:handle_event({literal, Literal}, State);
value([{}], {Handler, State}, _Opts) -> value([{}], {Handler, State}, _Config) ->
Handler:handle_event(end_object, Handler:handle_event(start_object, State)); Handler:handle_event(end_object, Handler:handle_event(start_object, State));
value([], {Handler, State}, _Opts) -> value([], {Handler, State}, _Config) ->
Handler:handle_event(end_array, Handler:handle_event(start_array, State)); Handler:handle_event(end_array, Handler:handle_event(start_array, State));
value([Tuple|_] = List, Handler, Opts) when is_tuple(Tuple) -> value([Tuple|_] = List, Handler, Config) when is_tuple(Tuple) ->
list_or_object(List, Handler, Opts); list_or_object(List, Handler, Config);
value(List, Handler, Opts) when is_list(List) -> value(List, Handler, Config) when is_list(List) ->
list_or_object(List, Handler, Opts); list_or_object(List, Handler, Config);
value(Term, Handler, Opts) -> ?error([Term, Handler, Opts]). value(Term, Handler, Config) -> ?error([Term, Handler, Config]).
list_or_object([Term|Rest], {Handler, State}, Opts) -> list_or_object([Term|Rest], {Handler, State}, Config) ->
case pre_encode(Term, Opts) of case pre_encode(Term, Config) of
{K, V} -> {K, V} ->
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Opts) object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Config)
; T -> ; T ->
list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Opts) list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Config)
end. end.
object([{Key, Value}, Next|Rest], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) -> object([{Key, Value}, Next|Rest], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
V = pre_encode(Value, Opts), V = pre_encode(Value, Config),
object( object(
[pre_encode(Next, Opts)|Rest], [pre_encode(Next, Config)|Rest],
{ {
Handler, Handler,
value( value(
V, V,
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)}, {Handler, Handler:handle_event({key, clean_string(fix_key(Key), Config)}, State)},
Opts Config
) )
}, },
Opts Config
); );
object([{Key, Value}], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) -> object([{Key, Value}], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
object( object(
[], [],
{ {
Handler, Handler,
value( value(
pre_encode(Value, Opts), pre_encode(Value, Config),
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)}, {Handler, Handler:handle_event({key, clean_string(fix_key(Key), Config)}, State)},
Opts Config
) )
}, },
Opts Config
); );
object([], {Handler, State}, _Opts) -> Handler:handle_event(end_object, State); object([], {Handler, State}, _Config) -> Handler:handle_event(end_object, State);
object(Term, Handler, Opts) -> ?error([Term, Handler, Opts]). object(Term, Handler, Config) -> ?error([Term, Handler, Config]).
list([Value, Next|Rest], {Handler, State}, Opts) -> list([Value, Next|Rest], {Handler, State}, Config) ->
list([pre_encode(Next, Opts)|Rest], {Handler, value(Value, {Handler, State}, Opts)}, Opts); list([pre_encode(Next, Config)|Rest], {Handler, value(Value, {Handler, State}, Config)}, Config);
list([Value], {Handler, State}, Opts) -> list([Value], {Handler, State}, Config) ->
list([], {Handler, value(Value, {Handler, State}, Opts)}, Opts); list([], {Handler, value(Value, {Handler, State}, Config)}, Config);
list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State); list([], {Handler, State}, _Config) -> Handler:handle_event(end_array, State);
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]). list(Term, Handler, Config) -> ?error([Term, Handler, Config]).
pre_encode(Value, #opts{pre_encode=false}) -> io:format("~p~n", [Value]), Value; pre_encode(Value, #config{pre_encode=false}) -> io:format("~p~n", [Value]), Value;
pre_encode(Value, Opts) -> (Opts#opts.pre_encode)(Value). pre_encode(Value, Config) -> (Config#config.pre_encode)(Value).
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8)); fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
fix_key(Key) when is_binary(Key) -> Key. fix_key(Key) when is_binary(Key) -> Key.
clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts). clean_string(Bin, Config) -> jsx_utils:clean_string(Bin, Config).
@ -142,7 +142,7 @@ encode_test_() ->
{ {
Title, ?_assertEqual( Title, ?_assertEqual(
Events ++ [end_json], Events ++ [end_json],
start(Term, {jsx, []}, #opts{}) start(Term, {jsx, []}, #config{})
) )
} || {Title, _, Term, Events} <- Data } || {Title, _, Term, Events} <- Data
]. ].

View file

@ -26,13 +26,13 @@
-export([parser/3]). -export([parser/3]).
-spec parser(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:parser(). -spec parser(Handler::module(), State::any(), Config::jsx:config()) -> jsx:parser().
parser(Handler, State, Opts) -> parser(Handler, State, Config) ->
fun(Tokens) -> value(Tokens, {Handler, Handler:init(State)}, [], jsx_utils:parse_opts(Opts)) end. fun(Tokens) -> value(Tokens, {Handler, Handler:init(State)}, [], jsx_utils:parse_config(Config)) end.
-include("jsx_opts.hrl"). -include("jsx_config.hrl").
%% error, incomplete and event macros %% error, incomplete and event macros
@ -44,124 +44,124 @@ parser(Handler, State, Opts) ->
-ifndef(incomplete). -ifndef(incomplete).
-define(incomplete(State, Handler, Stack, Opts), -define(incomplete(State, Handler, Stack, Config),
{incomplete, fun(end_stream) -> {incomplete, fun(end_stream) ->
case State([end_json], case State([end_json],
Handler, Handler,
Stack, Stack,
Opts) of Config) of
{incomplete, _} -> ?error([Handler, Stack, Opts]) {incomplete, _} -> ?error([Handler, Stack, Config])
; Events -> Events ; Events -> Events
end end
; (Tokens) -> ; (Tokens) ->
State(Tokens, Handler, Stack, Opts) State(Tokens, Handler, Stack, Config)
end end
} }
). ).
-endif. -endif.
handle_event([], Handler, _Opts) -> Handler; handle_event([], Handler, _Config) -> Handler;
handle_event([Event|Rest], Handler, Opts) -> handle_event(Rest, handle_event(Event, Handler, Opts), Opts); handle_event([Event|Rest], Handler, Config) -> handle_event(Rest, handle_event(Event, Handler, Config), Config);
handle_event(Event, {Handler, State}, _Opts) -> {Handler, Handler:handle_event(Event, State)}. handle_event(Event, {Handler, State}, _Config) -> {Handler, Handler:handle_event(Event, State)}.
value([start_object|Tokens], Handler, Stack, Opts) -> value([start_object|Tokens], Handler, Stack, Config) ->
object(Tokens, handle_event(start_object, Handler, Opts), [object|Stack], Opts); object(Tokens, handle_event(start_object, Handler, Config), [object|Stack], Config);
value([start_array|Tokens], Handler, Stack, Opts) -> value([start_array|Tokens], Handler, Stack, Config) ->
array(Tokens, handle_event(start_array, Handler, Opts), [array|Stack], Opts); array(Tokens, handle_event(start_array, Handler, Config), [array|Stack], Config);
value([{literal, true}|Tokens], Handler, [], Opts) -> value([{literal, true}|Tokens], Handler, [], Config) ->
done(Tokens, handle_event({literal, true}, Handler, Opts), [], Opts); done(Tokens, handle_event({literal, true}, Handler, Config), [], Config);
value([{literal, false}|Tokens], Handler, [], Opts) -> value([{literal, false}|Tokens], Handler, [], Config) ->
done(Tokens, handle_event({literal, false}, Handler, Opts), [], Opts); done(Tokens, handle_event({literal, false}, Handler, Config), [], Config);
value([{literal, null}|Tokens], Handler, [], Opts) -> value([{literal, null}|Tokens], Handler, [], Config) ->
done(Tokens, handle_event({literal, null}, Handler, Opts), [], Opts); done(Tokens, handle_event({literal, null}, Handler, Config), [], Config);
value([{literal, true}|Tokens], Handler, Stack, Opts) -> value([{literal, true}|Tokens], Handler, Stack, Config) ->
maybe_done(Tokens, handle_event({literal, true}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({literal, true}, Handler, Config), Stack, Config);
value([{literal, false}|Tokens], Handler, Stack, Opts) -> value([{literal, false}|Tokens], Handler, Stack, Config) ->
maybe_done(Tokens, handle_event({literal, false}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({literal, false}, Handler, Config), Stack, Config);
value([{literal, null}|Tokens], Handler, Stack, Opts) -> value([{literal, null}|Tokens], Handler, Stack, Config) ->
maybe_done(Tokens, handle_event({literal, null}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({literal, null}, Handler, Config), Stack, Config);
value([Literal|Tokens], Handler, Stack, Opts) when Literal == true; Literal == false; Literal == null -> value([Literal|Tokens], Handler, Stack, Config) when Literal == true; Literal == false; Literal == null ->
value([{literal, Literal}] ++ Tokens, Handler, Stack, Opts); value([{literal, Literal}] ++ Tokens, Handler, Stack, Config);
value([{integer, Number}|Tokens], Handler, [], Opts) when is_integer(Number) -> value([{integer, Number}|Tokens], Handler, [], Config) when is_integer(Number) ->
done(Tokens, handle_event({integer, Number}, Handler, Opts), [], Opts); done(Tokens, handle_event({integer, Number}, Handler, Config), [], Config);
value([{float, Number}|Tokens], Handler, [], Opts) when is_float(Number) -> value([{float, Number}|Tokens], Handler, [], Config) when is_float(Number) ->
done(Tokens, handle_event({float, Number}, Handler, Opts), [], Opts); done(Tokens, handle_event({float, Number}, Handler, Config), [], Config);
value([{integer, Number}|Tokens], Handler, Stack, Opts) when is_integer(Number) -> value([{integer, Number}|Tokens], Handler, Stack, Config) when is_integer(Number) ->
maybe_done(Tokens, handle_event({integer, Number}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({integer, Number}, Handler, Config), Stack, Config);
value([{float, Number}|Tokens], Handler, Stack, Opts) when is_float(Number) -> value([{float, Number}|Tokens], Handler, Stack, Config) when is_float(Number) ->
maybe_done(Tokens, handle_event({float, Number}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({float, Number}, Handler, Config), Stack, Config);
value([{number, Number}|Tokens], Handler, Stack, Opts) when is_integer(Number) -> value([{number, Number}|Tokens], Handler, Stack, Config) when is_integer(Number) ->
value([{integer, Number}] ++ Tokens, Handler, Stack, Opts); value([{integer, Number}] ++ Tokens, Handler, Stack, Config);
value([{number, Number}|Tokens], Handler, Stack, Opts) when is_float(Number) -> value([{number, Number}|Tokens], Handler, Stack, Config) when is_float(Number) ->
value([{float, Number}] ++ Tokens, Handler, Stack, Opts); value([{float, Number}] ++ Tokens, Handler, Stack, Config);
value([Number|Tokens], Handler, Stack, Opts) when is_integer(Number) -> value([Number|Tokens], Handler, Stack, Config) when is_integer(Number) ->
value([{integer, Number}] ++ Tokens, Handler, Stack, Opts); value([{integer, Number}] ++ Tokens, Handler, Stack, Config);
value([Number|Tokens], Handler, Stack, Opts) when is_float(Number) -> value([Number|Tokens], Handler, Stack, Config) when is_float(Number) ->
value([{float, Number}] ++ Tokens, Handler, Stack, Opts); value([{float, Number}] ++ Tokens, Handler, Stack, Config);
value([{string, String}|Tokens], Handler, [], Opts) when is_binary(String) -> value([{string, String}|Tokens], Handler, [], Config) when is_binary(String) ->
done(Tokens, handle_event({string, clean_string(String, Opts)}, Handler, Opts), [], Opts); done(Tokens, handle_event({string, clean_string(String, Config)}, Handler, Config), [], Config);
value([{string, String}|Tokens], Handler, Stack, Opts) when is_binary(String) -> value([{string, String}|Tokens], Handler, Stack, Config) when is_binary(String) ->
maybe_done(Tokens, handle_event({string, clean_string(String, Opts)}, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event({string, clean_string(String, Config)}, Handler, Config), Stack, Config);
value([String|Tokens], Handler, Stack, Opts) when is_binary(String) -> value([String|Tokens], Handler, Stack, Config) when is_binary(String) ->
value([{string, String}] ++ Tokens, Handler, Stack, Opts); value([{string, String}] ++ Tokens, Handler, Stack, Config);
value([], Handler, Stack, Opts) -> value([], Handler, Stack, Config) ->
?incomplete(value, Handler, Stack, Opts); ?incomplete(value, Handler, Stack, Config);
value(BadTokens, Handler, Stack, Opts) when is_list(BadTokens) -> value(BadTokens, Handler, Stack, Config) when is_list(BadTokens) ->
?error([BadTokens, Handler, Stack, Opts]); ?error([BadTokens, Handler, Stack, Config]);
value(Token, Handler, Stack, Opts) -> value(Token, Handler, Stack, Config) ->
value([Token], Handler, Stack, Opts). value([Token], Handler, Stack, Config).
object([end_object|Tokens], Handler, [object|Stack], Opts) -> object([end_object|Tokens], Handler, [object|Stack], Config) ->
maybe_done(Tokens, handle_event(end_object, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event(end_object, Handler, Config), Stack, Config);
object([{key, Key}|Tokens], Handler, Stack, Opts) when is_atom(Key); is_binary(Key) -> object([{key, Key}|Tokens], Handler, Stack, Config) when is_atom(Key); is_binary(Key) ->
value(Tokens, handle_event({key, clean_string(fix_key(Key), Opts)}, Handler, Opts), Stack, Opts); value(Tokens, handle_event({key, clean_string(fix_key(Key), Config)}, Handler, Config), Stack, Config);
object([Key|Tokens], Handler, Stack, Opts) when is_atom(Key); is_binary(Key) -> object([Key|Tokens], Handler, Stack, Config) when is_atom(Key); is_binary(Key) ->
value(Tokens, handle_event({key, clean_string(fix_key(Key), Opts)}, Handler, Opts), Stack, Opts); value(Tokens, handle_event({key, clean_string(fix_key(Key), Config)}, Handler, Config), Stack, Config);
object([], Handler, Stack, Opts) -> object([], Handler, Stack, Config) ->
?incomplete(object, Handler, Stack, Opts); ?incomplete(object, Handler, Stack, Config);
object(BadTokens, Handler, Stack, Opts) when is_list(BadTokens) -> object(BadTokens, Handler, Stack, Config) when is_list(BadTokens) ->
?error([BadTokens, Handler, Stack, Opts]); ?error([BadTokens, Handler, Stack, Config]);
object(Token, Handler, Stack, Opts) -> object(Token, Handler, Stack, Config) ->
object([Token], Handler, Stack, Opts). object([Token], Handler, Stack, Config).
array([end_array|Tokens], Handler, [array|Stack], Opts) -> array([end_array|Tokens], Handler, [array|Stack], Config) ->
maybe_done(Tokens, handle_event(end_array, Handler, Opts), Stack, Opts); maybe_done(Tokens, handle_event(end_array, Handler, Config), Stack, Config);
array([], Handler, Stack, Opts) -> array([], Handler, Stack, Config) ->
?incomplete(array, Handler, Stack, Opts); ?incomplete(array, Handler, Stack, Config);
array(Tokens, Handler, Stack, Opts) when is_list(Tokens) -> array(Tokens, Handler, Stack, Config) when is_list(Tokens) ->
value(Tokens, Handler, Stack, Opts); value(Tokens, Handler, Stack, Config);
array(Token, Handler, Stack, Opts) -> array(Token, Handler, Stack, Config) ->
array([Token], Handler, Stack, Opts). array([Token], Handler, Stack, Config).
maybe_done([end_json], Handler, [], Opts) -> maybe_done([end_json], Handler, [], Config) ->
done([], Handler, [], Opts); done([], Handler, [], Config);
maybe_done(Tokens, Handler, [object|_] = Stack, Opts) when is_list(Tokens) -> maybe_done(Tokens, Handler, [object|_] = Stack, Config) when is_list(Tokens) ->
object(Tokens, Handler, Stack, Opts); object(Tokens, Handler, Stack, Config);
maybe_done(Tokens, Handler, [array|_] = Stack, Opts) when is_list(Tokens) -> maybe_done(Tokens, Handler, [array|_] = Stack, Config) when is_list(Tokens) ->
array(Tokens, Handler, Stack, Opts); array(Tokens, Handler, Stack, Config);
maybe_done([], Handler, Stack, Opts) -> maybe_done([], Handler, Stack, Config) ->
?incomplete(maybe_done, Handler, Stack, Opts); ?incomplete(maybe_done, Handler, Stack, Config);
maybe_done(BadTokens, Handler, Stack, Opts) when is_list(BadTokens) -> maybe_done(BadTokens, Handler, Stack, Config) when is_list(BadTokens) ->
?error([BadTokens, Handler, Stack, Opts]); ?error([BadTokens, Handler, Stack, Config]);
maybe_done(Token, Handler, Stack, Opts) -> maybe_done(Token, Handler, Stack, Config) ->
maybe_done([Token], Handler, Stack, Opts). maybe_done([Token], Handler, Stack, Config).
done(Tokens, Handler, [], Opts) when Tokens == [end_json]; Tokens == [] -> done(Tokens, Handler, [], Config) when Tokens == [end_json]; Tokens == [] ->
{_, State} = handle_event(end_json, Handler, Opts), {_, State} = handle_event(end_json, Handler, Config),
State; State;
done(BadTokens, Handler, Stack, Opts) when is_list(BadTokens) -> done(BadTokens, Handler, Stack, Config) when is_list(BadTokens) ->
?error([BadTokens, Handler, Stack, Opts]); ?error([BadTokens, Handler, Stack, Config]);
done(Token, Handler, Stack, Opts) -> done(Token, Handler, Stack, Config) ->
done([Token], Handler, Stack, Opts). done([Token], Handler, Stack, Config).
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8)); fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
fix_key(Key) when is_binary(Key) -> Key. fix_key(Key) when is_binary(Key) -> Key.
clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts). clean_string(Bin, Config) -> jsx_utils:clean_string(Bin, Config).
@ -176,7 +176,7 @@ decode_test_() ->
{ {
Title, ?_assertEqual( Title, ?_assertEqual(
Events ++ [end_json], Events ++ [end_json],
value(Events ++ [end_json], {jsx, []}, [], #opts{}) value(Events ++ [end_json], {jsx, []}, [], #config{})
) )
} || {Title, _, _, Events} <- Data } || {Title, _, _, Events} <- Data
]. ].

View file

@ -27,49 +27,49 @@
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-record(opts, { -record(config, {
space = 0, space = 0,
indent = 0, indent = 0,
depth = 0 depth = 0
}). }).
-type opts() :: list(). -type config() :: list().
-spec to_json(Source::any(), Opts::opts()) -> binary(). -spec to_json(Source::any(), Config::config()) -> binary().
to_json(Source, Opts) when is_list(Opts) -> to_json(Source, Config) when is_list(Config) ->
(jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts ++ [escaped_strings])))(Source). (jsx:encoder(?MODULE, Config, jsx_utils:extract_config(Config ++ [escaped_strings])))(Source).
-spec format(Source::binary(), Opts::opts()) -> binary(). -spec format(Source::binary(), Config::config()) -> binary().
format(Source, Opts) when is_binary(Source) andalso is_list(Opts) -> format(Source, Config) when is_binary(Source) andalso is_list(Config) ->
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts ++ [escaped_strings])))(Source). (jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config ++ [escaped_strings])))(Source).
parse_opts(Opts) -> parse_opts(Opts, #opts{}). parse_config(Config) -> parse_config(Config, #config{}).
parse_opts([{space, Val}|Rest], Opts) when is_integer(Val), Val > 0 -> parse_config([{space, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
parse_opts(Rest, Opts#opts{space = Val}); parse_config(Rest, Config#config{space = Val});
parse_opts([space|Rest], Opts) -> parse_config([space|Rest], Config) ->
parse_opts(Rest, Opts#opts{space = 1}); parse_config(Rest, Config#config{space = 1});
parse_opts([{indent, Val}|Rest], Opts) when is_integer(Val), Val > 0 -> parse_config([{indent, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
parse_opts(Rest, Opts#opts{indent = Val}); parse_config(Rest, Config#config{indent = Val});
parse_opts([indent|Rest], Opts) -> parse_config([indent|Rest], Config) ->
parse_opts(Rest, Opts#opts{indent = 1}); parse_config(Rest, Config#config{indent = 1});
parse_opts([{K, _}|Rest] = Options, Opts) -> parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([K|Rest] = Options, Opts) -> parse_config([K|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([], Opts) -> parse_config([], Config) ->
Opts. Config.
@ -85,97 +85,97 @@ parse_opts([], Opts) ->
init(Opts) -> {start, [], parse_opts(Opts)}. init(Config) -> {start, [], parse_config(Config)}.
handle_event(Event, {start, Acc, Opts}) -> handle_event(Event, {start, Acc, Config}) ->
case Event of case Event of
{Type, Value} -> {[], [Acc, encode(Type, Value, Opts)], Opts} {Type, Value} -> {[], [Acc, encode(Type, Value, Config)], Config}
; start_object -> {[object_start], [Acc, ?start_object], Opts} ; start_object -> {[object_start], [Acc, ?start_object], Config}
; start_array -> {[array_start], [Acc, ?start_array], Opts} ; start_array -> {[array_start], [Acc, ?start_array], Config}
end; end;
handle_event(Event, {[object_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) -> handle_event(Event, {[object_start|Stack], Acc, OldConfig = #config{depth = Depth}}) ->
Opts = OldOpts#opts{depth = Depth + 1}, Config = OldConfig#config{depth = Depth + 1},
case Event of case Event of
{key, Key} -> {key, Key} ->
{[object_value|Stack], [Acc, indent(Opts), encode(string, Key, Opts), ?colon, space(Opts)], Opts} {[object_value|Stack], [Acc, indent(Config), encode(string, Key, Config), ?colon, space(Config)], Config}
; end_object -> ; end_object ->
{Stack, [Acc, ?end_object], OldOpts} {Stack, [Acc, ?end_object], OldConfig}
end; end;
handle_event(Event, {[object_value|Stack], Acc, Opts}) -> handle_event(Event, {[object_value|Stack], Acc, Config}) ->
case Event of case Event of
{Type, Value} when Type == string; Type == literal; {Type, Value} when Type == string; Type == literal;
Type == integer; Type == float -> Type == integer; Type == float ->
{[key|Stack], [Acc, encode(Type, Value, Opts)], Opts} {[key|Stack], [Acc, encode(Type, Value, Config)], Config}
; start_object -> {[object_start, key|Stack], [Acc, ?start_object], Opts} ; start_object -> {[object_start, key|Stack], [Acc, ?start_object], Config}
; start_array -> {[array_start, key|Stack], [Acc, ?start_array], Opts} ; start_array -> {[array_start, key|Stack], [Acc, ?start_array], Config}
end; end;
handle_event(Event, {[key|Stack], Acc, Opts = #opts{depth = Depth}}) -> handle_event(Event, {[key|Stack], Acc, Config = #config{depth = Depth}}) ->
case Event of case Event of
{key, Key} -> {key, Key} ->
{[object_value|Stack], [Acc, ?comma, indent_or_space(Opts), encode(string, Key, Opts), ?colon, space(Opts)], Opts} {[object_value|Stack], [Acc, ?comma, indent_or_space(Config), encode(string, Key, Config), ?colon, space(Config)], Config}
; end_object -> ; end_object ->
NewOpts = Opts#opts{depth = Depth - 1}, NewConfig = Config#config{depth = Depth - 1},
{Stack, [Acc, indent(NewOpts), ?end_object], NewOpts} {Stack, [Acc, indent(NewConfig), ?end_object], NewConfig}
end; end;
handle_event(Event, {[array_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) -> handle_event(Event, {[array_start|Stack], Acc, OldConfig = #config{depth = Depth}}) ->
Opts = OldOpts#opts{depth = Depth + 1}, Config = OldConfig#config{depth = Depth + 1},
case Event of case Event of
{Type, Value} when Type == string; Type == literal; {Type, Value} when Type == string; Type == literal;
Type == integer; Type == float -> Type == integer; Type == float ->
{[array|Stack], [Acc, indent(Opts), encode(Type, Value, Opts)], Opts} {[array|Stack], [Acc, indent(Config), encode(Type, Value, Config)], Config}
; start_object -> {[object_start, array|Stack], [Acc, indent(Opts), ?start_object], Opts} ; start_object -> {[object_start, array|Stack], [Acc, indent(Config), ?start_object], Config}
; start_array -> {[array_start, array|Stack], [Acc, indent(Opts), ?start_array], Opts} ; start_array -> {[array_start, array|Stack], [Acc, indent(Config), ?start_array], Config}
; end_array -> {Stack, [Acc, ?end_array], OldOpts} ; end_array -> {Stack, [Acc, ?end_array], OldConfig}
end; end;
handle_event(Event, {[array|Stack], Acc, Opts = #opts{depth = Depth}}) -> handle_event(Event, {[array|Stack], Acc, Config = #config{depth = Depth}}) ->
case Event of case Event of
{Type, Value} when Type == string; Type == literal; {Type, Value} when Type == string; Type == literal;
Type == integer; Type == float -> Type == integer; Type == float ->
{[array|Stack], [Acc, ?comma, indent_or_space(Opts), encode(Type, Value, Opts)], Opts} {[array|Stack], [Acc, ?comma, indent_or_space(Config), encode(Type, Value, Config)], Config}
; end_array -> ; end_array ->
NewOpts = Opts#opts{depth = Depth - 1}, NewConfig = Config#config{depth = Depth - 1},
{Stack, [Acc, indent(NewOpts), ?end_array], NewOpts} {Stack, [Acc, indent(NewConfig), ?end_array], NewConfig}
; start_object -> {[object_start, array|Stack], [Acc, ?comma, indent_or_space(Opts), ?start_object], Opts} ; start_object -> {[object_start, array|Stack], [Acc, ?comma, indent_or_space(Config), ?start_object], Config}
; start_array -> {[array_start, array|Stack], [Acc, ?comma, indent_or_space(Opts), ?start_array], Opts} ; start_array -> {[array_start, array|Stack], [Acc, ?comma, indent_or_space(Config), ?start_array], Config}
end; end;
handle_event(end_json, {[], Acc, _Opts}) -> unicode:characters_to_binary(Acc, utf8). handle_event(end_json, {[], Acc, _Config}) -> unicode:characters_to_binary(Acc, utf8).
encode(string, String, _Opts) -> encode(string, String, _Config) ->
[?quote, String, ?quote]; [?quote, String, ?quote];
encode(literal, Literal, _Opts) -> encode(literal, Literal, _Config) ->
erlang:atom_to_list(Literal); erlang:atom_to_list(Literal);
encode(integer, Integer, _Opts) -> encode(integer, Integer, _Config) ->
erlang:integer_to_list(Integer); erlang:integer_to_list(Integer);
encode(float, Float, _Opts) -> encode(float, Float, _Config) ->
[Output] = io_lib:format("~p", [Float]), Output. [Output] = io_lib:format("~p", [Float]), Output.
space(Opts) -> space(Config) ->
case Opts#opts.space of case Config#config.space of
0 -> [] 0 -> []
; X when X > 0 -> binary:copy(?space, X) ; X when X > 0 -> binary:copy(?space, X)
end. end.
indent(Opts) -> indent(Config) ->
case Opts#opts.indent of case Config#config.indent of
0 -> [] 0 -> []
; X when X > 0 -> ; X when X > 0 ->
Indent = binary:copy(?space, X), Indent = binary:copy(?space, X),
indent(Indent, Opts#opts.depth, [?newline]) indent(Indent, Config#config.depth, [?newline])
end. end.
indent(_Indent, 0, Acc) -> Acc; indent(_Indent, 0, Acc) -> Acc;
indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]). indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]).
indent_or_space(Opts) -> indent_or_space(Config) ->
case Opts#opts.indent > 0 of case Config#config.indent > 0 of
true -> indent(Opts) true -> indent(Config)
; false -> space(Opts) ; false -> space(Config)
end. end.
@ -185,56 +185,56 @@ indent_or_space(Opts) ->
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
opts_test_() -> config_test_() ->
[ [
{"empty opts", ?_assertEqual(#opts{}, parse_opts([]))}, {"empty config", ?_assertEqual(#config{}, parse_config([]))},
{"unspecified indent/space", ?_assertEqual( {"unspecified indent/space", ?_assertEqual(
#opts{space=1, indent=1}, #config{space=1, indent=1},
parse_opts([space, indent]) parse_config([space, indent])
)}, )},
{"specific indent", ?_assertEqual( {"specific indent", ?_assertEqual(
#opts{indent=4}, #config{indent=4},
parse_opts([{indent, 4}]) parse_config([{indent, 4}])
)}, )},
{"specific space", ?_assertEqual( {"specific space", ?_assertEqual(
#opts{space=2}, #config{space=2},
parse_opts([{space, 2}]) parse_config([{space, 2}])
)}, )},
{"specific space and indent", ?_assertEqual( {"specific space and indent", ?_assertEqual(
#opts{space=2, indent=2}, #config{space=2, indent=2},
parse_opts([{space, 2}, {indent, 2}]) parse_config([{space, 2}, {indent, 2}])
)}, )},
{"invalid opt flag", ?_assertError(badarg, parse_opts([error]))}, {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
{"invalid opt tuple", ?_assertError(badarg, parse_opts([{error, true}]))} {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
]. ].
space_test_() -> space_test_() ->
[ [
{"no space", ?_assertEqual([], space(#opts{space=0}))}, {"no space", ?_assertEqual([], space(#config{space=0}))},
{"one space", ?_assertEqual(<<" ">>, space(#opts{space=1}))}, {"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))},
{"four spaces", ?_assertEqual(<<" ">>, space(#opts{space=4}))} {"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))}
]. ].
indent_test_() -> indent_test_() ->
[ [
{"no indent", ?_assertEqual([], indent(#opts{indent=0, depth=1}))}, {"no indent", ?_assertEqual([], indent(#config{indent=0, depth=1}))},
{"indent 1 depth 1", ?_assertEqual( {"indent 1 depth 1", ?_assertEqual(
[[?newline], ?space], [[?newline], ?space],
indent(#opts{indent=1, depth=1}) indent(#config{indent=1, depth=1})
)}, )},
{"indent 1 depth 2", ?_assertEqual( {"indent 1 depth 2", ?_assertEqual(
[[[?newline], ?space], ?space], [[[?newline], ?space], ?space],
indent(#opts{indent=1, depth=2}) indent(#config{indent=1, depth=2})
)}, )},
{"indent 4 depth 1", ?_assertEqual( {"indent 4 depth 1", ?_assertEqual(
[[?newline], <<" ">>], [[?newline], <<" ">>],
indent(#opts{indent=4, depth=1}) indent(#config{indent=4, depth=1})
)}, )},
{"indent 4 depth 2", ?_assertEqual( {"indent 4 depth 2", ?_assertEqual(
[[[?newline], <<" ">>], <<" ">>], [[[?newline], <<" ">>], <<" ">>],
indent(#opts{indent=4, depth=2}) indent(#config{indent=4, depth=2})
)} )}
]. ].
@ -243,48 +243,48 @@ indent_or_space_test_() ->
[ [
{"no indent so space", ?_assertEqual( {"no indent so space", ?_assertEqual(
<<" ">>, <<" ">>,
indent_or_space(#opts{space=1, indent=0, depth=1}) indent_or_space(#config{space=1, indent=0, depth=1})
)}, )},
{"indent so no space", ?_assertEqual( {"indent so no space", ?_assertEqual(
[[?newline], ?space], [[?newline], ?space],
indent_or_space(#opts{space=1, indent=1, depth=1}) indent_or_space(#config{space=1, indent=1, depth=1})
)} )}
]. ].
format_test_() -> format_test_() ->
[ [
{"0.0", ?_assert(encode(float, 0.0, #opts{}) =:= "0.0")}, {"0.0", ?_assert(encode(float, 0.0, #config{}) =:= "0.0")},
{"1.0", ?_assert(encode(float, 1.0, #opts{}) =:= "1.0")}, {"1.0", ?_assert(encode(float, 1.0, #config{}) =:= "1.0")},
{"-1.0", ?_assert(encode(float, -1.0, #opts{}) =:= "-1.0")}, {"-1.0", ?_assert(encode(float, -1.0, #config{}) =:= "-1.0")},
{"3.1234567890987654321", {"3.1234567890987654321",
?_assert( ?_assert(
encode(float, 3.1234567890987654321, #opts{}) =:= "3.1234567890987655") encode(float, 3.1234567890987654321, #config{}) =:= "3.1234567890987655")
}, },
{"1.0e23", ?_assert(encode(float, 1.0e23, #opts{}) =:= "1.0e23")}, {"1.0e23", ?_assert(encode(float, 1.0e23, #config{}) =:= "1.0e23")},
{"0.3", ?_assert(encode(float, 3.0/10.0, #opts{}) =:= "0.3")}, {"0.3", ?_assert(encode(float, 3.0/10.0, #config{}) =:= "0.3")},
{"0.0001", ?_assert(encode(float, 0.0001, #opts{}) =:= "0.0001")}, {"0.0001", ?_assert(encode(float, 0.0001, #config{}) =:= "0.0001")},
{"0.00001", ?_assert(encode(float, 0.00001, #opts{}) =:= "1.0e-5")}, {"0.00001", ?_assert(encode(float, 0.00001, #config{}) =:= "1.0e-5")},
{"0.00000001", ?_assert(encode(float, 0.00000001, #opts{}) =:= "1.0e-8")}, {"0.00000001", ?_assert(encode(float, 0.00000001, #config{}) =:= "1.0e-8")},
{"1.0e-323", ?_assert(encode(float, 1.0e-323, #opts{}) =:= "1.0e-323")}, {"1.0e-323", ?_assert(encode(float, 1.0e-323, #config{}) =:= "1.0e-323")},
{"1.0e308", ?_assert(encode(float, 1.0e308, #opts{}) =:= "1.0e308")}, {"1.0e308", ?_assert(encode(float, 1.0e308, #config{}) =:= "1.0e308")},
{"min normalized float", {"min normalized float",
?_assert( ?_assert(
encode(float, math:pow(2, -1022), #opts{}) =:= "2.2250738585072014e-308" encode(float, math:pow(2, -1022), #config{}) =:= "2.2250738585072014e-308"
) )
}, },
{"max normalized float", {"max normalized float",
?_assert( ?_assert(
encode(float, (2 - math:pow(2, -52)) * math:pow(2, 1023), #opts{}) encode(float, (2 - math:pow(2, -52)) * math:pow(2, 1023), #config{})
=:= "1.7976931348623157e308" =:= "1.7976931348623157e308"
) )
}, },
{"min denormalized float", {"min denormalized float",
?_assert(encode(float, math:pow(2, -1074), #opts{}) =:= "5.0e-324") ?_assert(encode(float, math:pow(2, -1074), #config{}) =:= "5.0e-324")
}, },
{"max denormalized float", {"max denormalized float",
?_assert( ?_assert(
encode(float, (1 - math:pow(2, -52)) * math:pow(2, -1022), #opts{}) encode(float, (1 - math:pow(2, -52)) * math:pow(2, -1022), #config{})
=:= "2.225073858507201e-308" =:= "2.225073858507201e-308"
) )
} }
@ -297,7 +297,7 @@ handle_event_test_() ->
{ {
Title, ?_assertEqual( Title, ?_assertEqual(
JSON, JSON,
lists:foldl(fun handle_event/2, {start, [], #opts{}}, Events ++ [end_json]) lists:foldl(fun handle_event/2, {start, [], #config{}}, Events ++ [end_json])
) )
} || {Title, JSON, _, Events} <- Data } || {Title, JSON, _, Events} <- Data
]. ].

View file

@ -27,12 +27,12 @@
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-record(opts, { -record(config, {
labels = binary, labels = binary,
post_decode = false post_decode = false
}). }).
-type opts() :: list(). -type config() :: list().
-type json_value() :: list({binary(), json_value()}) -type json_value() :: list({binary(), json_value()})
| list(json_value()) | list(json_value())
@ -44,74 +44,74 @@
| binary(). | binary().
-spec to_term(Source::binary(), Opts::opts()) -> json_value(). -spec to_term(Source::binary(), Config::config()) -> json_value().
to_term(Source, Opts) when is_list(Opts) -> to_term(Source, Config) when is_list(Config) ->
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source). (jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source).
parse_opts(Opts) -> parse_opts(Opts, #opts{}). parse_config(Config) -> parse_config(Config, #config{}).
parse_opts([{labels, Val}|Rest], Opts) parse_config([{labels, Val}|Rest], Config)
when Val == binary; Val == atom; Val == existing_atom -> when Val == binary; Val == atom; Val == existing_atom ->
parse_opts(Rest, Opts#opts{labels = Val}); parse_config(Rest, Config#config{labels = Val});
parse_opts([labels|Rest], Opts) -> parse_config([labels|Rest], Config) ->
parse_opts(Rest, Opts#opts{labels = binary}); parse_config(Rest, Config#config{labels = binary});
parse_opts([{post_decode, F}|Rest], Opts=#opts{post_decode=false}) when is_function(F, 1) -> parse_config([{post_decode, F}|Rest], Config=#config{post_decode=false}) when is_function(F, 1) ->
parse_opts(Rest, Opts#opts{post_decode=F}); parse_config(Rest, Config#config{post_decode=F});
parse_opts([{K, _}|Rest] = Options, Opts) -> parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([K|Rest] = Options, Opts) -> parse_config([K|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([], Opts) -> parse_config([], Config) ->
Opts. Config.
init(Opts) -> {[[]], parse_opts(Opts)}. init(Config) -> {[[]], parse_config(Config)}.
handle_event(end_json, {[[Terms]], _Opts}) -> Terms; handle_event(end_json, {[[Terms]], _Config}) -> Terms;
handle_event(start_object, {Terms, Opts}) -> {[[]|Terms], Opts}; handle_event(start_object, {Terms, Config}) -> {[[]|Terms], Config};
handle_event(end_object, {[[], {key, Key}, Last|Terms], Opts}) -> handle_event(end_object, {[[], {key, Key}, Last|Terms], Config}) ->
{[[{Key, post_decode([{}], Opts)}] ++ Last] ++ Terms, Opts}; {[[{Key, post_decode([{}], Config)}] ++ Last] ++ Terms, Config};
handle_event(end_object, {[Object, {key, Key}, Last|Terms], Opts}) -> handle_event(end_object, {[Object, {key, Key}, Last|Terms], Config}) ->
{[[{Key, post_decode(lists:reverse(Object), Opts)}] ++ Last] ++ Terms, Opts}; {[[{Key, post_decode(lists:reverse(Object), Config)}] ++ Last] ++ Terms, Config};
handle_event(end_object, {[[], Last|Terms], Opts}) -> handle_event(end_object, {[[], Last|Terms], Config}) ->
{[[post_decode([{}], Opts)] ++ Last] ++ Terms, Opts}; {[[post_decode([{}], Config)] ++ Last] ++ Terms, Config};
handle_event(end_object, {[Object, Last|Terms], Opts}) -> handle_event(end_object, {[Object, Last|Terms], Config}) ->
{[[post_decode(lists:reverse(Object), Opts)] ++ Last] ++ Terms, Opts}; {[[post_decode(lists:reverse(Object), Config)] ++ Last] ++ Terms, Config};
handle_event(start_array, {Terms, Opts}) -> {[[]|Terms], Opts}; handle_event(start_array, {Terms, Config}) -> {[[]|Terms], Config};
handle_event(end_array, {[List, {key, Key}, Last|Terms], Opts}) -> handle_event(end_array, {[List, {key, Key}, Last|Terms], Config}) ->
{[[{Key, post_decode(lists:reverse(List), Opts)}] ++ Last] ++ Terms, Opts}; {[[{Key, post_decode(lists:reverse(List), Config)}] ++ Last] ++ Terms, Config};
handle_event(end_array, {[List, Last|Terms], Opts}) -> handle_event(end_array, {[List, Last|Terms], Config}) ->
{[[post_decode(lists:reverse(List), Opts)] ++ Last] ++ Terms, Opts}; {[[post_decode(lists:reverse(List), Config)] ++ Last] ++ Terms, Config};
handle_event({key, Key}, {Terms, Opts}) -> {[{key, format_key(Key, Opts)}] ++ Terms, Opts}; handle_event({key, Key}, {Terms, Config}) -> {[{key, format_key(Key, Config)}] ++ Terms, Config};
handle_event({_, Event}, {[{key, Key}, Last|Terms], Opts}) -> handle_event({_, Event}, {[{key, Key}, Last|Terms], Config}) ->
{[[{Key, post_decode(Event, Opts)}] ++ Last] ++ Terms, Opts}; {[[{Key, post_decode(Event, Config)}] ++ Last] ++ Terms, Config};
handle_event({_, Event}, {[Last|Terms], Opts}) -> handle_event({_, Event}, {[Last|Terms], Config}) ->
{[[post_decode(Event, Opts)] ++ Last] ++ Terms, Opts}. {[[post_decode(Event, Config)] ++ Last] ++ Terms, Config}.
format_key(Key, Opts) -> format_key(Key, Config) ->
case Opts#opts.labels of case Config#config.labels of
binary -> Key binary -> Key
; atom -> binary_to_atom(Key, utf8) ; atom -> binary_to_atom(Key, utf8)
; existing_atom -> binary_to_existing_atom(Key, utf8) ; existing_atom -> binary_to_existing_atom(Key, utf8)
end. end.
post_decode(Value, #opts{post_decode=false}) -> Value; post_decode(Value, #config{post_decode=false}) -> Value;
post_decode(Value, Opts) -> (Opts#opts.post_decode)(Value). post_decode(Value, Config) -> (Config#config.post_decode)(Value).
%% eunit tests %% eunit tests
@ -120,40 +120,40 @@ post_decode(Value, Opts) -> (Opts#opts.post_decode)(Value).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
opts_test_() -> config_test_() ->
%% for post_decode tests %% for post_decode tests
F = fun(X) -> X end, F = fun(X) -> X end,
G = fun(X, Y) -> {X, Y} end, G = fun(X, Y) -> {X, Y} end,
[ [
{"empty opts", ?_assertEqual(#opts{}, parse_opts([]))}, {"empty config", ?_assertEqual(#config{}, parse_config([]))},
{"implicit binary labels", ?_assertEqual(#opts{}, parse_opts([labels]))}, {"implicit binary labels", ?_assertEqual(#config{}, parse_config([labels]))},
{"binary labels", ?_assertEqual(#opts{}, parse_opts([{labels, binary}]))}, {"binary labels", ?_assertEqual(#config{}, parse_config([{labels, binary}]))},
{"atom labels", ?_assertEqual(#opts{labels=atom}, parse_opts([{labels, atom}]))}, {"atom labels", ?_assertEqual(#config{labels=atom}, parse_config([{labels, atom}]))},
{"existing atom labels", ?_assertEqual( {"existing atom labels", ?_assertEqual(
#opts{labels=existing_atom}, #config{labels=existing_atom},
parse_opts([{labels, existing_atom}]) parse_config([{labels, existing_atom}])
)}, )},
{"post decode", ?_assertEqual( {"post decode", ?_assertEqual(
#opts{post_decode=F}, #config{post_decode=F},
parse_opts([{post_decode, F}]) parse_config([{post_decode, F}])
)}, )},
{"post decode wrong arity", ?_assertError(badarg, parse_opts([{post_decode, G}]))}, {"post decode wrong arity", ?_assertError(badarg, parse_config([{post_decode, G}]))},
{"invalid opt flag", ?_assertError(badarg, parse_opts([error]))}, {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
{"invalid opt tuple", ?_assertError(badarg, parse_opts([{error, true}]))} {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
]. ].
format_key_test_() -> format_key_test_() ->
[ [
{"binary key", ?_assertEqual(<<"key">>, format_key(<<"key">>, #opts{labels=binary}))}, {"binary key", ?_assertEqual(<<"key">>, format_key(<<"key">>, #config{labels=binary}))},
{"atom key", ?_assertEqual(key, format_key(<<"key">>, #opts{labels=atom}))}, {"atom key", ?_assertEqual(key, format_key(<<"key">>, #config{labels=atom}))},
{"existing atom key", ?_assertEqual( {"existing atom key", ?_assertEqual(
key, key,
format_key(<<"key">>, #opts{labels=existing_atom}) format_key(<<"key">>, #config{labels=existing_atom})
)}, )},
{"nonexisting atom key", ?_assertError( {"nonexisting atom key", ?_assertError(
badarg, badarg,
format_key(<<"nonexistentatom">>, #opts{labels=existing_atom}) format_key(<<"nonexistentatom">>, #config{labels=existing_atom})
)} )}
]. ].
@ -177,7 +177,7 @@ post_decoders_test_() ->
[ [
{"no post_decode", ?_assertEqual( {"no post_decode", ?_assertEqual(
Events, Events,
[ post_decode(Event, #opts{}) || Event <- Events ] [ post_decode(Event, #config{}) || Event <- Events ]
)}, )},
{"replace arrays with empty arrays", ?_assertEqual( {"replace arrays with empty arrays", ?_assertEqual(
[ [
@ -195,7 +195,7 @@ post_decoders_test_() ->
1, 1,
1.0 1.0
], ],
[ post_decode(Event, #opts{ [ post_decode(Event, #config{
post_decode=fun([T|_] = V) when is_tuple(T) -> V; (V) when is_list(V) -> []; (V) -> V end post_decode=fun([T|_] = V) when is_tuple(T) -> V; (V) when is_list(V) -> []; (V) -> V end
}) || Event <- Events }) || Event <- Events
] ]
@ -216,7 +216,7 @@ post_decoders_test_() ->
1, 1,
1.0 1.0
], ],
[ post_decode(Event, #opts{ [ post_decode(Event, #config{
post_decode=fun([T|_]) when is_tuple(T) -> [{}]; (V) -> V end post_decode=fun([T|_]) when is_tuple(T) -> [{}]; (V) -> V end
}) || Event <- Events }) || Event <- Events
] ]
@ -237,7 +237,7 @@ post_decoders_test_() ->
false, false,
false false
], ],
[ post_decode(Event, #opts{ [ post_decode(Event, #config{
post_decode=fun(V) when is_list(V) -> V; (_) -> false end post_decode=fun(V) when is_list(V) -> V; (_) -> false end
}) || Event <- Events }) || Event <- Events
] ]
@ -258,7 +258,7 @@ post_decoders_test_() ->
1, 1,
1.0 1.0
], ],
[ post_decode(Event, #opts{ [ post_decode(Event, #config{
post_decode=fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end post_decode=fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end
}) || Event <- Events }) || Event <- Events
] ]
@ -273,7 +273,7 @@ handle_event_test_() ->
{ {
Title, ?_assertEqual( Title, ?_assertEqual(
Term, Term,
lists:foldl(fun handle_event/2, {[[]], #opts{}}, Events ++ [end_json]) lists:foldl(fun handle_event/2, {[[]], #config{}}, Events ++ [end_json])
) )
} || {Title, _, Term, Events} <- Data } || {Title, _, Term, Events} <- Data
]. ].

File diff suppressed because it is too large Load diff

View file

@ -27,65 +27,65 @@
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-record(opts, { -record(config, {
repeated_keys = true repeated_keys = true
}). }).
-type opts() :: []. -type config() :: [].
-spec is_json(Source::binary(), Opts::opts()) -> true | false. -spec is_json(Source::binary(), Config::config()) -> true | false.
is_json(Source, Opts) when is_list(Opts) -> is_json(Source, Config) when is_list(Config) ->
try (jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source) try (jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source)
catch error:badarg -> false catch error:badarg -> false
end. end.
-spec is_term(Source::any(), Opts::opts()) -> true | false. -spec is_term(Source::any(), Config::config()) -> true | false.
is_term(Source, Opts) when is_list(Opts) -> is_term(Source, Config) when is_list(Config) ->
try (jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source) try (jsx:encoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source)
catch error:badarg -> false catch error:badarg -> false
end. end.
parse_opts(Opts) -> parse_opts(Opts, #opts{}). parse_config(Config) -> parse_config(Config, #config{}).
parse_opts([{repeated_keys, Val}|Rest], Opts) when Val == true; Val == false -> parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
parse_opts(Rest, Opts#opts{repeated_keys = Val}); parse_config(Rest, Config#config{repeated_keys = Val});
parse_opts([repeated_keys|Rest], Opts) -> parse_config([repeated_keys|Rest], Config) ->
parse_opts(Rest, Opts#opts{repeated_keys = true}); parse_config(Rest, Config#config{repeated_keys = true});
parse_opts([{K, _}|Rest] = Options, Opts) -> parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([K|Rest] = Options, Opts) -> parse_config([K|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_opts(Rest, Opts) true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Opts]) ; false -> erlang:error(badarg, [Options, Config])
end; end;
parse_opts([], Opts) -> parse_config([], Config) ->
Opts. Config.
init(Opts) -> {parse_opts(Opts), []}. init(Config) -> {parse_config(Config), []}.
handle_event(end_json, _) -> true; handle_event(end_json, _) -> true;
handle_event(_, {Opts, _} = State) when Opts#opts.repeated_keys == true -> State; handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State;
handle_event(start_object, {Opts, Keys}) -> {Opts, [dict:new()] ++ Keys}; handle_event(start_object, {Config, Keys}) -> {Config, [dict:new()] ++ Keys};
handle_event(end_object, {Opts, [_|Keys]}) -> {Opts, Keys}; handle_event(end_object, {Config, [_|Keys]}) -> {Config, Keys};
handle_event({key, Key}, {Opts, [CurrentKeys|Keys]}) -> handle_event({key, Key}, {Config, [CurrentKeys|Keys]}) ->
case dict:is_key(Key, CurrentKeys) of case dict:is_key(Key, CurrentKeys) of
true -> erlang:error(badarg) true -> erlang:error(badarg)
; false -> {Opts, [dict:store(Key, blah, CurrentKeys)|Keys]} ; false -> {Config, [dict:store(Key, blah, CurrentKeys)|Keys]}
end; end;
handle_event(_, State) -> State. handle_event(_, State) -> State.
@ -97,20 +97,20 @@ handle_event(_, State) -> State.
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
opts_test_() -> config_test_() ->
[ [
{"empty opts", ?_assertEqual(#opts{}, parse_opts([]))}, {"empty config", ?_assertEqual(#config{}, parse_config([]))},
{"bare repeated keys", ?_assertEqual(#opts{}, parse_opts([repeated_keys]))}, {"bare repeated keys", ?_assertEqual(#config{}, parse_config([repeated_keys]))},
{"repeated keys true", ?_assertEqual( {"repeated keys true", ?_assertEqual(
#opts{}, #config{},
parse_opts([{repeated_keys, true}]) parse_config([{repeated_keys, true}])
)}, )},
{"repeated keys false", ?_assertEqual( {"repeated keys false", ?_assertEqual(
#opts{repeated_keys=false}, #config{repeated_keys=false},
parse_opts([{repeated_keys, false}]) parse_config([{repeated_keys, false}])
)}, )},
{"invalid opt flag", ?_assertError(badarg, parse_opts([error]))}, {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
{"invalid opt tuple", ?_assertError(badarg, parse_opts([{error, true}]))} {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
]. ].
@ -121,7 +121,7 @@ handle_event_test_() ->
{ {
Title, ?_assertEqual( Title, ?_assertEqual(
true, true,
lists:foldl(fun handle_event/2, {#opts{}, []}, Events ++ [end_json]) lists:foldl(fun handle_event/2, {#config{}, []}, Events ++ [end_json])
) )
} || {Title, _, _, Events} <- Data } || {Title, _, _, Events} <- Data
]. ].