diff --git a/src/jsx.erl b/src/jsx.erl index b389d8b..7d87092 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -27,7 +27,7 @@ -export([to_term/1, to_term/2]). -export([is_json/1, is_json/2, is_term/1, is_term/2]). -export([format/1, format/2, minify/1, prettify/1]). --export([encoder/3, decoder/3]). +-export([encoder/3, decoder/3, parser/3]). %% old api -export([term_to_json/1, term_to_json/2, json_to_term/1, json_to_term/2]). @@ -37,8 +37,8 @@ -endif. --type json() :: list({binary(), json()}) - | list(json()) +-type json_term() :: list({binary(), json_term()}) + | list(json_term()) | true | false | null @@ -46,9 +46,11 @@ | float() | binary(). +-type json_text() :: binary(). --spec to_json(Source::json()) -> binary(). --spec to_json(Source::json(), Opts::jsx_to_json:opts()) -> binary(). + +-spec to_json(Source::json_term()) -> json_text(). +-spec to_json(Source::json_term(), Opts::jsx_to_json:opts()) -> json_text(). to_json(Source) -> to_json(Source, []). @@ -61,26 +63,26 @@ term_to_json(Source) -> to_json(Source, []). term_to_json(Source, Opts) -> to_json(Source, Opts). --spec format(Source::binary()) -> binary(). --spec format(Source::binary(), Opts::jsx_to_json:opts()) -> binary(). +-spec format(Source::json_text()) -> json_text(). +-spec format(Source::json_text(), Opts::jsx_to_json:opts()) -> json_text(). format(Source) -> format(Source, []). format(Source, Opts) -> jsx_to_json:format(Source, Opts). --spec minify(Source::binary()) -> binary(). +-spec minify(Source::json_text()) -> json_text(). minify(Source) -> format(Source, []). --spec prettify(Source::binary()) -> binary(). +-spec prettify(Source::json_text()) -> json_text(). prettify(Source) -> format(Source, [space, {indent, 2}]). --spec to_term(Source::binary()) -> json(). --spec to_term(Source::binary(), Opts::jsx_to_term:opts()) -> json(). +-spec to_term(Source::json_text()) -> json_term(). +-spec to_term(Source::json_text(), Opts::jsx_to_term:opts()) -> json_term(). to_term(Source) -> to_term(Source, []). @@ -109,16 +111,49 @@ is_term(Source) -> is_term(Source, []). is_term(Source, Opts) -> jsx_verify:is_term(Source, Opts). --spec decoder(Handler::module(), State::any(), Opts::list()) -> fun(). +-type decoder() :: fun((json_text() | end_stream) -> any()). + +-spec decoder(Handler::module(), State::any(), Opts::list()) -> decoder(). decoder(Handler, State, Opts) -> jsx_decoder:decoder(Handler, State, Opts). --spec encoder(Handler::module(), State::any(), Opts::list()) -> fun(). +-type encoder() :: fun((json_term() | end_stream) -> any()). + +-spec encoder(Handler::module(), State::any(), Opts::list()) -> encoder(). encoder(Handler, State, Opts) -> jsx_encoder:encoder(Handler, State, Opts). +-type token() :: [token()] + | start_object + | end_object + | start_array + | end_array + | {key, binary()} + | {string, binary()} + | binary() + | {number, integer() | float()} + | {integer, integer()} + | {float, float()} + | integer() + | float() + | {literal, true} + | {literal, false} + | {literal, null} + | true + | false + | null + | end_json. + + +-type parser() :: fun((token() | end_stream) -> any()). + +-spec parser(Handler::module(), State::any(), Opts::list()) -> parser(). + +parser(Handler, State, Opts) -> jsx_parser:parser(Handler, State, Opts). + + -ifdef(TEST). -include_lib("eunit/include/eunit.hrl").