rename jsx_utils jsx_config
This commit is contained in:
parent
76ccb0722a
commit
89d3cbbd36
11 changed files with 57 additions and 59 deletions
|
@ -9,7 +9,7 @@
|
|||
jsx_parser,
|
||||
jsx_to_json,
|
||||
jsx_to_term,
|
||||
jsx_utils,
|
||||
jsx_config,
|
||||
jsx_verify
|
||||
]},
|
||||
{registered, []},
|
||||
|
|
|
@ -160,6 +160,6 @@ parser(Handler, State, Config) -> jsx_parser:parser(Handler, State, Config).
|
|||
-spec resume(Term::json_text() | token(), InternalState::internal_state(), Config::list()) -> any().
|
||||
|
||||
resume(Term, {decoder, State, Handler, Acc, Stack}, Config) ->
|
||||
jsx_decoder:resume(Term, State, Handler, Acc, Stack, jsx_utils:parse_config(Config));
|
||||
jsx_decoder:resume(Term, State, Handler, Acc, Stack, jsx_config:parse_config(Config));
|
||||
resume(Term, {parser, State, Handler, Stack}, Config) ->
|
||||
jsx_parser:resume(Term, State, Handler, Stack, jsx_utils:parse_config(Config)).
|
||||
jsx_parser:resume(Term, State, Handler, Stack, jsx_config:parse_config(Config)).
|
|
@ -21,12 +21,11 @@
|
|||
%% THE SOFTWARE.
|
||||
|
||||
|
||||
-module(jsx_utils).
|
||||
-module(jsx_config).
|
||||
|
||||
-export([parse_config/1]).
|
||||
-export([config_to_list/1]).
|
||||
-export([extract_config/1, valid_flags/0]).
|
||||
-export([json_escape_sequence/1]).
|
||||
|
||||
-ifdef(TEST).
|
||||
-export([fake_error_handler/3]).
|
||||
|
@ -159,35 +158,11 @@ extract_parser_config([K|Rest], Acc) ->
|
|||
end.
|
||||
|
||||
|
||||
%% convert a codepoint to it's \uXXXX equiv.
|
||||
json_escape_sequence(X) ->
|
||||
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
||||
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
|
||||
|
||||
|
||||
to_hex(10) -> $a;
|
||||
to_hex(11) -> $b;
|
||||
to_hex(12) -> $c;
|
||||
to_hex(13) -> $d;
|
||||
to_hex(14) -> $e;
|
||||
to_hex(15) -> $f;
|
||||
to_hex(X) -> X + 48. %% ascii "1" is [49], "2" is [50], etc...
|
||||
|
||||
|
||||
|
||||
%% eunit tests
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
|
||||
json_escape_sequence_test_() ->
|
||||
[
|
||||
{"json escape sequence test - 16#0000", ?_assertEqual(json_escape_sequence(16#0000), "\\u0000")},
|
||||
{"json escape sequence test - 16#abc", ?_assertEqual(json_escape_sequence(16#abc), "\\u0abc")},
|
||||
{"json escape sequence test - 16#def", ?_assertEqual(json_escape_sequence(16#def), "\\u0def")}
|
||||
].
|
||||
|
||||
|
||||
config_test_() ->
|
||||
[
|
||||
{"all flags",
|
|
@ -35,7 +35,7 @@
|
|||
-spec decoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:decoder().
|
||||
|
||||
decoder(Handler, State, Config) ->
|
||||
fun(JSON) -> start(JSON, {Handler, Handler:init(State)}, [], jsx_utils:parse_config(Config)) end.
|
||||
fun(JSON) -> start(JSON, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
|
||||
|
||||
|
||||
%% resume allows continuation from interrupted decoding without having to explicitly export
|
||||
|
@ -128,7 +128,7 @@ resume(Rest, State, Handler, Acc, Stack, Config) ->
|
|||
-define(error(State, Bin, Handler, Acc, Stack, Config),
|
||||
case Config#config.error_handler of
|
||||
false -> erlang:error(badarg);
|
||||
F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config))
|
||||
F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config))
|
||||
end
|
||||
).
|
||||
-define(error(State, Bin, Handler, Stack, Config),
|
||||
|
@ -151,7 +151,7 @@ incomplete(State, Rest, Handler, Acc, Stack, Config=#config{incomplete_handler=f
|
|||
end
|
||||
};
|
||||
incomplete(State, Rest, Handler, Acc, Stack, Config=#config{incomplete_handler=F}) ->
|
||||
F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config)).
|
||||
F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config)).
|
||||
|
||||
|
||||
%% lists are benchmarked to be faster (tho higher in memory usage) than binaries
|
||||
|
@ -701,13 +701,28 @@ maybe_replace($\\, #config{escaped_strings=true}) -> [$\\, $\\];
|
|||
maybe_replace(X, Config=#config{escaped_strings=true}) when X == 16#2028; X == 16#2029 ->
|
||||
case Config#config.unescaped_jsonp of
|
||||
true -> [X]
|
||||
; false -> jsx_utils:json_escape_sequence(X)
|
||||
; false -> json_escape_sequence(X)
|
||||
end;
|
||||
maybe_replace(X, #config{escaped_strings=true}) when X < 32 ->
|
||||
jsx_utils:json_escape_sequence(X);
|
||||
maybe_replace(X, #config{escaped_strings=true}) when X < 32 -> json_escape_sequence(X);
|
||||
maybe_replace(X, _Config) -> [X].
|
||||
|
||||
|
||||
%% convert a codepoint to it's \uXXXX equiv.
|
||||
json_escape_sequence(X) ->
|
||||
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
||||
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
|
||||
|
||||
|
||||
%% ascii "1" is [49], "2" is [50], etc...
|
||||
to_hex(10) -> $a;
|
||||
to_hex(11) -> $b;
|
||||
to_hex(12) -> $c;
|
||||
to_hex(13) -> $d;
|
||||
to_hex(14) -> $e;
|
||||
to_hex(15) -> $f;
|
||||
to_hex(X) -> X + 48.
|
||||
|
||||
|
||||
%% like in strings, there's some pseudo states in here that will never
|
||||
%% show up in errors or incompletes. some show up in value, some show
|
||||
%% up in integer, decimal or exp
|
||||
|
@ -934,7 +949,7 @@ json_to_bytes(<<X, Rest/binary>>, Acc) -> json_to_bytes(Rest, [<<X>>] ++ Acc).
|
|||
|
||||
decode(JSON, Config) ->
|
||||
Chunk = try
|
||||
start(JSON, {jsx, []}, [], jsx_utils:parse_config(Config))
|
||||
start(JSON, {jsx, []}, [], jsx_config:parse_config(Config))
|
||||
catch
|
||||
error:badarg -> {error, badarg}
|
||||
end,
|
||||
|
@ -1858,7 +1873,7 @@ bom_test_() ->
|
|||
|
||||
|
||||
error_test_() ->
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_utils:parse_config(Config)) end,
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_config:parse_config(Config)) end,
|
||||
[
|
||||
{"maybe_bom error", ?_assertError(
|
||||
badarg,
|
||||
|
@ -1952,7 +1967,7 @@ error_test_() ->
|
|||
|
||||
|
||||
custom_error_handler_test_() ->
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_utils:parse_config(Config)) end,
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_config:parse_config(Config)) end,
|
||||
Error = fun(Rest, {_, State, _, _, _}, _) -> {State, Rest} end,
|
||||
[
|
||||
{"maybe_bom error", ?_assertEqual(
|
||||
|
@ -2047,7 +2062,7 @@ custom_error_handler_test_() ->
|
|||
|
||||
|
||||
custom_incomplete_handler_test_() ->
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_utils:parse_config(Config)) end,
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_config:parse_config(Config)) end,
|
||||
[
|
||||
{"custom incomplete handler", ?_assertError(
|
||||
badarg,
|
||||
|
|
|
@ -32,7 +32,7 @@ encoder(Handler, State, Config) ->
|
|||
start(
|
||||
JSON,
|
||||
{Handler, Handler:init(State)},
|
||||
jsx_utils:parse_config(Config)
|
||||
jsx_config:parse_config(Config)
|
||||
)
|
||||
end.
|
||||
|
||||
|
@ -45,7 +45,7 @@ encoder(Handler, State, Config) ->
|
|||
-define(error(State, Term, Handler, Config),
|
||||
case Config#config.error_handler of
|
||||
false -> erlang:error(badarg);
|
||||
F -> erlang:throw(F(Term, {encoder, State, Handler}, jsx_utils:config_to_list(Config)))
|
||||
F -> erlang:throw(F(Term, {encoder, State, Handler}, jsx_config:config_to_list(Config)))
|
||||
end
|
||||
).
|
||||
-endif.
|
||||
|
@ -159,7 +159,7 @@ encode_test_() ->
|
|||
].
|
||||
|
||||
|
||||
encode(Term, Config) -> start(Term, {jsx, []}, jsx_utils:parse_config(Config)).
|
||||
encode(Term, Config) -> start(Term, {jsx, []}, jsx_config:parse_config(Config)).
|
||||
|
||||
pre_encoders_test_() ->
|
||||
Term = [
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
-spec parser(Handler::module(), State::any(), Config::jsx:config()) -> jsx:parser().
|
||||
|
||||
parser(Handler, State, Config) ->
|
||||
fun(Tokens) -> value(Tokens, {Handler, Handler:init(State)}, [], jsx_utils:parse_config(Config)) end.
|
||||
fun(Tokens) -> value(Tokens, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
|
||||
|
||||
|
||||
%% resume allows continuation from interrupted decoding without having to explicitly export
|
||||
|
@ -60,7 +60,7 @@ resume(Rest, State, Handler, Stack, Config) ->
|
|||
-define(error(State, Terms, Handler, Stack, Config),
|
||||
case Config#config.error_handler of
|
||||
false -> erlang:error(badarg);
|
||||
F -> F(Terms, {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config))
|
||||
F -> F(Terms, {parser, State, Handler, Stack}, jsx_config:config_to_list(Config))
|
||||
end
|
||||
|
||||
).
|
||||
|
@ -78,7 +78,7 @@ incomplete(State, Handler, Stack, Config=#config{incomplete_handler=false}) ->
|
|||
end
|
||||
};
|
||||
incomplete(State, Handler, Stack, Config=#config{incomplete_handler=F}) ->
|
||||
F([], {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config)).
|
||||
F([], {parser, State, Handler, Stack}, jsx_config:config_to_list(Config)).
|
||||
|
||||
|
||||
handle_event([], Handler, _Config) -> Handler;
|
||||
|
@ -213,7 +213,7 @@ clean_string(Bin, Tokens, Handler, Stack, Config) ->
|
|||
|
||||
parse(Events, Config) ->
|
||||
Chunk = try
|
||||
value(Events ++ [end_json], {jsx, []}, [], jsx_utils:parse_config(Config))
|
||||
value(Events ++ [end_json], {jsx, []}, [], jsx_config:parse_config(Config))
|
||||
catch
|
||||
error:badarg -> {error, badarg}
|
||||
end,
|
||||
|
@ -243,7 +243,7 @@ parse_test_() ->
|
|||
].
|
||||
|
||||
|
||||
parse_error(Events, Config) -> value(Events, {jsx, []}, [], jsx_utils:parse_config(Config)).
|
||||
parse_error(Events, Config) -> value(Events, {jsx, []}, [], jsx_config:parse_config(Config)).
|
||||
|
||||
|
||||
error_test_() ->
|
||||
|
|
|
@ -381,7 +381,7 @@ maybe_replace(X, Config=#config{escaped_strings=true}) when X == 16#2028; X ==
|
|||
false -> lists:reverse(json_escape_sequence(X))
|
||||
end;
|
||||
maybe_replace(X, #config{escaped_strings=true}) when X < 32 ->
|
||||
lists:reverse(jsx_utils:json_escape_sequence(X));
|
||||
lists:reverse(json_escape_sequence(X));
|
||||
maybe_replace(noncharacter, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
||||
maybe_replace(surrogate, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
||||
maybe_replace(badutf, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
||||
|
|
|
@ -679,3 +679,11 @@ bad_utf8_test_() ->
|
|||
clean_string(<<16#f0, 16#8f, 16#bf, 16#bf, 32>>, #config{replaced_bad_utf8=true})
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
json_escape_sequence_test_() ->
|
||||
[
|
||||
{"json escape sequence test - 16#0000", ?_assertEqual(json_escape_sequence(16#0000), "\\u0000")},
|
||||
{"json escape sequence test - 16#abc", ?_assertEqual(json_escape_sequence(16#abc), "\\u0abc")},
|
||||
{"json escape sequence test - 16#def", ?_assertEqual(json_escape_sequence(16#def), "\\u0def")}
|
||||
].
|
|
@ -39,13 +39,13 @@
|
|||
-spec to_json(Source::any(), Config::config()) -> binary().
|
||||
|
||||
to_json(Source, Config) when is_list(Config) ->
|
||||
(jsx:encoder(?MODULE, Config, jsx_utils:extract_config(Config ++ [escaped_strings])))(Source).
|
||||
(jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source).
|
||||
|
||||
|
||||
-spec format(Source::binary(), Config::config()) -> binary().
|
||||
|
||||
format(Source, Config) when is_binary(Source) andalso is_list(Config) ->
|
||||
(jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config ++ [escaped_strings])))(Source).
|
||||
(jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source).
|
||||
|
||||
|
||||
parse_config(Config) -> parse_config(Config, #config{}).
|
||||
|
@ -59,12 +59,12 @@ parse_config([{indent, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
|
|||
parse_config([indent|Rest], Config) ->
|
||||
parse_config(Rest, Config#config{indent = 1});
|
||||
parse_config([{K, _}|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config)
|
||||
; false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
parse_config([K|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config)
|
||||
; false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
-spec to_term(Source::binary(), Config::config()) -> json_value().
|
||||
|
||||
to_term(Source, Config) when is_list(Config) ->
|
||||
(jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source).
|
||||
(jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source).
|
||||
|
||||
|
||||
parse_config(Config) -> parse_config(Config, #config{}).
|
||||
|
@ -60,12 +60,12 @@ parse_config([labels|Rest], Config) ->
|
|||
parse_config([{post_decode, F}|Rest], Config=#config{post_decode=false}) when is_function(F, 1) ->
|
||||
parse_config(Rest, Config#config{post_decode=F});
|
||||
parse_config([{K, _}|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config)
|
||||
; false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
parse_config([K|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config)
|
||||
; false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
-spec is_json(Source::binary(), Config::config()) -> true | false.
|
||||
|
||||
is_json(Source, Config) when is_list(Config) ->
|
||||
try (jsx:decoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source)
|
||||
try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
||||
catch error:badarg -> false
|
||||
end.
|
||||
|
||||
|
@ -45,7 +45,7 @@ is_json(Source, Config) when is_list(Config) ->
|
|||
-spec is_term(Source::any(), Config::config()) -> true | false.
|
||||
|
||||
is_term(Source, Config) when is_list(Config) ->
|
||||
try (jsx:encoder(?MODULE, Config, jsx_utils:extract_config(Config)))(Source)
|
||||
try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
||||
catch error:badarg -> false
|
||||
end.
|
||||
|
||||
|
@ -60,12 +60,12 @@ parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false
|
|||
parse_config([repeated_keys|Rest], Config) ->
|
||||
parse_config(Rest, Config#config{repeated_keys=true});
|
||||
parse_config([{K, _}|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config);
|
||||
false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
parse_config([K|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_utils:valid_flags()) of
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config);
|
||||
false -> erlang:error(badarg, [Options, Config])
|
||||
end;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue