From ed33626ed5776b8b81479f2d3d9e8274e265504d Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 4 Mar 2012 15:46:41 -0800 Subject: [PATCH] add encoder and decoder functions to api --- makefile | 2 +- src/jsx.erl | 11 +++++++++++ src/jsx_to_json.erl | 4 ++-- src/jsx_to_term.erl | 2 +- src/jsx_verify.erl | 4 ++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/makefile b/makefile index d6d2f8c..5dccc0f 100644 --- a/makefile +++ b/makefile @@ -5,4 +5,4 @@ clean: ./rebar clean test: clean - ./rebar --jobs=1 eunit \ No newline at end of file + ./rebar --jobs=1 skip_deps=true eunit \ No newline at end of file diff --git a/src/jsx.erl b/src/jsx.erl index 2c5dc0a..baa6f66 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -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"). diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index aa9089f..5a6dada 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -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). diff --git a/src/jsx_to_term.erl b/src/jsx_to_term.erl index fac4b32..e5f434f 100644 --- a/src/jsx_to_term.erl +++ b/src/jsx_to_term.erl @@ -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). diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index ee21c74..ec18371 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -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.