Added typespecs on all exported functions

Made sure all exported types do have a typespec is useful in build scenarios
where the erlang compile options
[warnings_as_errors, warn_missing_spec]
are used.

A few type errors found by Dialyzer are corrected too.

	modified:   src/jsx.erl
	modified:   src/jsx_config.erl
	modified:   src/jsx_config.hrl
	modified:   src/jsx_decoder.erl
	modified:   src/jsx_parser.erl
	modified:   src/jsx_to_json.erl
	modified:   src/jsx_to_term.erl
	modified:   src/jsx_verify.erl
This commit is contained in:
Ola Backstrom 2013-09-19 17:04:38 +02:00
parent a39baa4efc
commit 2943116c08
8 changed files with 48 additions and 10 deletions

View file

@ -63,8 +63,14 @@ encode(Source, Config) -> jsx_to_json:to_json(Source, Config).
%% old api, alias for encode/x %% 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) -> encode(Source, []).
to_json(Source, Config) -> encode(Source, Config). 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) -> encode(Source, []).
term_to_json(Source, Config) -> encode(Source, Config). 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 %% 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) -> decode(Source, []).
to_term(Source, Config) -> decode(Source, Config). 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) -> decode(Source, []).
json_to_term(Source, Config) -> decode(Source, Config). json_to_term(Source, Config) -> decode(Source, Config).

View file

@ -35,9 +35,12 @@
%% parsing of jsx config %% parsing of jsx config
-spec parse_config(Config::proplists:proplist()) -> config().
parse_config(Config) -> parse_config(Config) ->
parse_config(Config, #config{}). parse_config(Config, #config{}).
parse_config([], Config) -> parse_config([], Config) ->
Config; Config;
parse_config([replaced_bad_utf8|Rest], Config) -> parse_config([replaced_bad_utf8|Rest], Config) ->
@ -101,6 +104,7 @@ parse_config([ignore_bad_escapes|Rest], Config) ->
parse_config(Options, Config) -> parse_config(Options, Config) ->
erlang:error(badarg, [Options, Config]). erlang:error(badarg, [Options, Config]).
-spec config_to_list(Config::config()) -> proplists:proplist().
config_to_list(Config) -> config_to_list(Config) ->
lists:map( lists:map(
@ -115,6 +119,7 @@ config_to_list(Config) ->
) )
). ).
-spec valid_flags() -> [atom()].
valid_flags() -> valid_flags() ->
[ [
@ -141,6 +146,7 @@ valid_flags() ->
ignore_bad_escapes %% ignored_bad_escapes ignore_bad_escapes %% ignored_bad_escapes
]. ].
-spec extract_config(Config::proplists:proplist()) -> proplists:proplist().
extract_config(Config) -> extract_config(Config) ->
extract_parser_config(Config, []). extract_parser_config(Config, []).
@ -305,4 +311,4 @@ config_to_list_test_() ->
fake_error_handler(_, _, _) -> ok. fake_error_handler(_, _, _) -> ok.
-endif. -endif.

View file

@ -11,4 +11,6 @@
pre_encode = false, pre_encode = false,
error_handler = false, error_handler = false,
incomplete_handler = false incomplete_handler = false
}). }).
-type config() :: #config{}.

View file

@ -48,7 +48,7 @@ decoder(Handler, State, Config) ->
Acc::any(), Acc::any(),
Stack::list(atom()), Stack::list(atom()),
Config::jsx:config() Config::jsx:config()
) -> jsx:decoder(). ) -> jsx:decoder() | {incomplete, _}.
resume(Rest, State, Handler, Acc, Stack, Config) -> resume(Rest, State, Handler, Acc, Stack, Config) ->
case State of case State of
@ -2097,4 +2097,4 @@ custom_incomplete_handler_test_() ->
]. ].
-endif. -endif.

View file

