add encoder and decoder functions to api

This commit is contained in:
alisdair sullivan 2012-03-04 15:46:41 -08:00
parent 3cbabec80e
commit ed33626ed5
5 changed files with 17 additions and 6 deletions

View file

@ -27,6 +27,7 @@
-export([to_term/1, to_term/2]).
-export([is_json/1, is_json/2]).
-export([format/1, format/2]).
-export([encoder/3, decoder/3]).
%% old api
-export([term_to_json/1, term_to_json/2, json_to_term/1, json_to_term/2]).
@ -79,6 +80,16 @@ is_json(Source) -> is_json(Source, []).
is_json(Source, Opts) -> jsx_verify:is_json(Source, Opts).
-spec decoder(Handler::module(), State::any(), Opts::list()) -> fun().
decoder(Handler, State, Opts) -> jsx_decoder:decoder(Handler, State, Opts).
-spec encoder(Handler::module(), State::any(), Opts::list()) -> fun().
encoder(Handler, State, Opts) -> jsx_encoder:encoder(Handler, State, Opts).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

View file

@ -39,9 +39,9 @@
-spec to_json(Source::(binary() | list()), Opts::opts()) -> binary().
to_json(Source, Opts) when is_list(Source) andalso is_list(Opts) ->
(jsx_encoder:encoder(?MODULE, init(Opts), Opts))(Source);
(jsx:encoder(?MODULE, init(Opts), Opts))(Source);
to_json(Source, Opts) when is_binary(Source) andalso is_list(Opts) ->
(jsx_decoder:decoder(?MODULE, init(Opts), Opts))(Source).
(jsx:decoder(?MODULE, init(Opts), Opts))(Source).

View file

@ -37,7 +37,7 @@
-spec to_term(Source::(binary() | list()), Opts::opts()) -> binary().
to_term(Source, Opts) when is_list(Opts) ->
(jsx_decoder:decoder(?MODULE, init(Opts), Opts))(Source).
(jsx:decoder(?MODULE, init(Opts), Opts))(Source).

View file

@ -37,11 +37,11 @@
-spec is_json(Source::(binary() | list()), Opts::opts()) -> binary().
is_json(Source, Opts) when is_list(Source) andalso is_list(Opts) ->
try (jsx_encoder:encoder(?MODULE, init(Opts), Opts))(Source)
try (jsx:encoder(?MODULE, init(Opts), Opts))(Source)
catch error:badarg -> false
end;
is_json(Source, Opts) when is_binary(Source) andalso is_list(Opts) ->
try (jsx_decoder:decoder(?MODULE, init(Opts), Opts))(Source)
try (jsx:decoder(?MODULE, init(Opts), Opts))(Source)
catch error:badarg -> false
end.