lift init methods into decoder/encoder
This commit is contained in:
parent
90520b9c0c
commit
d654a0e882
5 changed files with 20 additions and 7 deletions
|
@ -29,7 +29,14 @@
|
|||
-spec decoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:decoder().
|
||||
|
||||
decoder(Handler, State, Opts) ->
|
||||
fun(JSON) -> value(JSON, {Handler, State}, [], jsx_utils:parse_opts(Opts)) end.
|
||||
fun(JSON) ->
|
||||
value(
|
||||
JSON,
|
||||
{Handler, Handler:init(State)},
|
||||
[],
|
||||
jsx_utils:parse_opts(Opts)
|
||||
)
|
||||
end.
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -29,7 +29,13 @@
|
|||
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder().
|
||||
|
||||
encoder(Handler, State, Opts) ->
|
||||
fun(JSON) -> start(JSON, {Handler, State}, jsx_utils:parse_opts(Opts)) end.
|
||||
fun(JSON) ->
|
||||
start(
|
||||
JSON,
|
||||
{Handler, Handler:init(State)},
|
||||
jsx_utils:parse_opts(Opts)
|
||||
)
|
||||
end.
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -39,13 +39,13 @@
|
|||
-spec to_json(Source::any(), Opts::opts()) -> binary().
|
||||
|
||||
to_json(Source, Opts) when is_list(Opts) ->
|
||||
(jsx:encoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source).
|
||||
(jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source).
|
||||
|
||||
|
||||
-spec format(Source::binary(), Opts::opts()) -> binary().
|
||||
|
||||
format(Source, Opts) when is_binary(Source) andalso is_list(Opts) ->
|
||||
(jsx:decoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source).
|
||||
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source).
|
||||
|
||||
|
||||
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
-spec to_term(Source::(binary() | list()), Opts::opts()) -> binary().
|
||||
|
||||
to_term(Source, Opts) when is_list(Opts) ->
|
||||
(jsx:decoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source).
|
||||
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source).
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
-spec is_json(Source::binary(), Opts::opts()) -> true | false.
|
||||
|
||||
is_json(Source, Opts) when is_list(Opts) ->
|
||||
try (jsx:decoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source)
|
||||
try (jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source)
|
||||
catch error:badarg -> false
|
||||
end.
|
||||
|
||||
|
@ -45,7 +45,7 @@ is_json(Source, Opts) when is_list(Opts) ->
|
|||
-spec is_term(Source::any(), Opts::opts()) -> true | false.
|
||||
|
||||
is_term(Source, Opts) when is_list(Opts) ->
|
||||
try (jsx:encoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source)
|
||||
try (jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source)
|
||||
catch error:badarg -> false
|
||||
end.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue