lift init methods into decoder/encoder

This commit is contained in:
alisdair sullivan 2012-03-05 19:53:55 -08:00
parent 90520b9c0c
commit d654a0e882
5 changed files with 20 additions and 7 deletions

View file

@ -29,7 +29,14 @@
-spec decoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:decoder(). -spec decoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:decoder().
decoder(Handler, State, Opts) -> 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.

View file

@ -29,7 +29,13 @@
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder(). -spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder().
encoder(Handler, State, Opts) -> 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.

View file

@ -39,13 +39,13 @@
-spec to_json(Source::any(), Opts::opts()) -> binary(). -spec to_json(Source::any(), Opts::opts()) -> binary().
to_json(Source, Opts) when is_list(Opts) -> 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(). -spec format(Source::binary(), Opts::opts()) -> binary().
format(Source, Opts) when is_binary(Source) andalso is_list(Opts) -> 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{}). parse_opts(Opts) -> parse_opts(Opts, #opts{}).

View file

@ -37,7 +37,7 @@
-spec to_term(Source::(binary() | list()), Opts::opts()) -> binary(). -spec to_term(Source::(binary() | list()), Opts::opts()) -> binary().
to_term(Source, Opts) when is_list(Opts) -> 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).

View file

@ -37,7 +37,7 @@
-spec is_json(Source::binary(), Opts::opts()) -> true | false. -spec is_json(Source::binary(), Opts::opts()) -> true | false.
is_json(Source, Opts) when is_list(Opts) -> 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 catch error:badarg -> false
end. end.
@ -45,7 +45,7 @@ is_json(Source, Opts) when is_list(Opts) ->
-spec is_term(Source::any(), Opts::opts()) -> true | false. -spec is_term(Source::any(), Opts::opts()) -> true | false.
is_term(Source, Opts) when is_list(Opts) -> 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 catch error:badarg -> false
end. end.