From 0052480652793c6b9cc7425a31eb91a860a0f42b Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Mon, 4 Jul 2011 19:25:09 -0700 Subject: [PATCH] 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 --- src/jsx_common.hrl | 30 ++++++++++++++++++++---------- src/jsx_format.erl | 15 ++++++--------- src/jsx_verify.erl | 25 ++++++++++++------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/jsx_common.hrl b/src/jsx_common.hrl index cdc660c..07a7c98 100644 --- a/src/jsx_common.hrl +++ b/src/jsx_common.hrl @@ -35,12 +35,12 @@ -type jsx_opt() :: {escaped_unicode, ascii | codepoint | none} | {multi_term, true | false} | {encoding, auto - | utf8 - | utf16 - | {utf16, little} - | utf32 - | {utf32, little} -}. + | utf8 + | utf16 + | {utf16, little} + | utf32 + | {utf32, little} + }. %% events emitted by the parser and component types @@ -60,16 +60,26 @@ | {literal, false} | {literal, null}. + +-type jsx_iterator() :: jsx_decoder() | jsx_encoder(). + %% this probably doesn't work properly --type jsx_decoder() :: fun((binary()) -> jsx_decoder_result()). +-type jsx_decoder() :: fun((binary()) -> jsx_iterator_result()). --type jsx_decoder_result() :: - {event, jsx_event(), fun(() -> jsx_decoder_result())} - | {incomplete, jsx_decoder()} +-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()}}. + + + + + -type supported_utf() :: utf8 | utf16 | {utf16, little} diff --git a/src/jsx_format.erl b/src/jsx_format.erl index 85ca14a..652fef4 100644 --- a/src/jsx_format.erl +++ b/src/jsx_format.erl @@ -32,17 +32,13 @@ -include("jsx_format.hrl"). --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). --endif. - - --spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist(). +-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist() + ; (F::jsx_iterator(), Opts::format_opts()) -> binary() | iolist(). -format(JSON, Opts) when is_binary(JSON) -> - P = jsx:decoder(extract_parser_opts(Opts)), - format(fun() -> P(JSON) end, Opts); +format(JSON, OptsList) when is_binary(JSON) -> + P = jsx:decoder(extract_parser_opts(OptsList)), + format(fun() -> P(JSON) end, OptsList); format(F, OptsList) when is_function(F) -> Opts = parse_opts(OptsList, #format_opts{}), {Continue, String} = format_something(F(), Opts, 0), @@ -198,6 +194,7 @@ space(Opts) -> %% eunit tests -ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). minify_test_() -> [ diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index e0fbc08..957ed54 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -31,19 +31,17 @@ -include("jsx_common.hrl"). --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). --endif. - - - --spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false. - -is_json(JSON, Opts) -> - P = jsx:decoder(extract_parser_opts(Opts)), - case proplists:get_value(strict, Opts, false) of - true -> collect_strict(P(JSON), [[]]) - ; false -> collect(P(JSON), [[]]) + +-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) -> + P = jsx:decoder(extract_parser_opts(OptsList)), + is_json(fun() -> P(JSON) end, OptsList); +is_json(F, OptsList) when is_function(F) -> + case proplists:get_value(strict, OptsList, false) of + true -> collect_strict(F(), [[]]) + ; false -> collect(F(), [[]]) end. @@ -107,6 +105,7 @@ collect(_, _) -> %% eunit tests -ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). true_test_() -> [