is_json/2 and format/2 now both accept either a json text (binary) or a function that acts as a jsx iterator. specs updated to reflect

This commit is contained in:
alisdair sullivan 2011-07-04 19:25:09 -07:00
parent e088c6fd26
commit 0052480652
3 changed files with 38 additions and 32 deletions

View file

@ -40,7 +40,7 @@
| {utf16, little} | {utf16, little}
| utf32 | utf32
| {utf32, little} | {utf32, little}
}. }.
%% events emitted by the parser and component types %% events emitted by the parser and component types
@ -61,15 +61,25 @@
| {literal, null}. | {literal, null}.
%% this probably doesn't work properly -type jsx_iterator() :: jsx_decoder() | jsx_encoder().
-type jsx_decoder() :: fun((binary()) -> jsx_decoder_result()).
-type jsx_decoder_result() ::
{event, jsx_event(), fun(() -> jsx_decoder_result())} %% this probably doesn't work properly
| {incomplete, jsx_decoder()} -type jsx_decoder() :: fun((binary()) -> jsx_iterator_result()).
-type jsx_encoder() :: fun((list(jsx_event())) -> jsx_iterator_result()).
-type jsx_iterator_result() ::
{event, jsx_event(), fun(() -> jsx_iterator_result())}
| {incomplete, jsx_iterator()}
| {error, {badjson, binary()}}. | {error, {badjson, binary()}}.
-type supported_utf() :: utf8 -type supported_utf() :: utf8
| utf16 | utf16
| {utf16, little} | {utf16, little}

View file

@ -32,17 +32,13 @@
-include("jsx_format.hrl"). -include("jsx_format.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist()
; (F::jsx_iterator(), Opts::format_opts()) -> binary() | iolist().
format(JSON, OptsList) when is_binary(JSON) ->
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist(). P = jsx:decoder(extract_parser_opts(OptsList)),
format(fun() -> P(JSON) end, OptsList);
format(JSON, Opts) when is_binary(JSON) ->
P = jsx:decoder(extract_parser_opts(Opts)),
format(fun() -> P(JSON) end, Opts);
format(F, OptsList) when is_function(F) -> format(F, OptsList) when is_function(F) ->
Opts = parse_opts(OptsList, #format_opts{}), Opts = parse_opts(OptsList, #format_opts{}),
{Continue, String} = format_something(F(), Opts, 0), {Continue, String} = format_something(F(), Opts, 0),
@ -198,6 +194,7 @@ space(Opts) ->
%% eunit tests %% eunit tests
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
minify_test_() -> minify_test_() ->
[ [

View file

@ -31,19 +31,17 @@
-include("jsx_common.hrl"). -include("jsx_common.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false
; (F::jsx_iterator(), Opts::verify_opts()) -> true | false.
is_json(JSON, OptsList) when is_binary(JSON) ->
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false. P = jsx:decoder(extract_parser_opts(OptsList)),
is_json(fun() -> P(JSON) end, OptsList);
is_json(JSON, Opts) -> is_json(F, OptsList) when is_function(F) ->
P = jsx:decoder(extract_parser_opts(Opts)), case proplists:get_value(strict, OptsList, false) of
case proplists:get_value(strict, Opts, false) of true -> collect_strict(F(), [[]])
true -> collect_strict(P(JSON), [[]]) ; false -> collect(F(), [[]])
; false -> collect(P(JSON), [[]])
end. end.
@ -107,6 +105,7 @@ collect(_, _) ->
%% eunit tests %% eunit tests
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
true_test_() -> true_test_() ->
[ [