rewrote extraction of core parser options in various components in a consistent manner
This commit is contained in:
parent
ea32b3db40
commit
b73e506fe2
2 changed files with 8 additions and 25 deletions
|
@ -42,7 +42,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(opts_to_jsx_opts(Opts)),
|
P = jsx:parser(extract_parser_opts(Opts)),
|
||||||
case proplists:get_value(strict, Opts, true) of
|
case proplists:get_value(strict, Opts, true) of
|
||||||
true -> collect_strict(P(JSON), [[]], Opts)
|
true -> collect_strict(P(JSON), [[]], Opts)
|
||||||
; false -> collect(P(JSON), [[]], Opts)
|
; false -> collect(P(JSON), [[]], Opts)
|
||||||
|
@ -68,27 +68,8 @@ term_to_json(List, Opts) ->
|
||||||
).
|
).
|
||||||
|
|
||||||
|
|
||||||
%% parse opts for the decoder
|
extract_parser_opts(Opts) ->
|
||||||
opts_to_jsx_opts(Opts) ->
|
[ {K, V} || {K, V} <- Opts, lists:member(K, [comments, encoding]) ].
|
||||||
opts_to_jsx_opts(Opts, []).
|
|
||||||
|
|
||||||
opts_to_jsx_opts([{encoding, Val}|Rest], Acc) ->
|
|
||||||
case lists:member(Val,
|
|
||||||
[auto, utf8, utf16, {utf16, little}, utf32, {utf32, little}]
|
|
||||||
) of
|
|
||||||
true -> opts_to_jsx_opts(Rest, [{encoding, Val}] ++ Acc)
|
|
||||||
; false -> opts_to_jsx_opts(Rest, Acc)
|
|
||||||
end;
|
|
||||||
opts_to_jsx_opts([{comments, Val}|Rest], Acc) ->
|
|
||||||
case Val of
|
|
||||||
true -> opts_to_jsx_opts(Rest, [{comments, true}] ++ Acc)
|
|
||||||
; false -> opts_to_jsx_opts(Rest, [{comments, false}] ++ Acc)
|
|
||||||
; _ -> opts_to_jsx_opts(Rest, Acc)
|
|
||||||
end;
|
|
||||||
opts_to_jsx_opts([_|Rest], Acc) ->
|
|
||||||
opts_to_jsx_opts(Rest, Acc);
|
|
||||||
opts_to_jsx_opts([], Acc) ->
|
|
||||||
Acc.
|
|
||||||
|
|
||||||
|
|
||||||
%% ensure the first jsx event we get is start_object or start_array when running
|
%% ensure the first jsx event we get is start_object or start_array when running
|
||||||
|
|
|
@ -43,13 +43,15 @@
|
||||||
-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) ->
|
||||||
Encoding = proplists:get_value(encoding, Opts, utf8),
|
P = jsx:parser(extract_parser_opts(Opts)),
|
||||||
Comments = proplists:get_value(comments, Opts, false),
|
|
||||||
P = jsx:parser([{encoding, Encoding}, {comments, Comments}]),
|
|
||||||
case proplists:get_value(strict, Opts, true) of
|
case proplists:get_value(strict, Opts, true) of
|
||||||
true -> collect_strict(P(JSON), [[]])
|
true -> collect_strict(P(JSON), [[]])
|
||||||
; false -> collect(P(JSON), [[]])
|
; false -> collect(P(JSON), [[]])
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
extract_parser_opts(Opts) ->
|
||||||
|
[ {K, V} || {K, V} <- Opts, lists:member(K, [comments, encoding]) ].
|
||||||
|
|
||||||
|
|
||||||
%% enforce only arrays and objects at top level
|
%% enforce only arrays and objects at top level
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue