new is_term function that mirrors is_json for erlang terms

This commit is contained in:
alisdair sullivan 2012-03-04 18:40:00 -08:00
parent 620e6c7d91
commit ec295a888b
6 changed files with 64 additions and 27 deletions

View file

@ -23,7 +23,7 @@
-module(jsx_verify).
-export([is_json/2]).
-export([is_json/2, is_term/2]).
-export([init/1, handle_event/2]).
@ -34,18 +34,21 @@
-type opts() :: [].
-spec is_json(Source::(binary() | list()), Opts::opts()) -> binary().
-spec is_json(Source::binary(), Opts::opts()) -> true | false.
is_json(Source, Opts) when is_list(Source) andalso is_list(Opts) ->
try (jsx:encoder(?MODULE, init(Opts), Opts))(Source)
catch error:badarg -> false
end;
is_json(Source, Opts) when is_binary(Source) andalso is_list(Opts) ->
try (jsx:decoder(?MODULE, init(Opts), Opts))(Source)
is_json(Source, Opts) when is_list(Opts) ->
try (jsx:decoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source)
catch error:badarg -> false
end.
-spec is_term(Source::any(), Opts::opts()) -> true | false.
is_term(Source, Opts) when is_list(Opts) ->
try (jsx:encoder(?MODULE, init(Opts), jsx_utils:extract_opts(Opts)))(Source)
catch error:badarg -> false
end.
parse_opts(Opts) -> parse_opts(Opts, #opts{}).