diff --git a/src/jsx.erl b/src/jsx.erl index dbf072b..fe48bc5 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -63,8 +63,14 @@ encode(Source, Config) -> jsx_to_json:to_json(Source, Config). %% old api, alias for encode/x +-spec to_json(Source::json_term()) -> json_text() | {incomplete, encoder()}. +-spec to_json(Source::json_term(), Config::jsx_to_json:config()) -> json_text() | {incomplete, encoder()}. + to_json(Source) -> encode(Source, []). to_json(Source, Config) -> encode(Source, Config). + +-spec term_to_json(Source::json_term()) -> json_text() | {incomplete, encoder()}. +-spec term_to_json(Source::json_term(), Config::jsx_to_json:config()) -> json_text() | {incomplete, encoder()}. term_to_json(Source) -> encode(Source, []). term_to_json(Source, Config) -> encode(Source, Config). @@ -94,8 +100,15 @@ decode(Source, Config) -> jsx_to_term:to_term(Source, Config). %% old api, alias for to_term/x +-spec to_term(Source::json_text()) -> json_term() | {incomplete, decoder()}. +-spec to_term(Source::json_text(), Config::jsx_to_term:config()) -> json_term() | {incomplete, decoder()}. + to_term(Source) -> decode(Source, []). to_term(Source, Config) -> decode(Source, Config). + +-spec json_to_term(Source::json_text()) -> json_term() | {incomplete, decoder()}. +-spec json_to_term(Source::json_text(), Config::jsx_to_term:config()) -> json_term() | {incomplete, decoder()}. + json_to_term(Source) -> decode(Source, []). json_to_term(Source, Config) -> decode(Source, Config). diff --git a/src/jsx_config.erl b/src/jsx_config.erl index 9c90aec..2113330 100644 --- a/src/jsx_config.erl +++ b/src/jsx_config.erl @@ -35,9 +35,12 @@ %% parsing of jsx config +-spec parse_config(Config::proplists:proplist()) -> config(). + parse_config(Config) -> parse_config(Config, #config{}). + parse_config([], Config) -> Config; parse_config([replaced_bad_utf8|Rest], Config) -> @@ -101,6 +104,7 @@ parse_config([ignore_bad_escapes|Rest], Config) -> parse_config(Options, Config) -> erlang:error(badarg, [Options, Config]). +-spec config_to_list(Config::config()) -> proplists:proplist(). config_to_list(Config) -> lists:map( @@ -115,6 +119,7 @@ config_to_list(Config) -> ) ). +-spec valid_flags() -> [atom()]. valid_flags() -> [ @@ -141,6 +146,7 @@ valid_flags() -> ignore_bad_escapes %% ignored_bad_escapes ]. +-spec extract_config(Config::proplists:proplist()) -> proplists:proplist(). extract_config(Config) -> extract_parser_config(Config, []). @@ -305,4 +311,4 @@ config_to_list_test_() -> fake_error_handler(_, _, _) -> ok. --endif. \ No newline at end of file +-endif. diff --git a/src/jsx_config.hrl b/src/jsx_config.hrl index 642dcf5..52c3ac4 100644 --- a/src/jsx_config.hrl +++ b/src/jsx_config.hrl @@ -11,4 +11,6 @@ pre_encode = false, error_handler = false, incomplete_handler = false -}). \ No newline at end of file +}). + +-type config() :: #config{}. diff --git a/src/jsx_decoder.erl b/src/jsx_decoder.erl index 18ce26a..4908f4b 100644 --- a/src/jsx_decoder.erl +++ b/src/jsx_decoder.erl @@ -48,7 +48,7 @@ decoder(Handler, State, Config) -> Acc::any(), Stack::list(atom()), Config::jsx:config() - ) -> jsx:decoder(). + ) -> jsx:decoder() | {incomplete, _}. resume(Rest, State, Handler, Acc, Stack, Config) -> case State of @@ -2097,4 +2097,4 @@ custom_incomplete_handler_test_() -> ]. --endif. \ No newline at end of file +-endif. diff --git a/src/jsx_parser.erl b/src/jsx_parser.erl index 85492a6..f3affaa 100644 --- a/src/jsx_parser.erl +++ b/src/jsx_parser.erl @@ -36,12 +36,12 @@ parser(Handler, State, Config) -> %% resume allows continuation from interrupted decoding without having to explicitly export %% all states -spec resume( - Rest::binary(), + Rest::list(), %% was binary(), State::atom(), Handler::{atom(), any()}, Stack::list(atom()), Config::jsx:config() - ) -> jsx:parser(). + ) -> jsx:parser() | {incomplete, _}. resume(Rest, State, Handler, Stack, Config) -> case State of @@ -208,8 +208,12 @@ clean_string(Bin, Tokens, Handler, Stack, Config) -> %% for raw input +-spec init(proplists:proplist()) -> list(). + init([]) -> []. +-spec handle_event(Event::any(), Acc::list()) -> list(). + handle_event(end_json, State) -> lists:reverse(State); handle_event(Event, State) -> [Event] ++ State. @@ -313,4 +317,4 @@ raw_test_() -> ]. --endif. \ No newline at end of file +-endif. diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index 177a32c..7e5aaf2 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -34,6 +34,7 @@ }). -type config() :: list(). +-export_type([config/0]). -spec to_json(Source::any(), Config::config()) -> binary(). @@ -85,10 +86,12 @@ parse_config([], Config) -> -define(newline, <<"\n">>). +-type state() :: {any(), unicode:charlist(), #config{}}. +-spec init(Config::proplists:proplist()) -> state(). init(Config) -> {start, [], parse_config(Config)}. - +-spec handle_event(Event::any(), State::state()) -> state(). handle_event(Event, {start, Acc, Config}) -> case Event of @@ -304,4 +307,4 @@ handle_event_test_() -> ]. --endif. \ No newline at end of file +-endif. diff --git a/src/jsx_to_term.erl b/src/jsx_to_term.erl index 52bc724..fad898a 100644 --- a/src/jsx_to_term.erl +++ b/src/jsx_to_term.erl @@ -33,6 +33,8 @@ }). -type config() :: list(). +-export_type([config/0]). + -type json_value() :: list({binary(), json_value()}) | list(json_value()) @@ -72,9 +74,12 @@ parse_config([K|Rest] = Options, Config) -> parse_config([], Config) -> Config. +-type state() :: {[any()], #config{}}. +-spec init(Config::proplists:proplist()) -> state(). init(Config) -> {[[]], parse_config(Config)}. +-spec handle_event(Event::any(), State::state()) -> state(). handle_event(end_json, {[[Terms]], _Config}) -> Terms; diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index 90251b3..34c50e5 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -32,6 +32,7 @@ }). -type config() :: []. +-export_type([config/0]). -spec is_json(Source::binary(), Config::config()) -> true | false. @@ -72,10 +73,14 @@ parse_config([K|Rest] = Options, Config) -> parse_config([], Config) -> Config. +-type state() :: {#config{}, any()}. +-spec init(Config::proplists:proplist()) -> state(). init(Config) -> {parse_config(Config), []}. +-spec handle_event(Event::any(), State::state()) -> state(). + handle_event(end_json, _) -> true; handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State; @@ -165,4 +170,4 @@ handle_event_test_() -> ]. --endif. \ No newline at end of file +-endif.