@ -36,12 +36,12 @@ parser(Handler, State, Config) ->
%% resume allows continuation from interrupted decoding without having to explicitly export %% resume allows continuation from interrupted decoding without having to explicitly export
%% all states %% all states
-spec resume( -spec resume(
Rest::binary(), Rest::list(), %% was binary(),
State::atom(), State::atom(),
Handler::{atom(), any()}, Handler::{atom(), any()},
Stack::list(atom()), Stack::list(atom()),
Config::jsx:config() Config::jsx:config()
) -> jsx:parser(). ) -> jsx:parser() | {incomplete, _}.
resume(Rest, State, Handler, Stack, Config) -> resume(Rest, State, Handler, Stack, Config) ->
case State of case State of
@ -208,8 +208,12 @@ clean_string(Bin, Tokens, Handler, Stack, Config) ->
%% for raw input %% for raw input
-spec init(proplists:proplist()) -> list().
init([]) -> []. init([]) -> [].
-spec handle_event(Event::any(), Acc::list()) -> list().
handle_event(end_json, State) -> lists:reverse(State); handle_event(end_json, State) -> lists:reverse(State);
handle_event(Event, State) -> [Event] ++ State. handle_event(Event, State) -> [Event] ++ State.
@ -313,4 +317,4 @@ raw_test_() ->
]. ].
-endif. -endif.

View file

@ -34,6 +34,7 @@
}). }).
-type config() :: list(). -type config() :: list().
-export_type([config/0]).
-spec to_json(Source::any(), Config::config()) -> binary(). -spec to_json(Source::any(), Config::config()) -> binary().
@ -85,10 +86,12 @@ parse_config([], Config) ->
-define(newline, <<"\n">>). -define(newline, <<"\n">>).
-type state() :: {any(), unicode:charlist(), #config{}}.
-spec init(Config::proplists:proplist()) -> state().
init(Config) -> {start, [], parse_config(Config)}. init(Config) -> {start, [], parse_config(Config)}.
-spec handle_event(Event::any(), State::state()) -> state().
handle_event(Event, {start, Acc, Config}) -> handle_event(Event, {start, Acc, Config}) ->
case Event of case Event of
@ -304,4 +307,4 @@ handle_event_test_() ->
]. ].
-endif. -endif.

View file

@ -33,6 +33,8 @@
}). }).
-type config() :: list(). -type config() :: list().
-export_type([config/0]).
-type json_value() :: list({binary(), json_value()}) -type json_value() :: list({binary(), json_value()})
| list(json_value()) | list(json_value())
@ -72,9 +74,12 @@ parse_config([K|Rest] = Options, Config) ->
parse_config([], Config) -> parse_config([], Config) ->
Config. Config.
-type state() :: {[any()], #config{}}.
-spec init(Config::proplists:proplist()) -> state().
init(Config) -> {[[]], parse_config(Config)}. init(Config) -> {[[]], parse_config(Config)}.
-spec handle_event(Event::any(), State::state()) -> state().
handle_event(end_json, {[[Terms]], _Config}) -> Terms; handle_event(end_json, {[[Terms]], _Config}) -> Terms;

View file

@ -32,6 +32,7 @@
}). }).
-type config() :: []. -type config() :: [].
-export_type([config/0]).
-spec is_json(Source::binary(), Config::config()) -> true | false. -spec is_json(Source::binary(), Config::config()) -> true | false.
@ -72,10 +73,14 @@ parse_config([K|Rest] = Options, Config) ->
parse_config([], Config) -> parse_config([], Config) ->
Config. Config.
-type state() :: {#config{}, any()}.
-spec init(Config::proplists:proplist()) -> state().
init(Config) -> {parse_config(Config), []}. init(Config) -> {parse_config(Config), []}.
-spec handle_event(Event::any(), State::state()) -> state().
handle_event(end_json, _) -> true; handle_event(end_json, _) -> true;
handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State; handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State;
@ -165,4 +170,4 @@ handle_event_test_() ->
]. ].
-endif. -endif.