commit
c7fcfbcb51
6 changed files with 39 additions and 25 deletions
|
@ -34,13 +34,16 @@
|
|||
-export([to_term/1, to_term/2]).
|
||||
|
||||
-export_type([json_term/0, json_text/0, token/0]).
|
||||
-export_type([encoder/0, decoder/0, parser/0, internal_state/0]).
|
||||
-export_type([config/0, encoder/0, decoder/0, parser/0, internal_state/0]).
|
||||
|
||||
|
||||
-ifdef(TEST).
|
||||
-include("jsx_tests.hrl").
|
||||
-else.
|
||||
-include("jsx_config.hrl").
|
||||
-endif.
|
||||
|
||||
-type config() :: #config{}.
|
||||
|
||||
-type json_term()
|
||||
:: [{binary() | atom(), json_term()}]
|
||||
|
@ -54,7 +57,6 @@
|
|||
|
||||
-type json_text() :: binary().
|
||||
|
||||
|
||||
-spec encode(Source::json_term()) -> json_text() | {incomplete, encoder()}.
|
||||
-spec encode(Source::json_term(), Config::jsx_to_json:config()) -> json_text() | {incomplete, encoder()}.
|
||||
|
||||
|
@ -178,3 +180,4 @@ resume(Term, {decoder, State, Handler, Acc, Stack}, Config) ->
|
|||
jsx_decoder:resume(Term, State, Handler, Acc, Stack, jsx_config:parse_config(Config));
|
||||
resume(Term, {parser, State, Handler, Stack}, Config) ->
|
||||
jsx_parser:resume(Term, State, Handler, Stack, jsx_config:parse_config(Config)).
|
||||
|
||||
|
|
|
@ -33,9 +33,21 @@
|
|||
|
||||
-include("jsx_config.hrl").
|
||||
|
||||
-type handler_type(Handler) ::
|
||||
fun((jsx:json_text() | end_stream |
|
||||
jsx:json_term(),
|
||||
{decoder, any(), module(), null | list(), list()} |
|
||||
{parser, any(), module(), list()} |
|
||||
{encoder, any(), module()},
|
||||
list({pre_encode, fun((any()) -> any())} |
|
||||
{error_handler, Handler} |
|
||||
{incomplete_handler, Handler} |
|
||||
atom())) -> any()).
|
||||
-type handler() :: handler_type(handler()).
|
||||
-export_type([handler/0]).
|
||||
|
||||
%% parsing of jsx config
|
||||
-spec parse_config(Config::proplists:proplist()) -> config().
|
||||
-spec parse_config(Config::proplists:proplist()) -> jsx:config().
|
||||
|
||||
parse_config(Config) ->
|
||||
parse_config(Config, #config{}).
|
||||
|
@ -104,7 +116,7 @@ parse_config(Options, Config) ->
|
|||
erlang:error(badarg, [Options, Config]).
|
||||
|
||||
|
||||
-spec config_to_list(Config::config()) -> proplists:proplist().
|
||||
-spec config_to_list(Config::jsx:config()) -> proplists:proplist().
|
||||
|
||||
config_to_list(Config) ->
|
||||
lists:map(
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
-record(config, {
|
||||
replaced_bad_utf8 = false,
|
||||
escaped_forward_slashes = false,
|
||||
single_quoted_strings = false,
|
||||
unescaped_jsonp = false,
|
||||
comments = false,
|
||||
escaped_strings = false,
|
||||
dirty_strings = false,
|
||||
ignored_bad_escapes = false,
|
||||
explicit_end = false,
|
||||
pre_encode = false,
|
||||
error_handler = false,
|
||||
incomplete_handler = false
|
||||
replaced_bad_utf8 = false :: boolean(),
|
||||
escaped_forward_slashes = false :: boolean(),
|
||||
single_quoted_strings = false :: boolean(),
|
||||
unescaped_jsonp = false :: boolean(),
|
||||
comments = false :: boolean(),
|
||||
escaped_strings = false :: boolean(),
|
||||
dirty_strings = false :: boolean(),
|
||||
ignored_bad_escapes = false :: boolean(),
|
||||
explicit_end = false :: boolean(),
|
||||
pre_encode = false :: false | fun((any()) -> any()),
|
||||
error_handler = false :: false | jsx_config:handler(),
|
||||
incomplete_handler = false :: false | jsx_config:handler()
|
||||
}).
|
||||
|
||||
-type config() :: #config{}.
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
-export([decoder/3, resume/6]).
|
||||
|
||||
|
||||
-spec decoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:decoder().
|
||||
-spec decoder(Handler::module(), State::any(), Config::list()) -> jsx:decoder().
|
||||
|
||||
decoder(Handler, State, Config) ->
|
||||
fun(JSON) -> start(JSON, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
|
||||
|
@ -48,7 +48,7 @@ decoder(Handler, State, Config) ->
|
|||
Acc::any(),
|
||||
Stack::list(atom()),
|
||||
Config::jsx:config()
|
||||
) -> jsx:decoder() | {incomplete, _}.
|
||||
) -> jsx:decoder() | {incomplete, jsx:decoder()}.
|
||||
|
||||
resume(Rest, State, Handler, Acc, Stack, Config) ->
|
||||
case State of
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
-export([encoder/3]).
|
||||
|
||||
-spec encoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:encoder().
|
||||
-spec encoder(Handler::module(), State::any(), Config::list()) -> jsx:encoder().
|
||||
|
||||
encoder(Handler, State, Config) ->
|
||||
fun(JSON) ->
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
-export([init/1, handle_event/2]).
|
||||
|
||||
|
||||
-spec parser(Handler::module(), State::any(), Config::jsx:config()) -> jsx:parser().
|
||||
-spec parser(Handler::module(), State::any(), Config::list()) -> jsx:parser().
|
||||
|
||||
parser(Handler, State, Config) ->
|
||||
fun(Tokens) -> value(Tokens, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
|
||||
|
@ -36,12 +36,12 @@ parser(Handler, State, Config) ->
|
|||
%% resume allows continuation from interrupted decoding without having to explicitly export
|
||||
%% all states
|
||||
-spec resume(
|
||||
Rest::list(), %% was binary(),
|
||||
Rest::jsx:token(),
|
||||
State::atom(),
|
||||
Handler::{atom(), any()},
|
||||
Stack::list(atom()),
|
||||
Config::jsx:config()
|
||||
) -> jsx:parser() | {incomplete, _}.
|
||||
) -> jsx:parser() | {incomplete, jsx:parser()}.
|
||||
|
||||
resume(Rest, State, Handler, Stack, Config) ->
|
||||
case State of
|
||||
|
@ -82,8 +82,8 @@ incomplete(State, Handler, Stack, Config=#config{incomplete_handler=F}) ->
|
|||
F([], {parser, State, Handler, Stack}, jsx_config:config_to_list(Config)).
|
||||
|
||||
|
||||
handle_event([], Handler, _Config) -> Handler;
|
||||
handle_event([Event|Rest], Handler, Config) -> handle_event(Rest, handle_event(Event, Handler, Config), Config);
|
||||
%handle_event([], Handler, _Config) -> Handler;
|
||||
%handle_event([Event|Rest], Handler, Config) -> handle_event(Rest, handle_event(Event, Handler, Config), Config);
|
||||
handle_event(Event, {Handler, State}, _Config) -> {Handler, Handler:handle_event(Event, State)}.
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue