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

@ -5,4 +5,4 @@ clean:
./rebar clean ./rebar clean
test: clean test: clean
./rebar --jobs=1 eunit ./rebar --jobs=1 skip_deps=true eunit

View file

@ -27,6 +27,7 @@
-export([to_term/1, to_term/2]). -export([to_term/1, to_term/2]).
-export([is_json/1, is_json/2]). -export([is_json/1, is_json/2]).
-export([format/1, format/2]). -export([format/1, format/2]).
-export([encoder/3, decoder/3]).
%% old api %% old api
-export([term_to_json/1, term_to_json/2, json_to_term/1, json_to_term/2]). -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). 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). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").

View file

@ -39,9 +39,9 @@
-spec to_json(Source::(binary() | list()), Opts::opts()) -> binary(). -spec to_json(Source::(binary() | list()), Opts::opts()) -> binary().
to_json(Source, Opts) when is_list(Source) andalso is_list(Opts) -> 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) -> 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(). -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: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(). -spec is_json(Source::(binary() | list()), Opts::opts()) -> binary().
is_json(Source, Opts) when is_list(Source) andalso is_list(Opts) -> 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 catch error:badarg -> false
end; end;
is_json(Source, Opts) when is_binary(Source) andalso is_list(Opts) -> 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 catch error:badarg -> false
end. end.