diff --git a/.gitignore b/.gitignore index 6666f22..3698040 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .eunit deps erln8.config +jiffy.plt diff --git a/Makefile b/Makefile index 025486e..eb28390 100644 --- a/Makefile +++ b/Makefile @@ -29,15 +29,38 @@ build: depends $(REBAR) compile -eunit: +eunit: build $(REBAR) eunit skip_deps=true -check: build eunit +check: build eunit dialyzer %.beam: %.erl erlc -o test/ $< +DIALYZER = dialyzer +DIALYZER_OPTS ?= +JIFFY_PLT = jiffy.plt +ERLANG_DIALYZER_APPS ?= asn1 \ + compiler \ + crypto \ + edoc \ + erts \ + inets \ + kernel \ + mnesia \ + public_key \ + ssl \ + stdlib \ + syntax_tools \ + tools \ + xmerl +$(JIFFY_PLT): + @echo "Missing $(JIFFY_PLT). Please wait while a new PLT is compiled." + $(DIALYZER) --build_plt --apps $(ERLANG_DIALYZER_APPS) --output_plt $(JIFFY_PLT) + +dialyzer: $(JIFFY_PLT) build + @$(DIALYZER) $(DIALYZER_OPTS) --plts $(JIFFY_PLT) -r ebin .PHONY: all clean distclean depends build etap eunit check diff --git a/src/jiffy.erl b/src/jiffy.erl index 23a0c9d..7bd0206 100644 --- a/src/jiffy.erl +++ b/src/jiffy.erl @@ -5,13 +5,41 @@ -export([decode/1, decode/2, encode/1, encode/2]). -define(NOT_LOADED, not_loaded(?LINE)). --on_load(init/0). +%% More information on the JSON standard: http://json.org +-type json_value() :: null + | true + | false + | json_string() + | json_number() + | json_object() + | json_array(). + + +-type json_array() :: [json_value()]. +-type json_string() :: binary(). +-type json_number() :: integer() | float(). +-type json_object() :: {[{json_string(),json_value()}]}. + +-type decode_option() :: return_maps + | {bytes_per_iter, non_neg_integer()}. +-type decode_options() :: [decode_option()]. + +-type encode_option() :: unescape + | pretty + | force_utf8 + | {bytes_per_iter, non_neg_integer()}. +-type encode_options() :: [encode_option()]. + +-export_type([json_value/0]). +-on_load(init/0). + +-spec decode(iolist() | binary()) -> json_value(). decode(Data) -> decode(Data, []). - +-spec decode(iolist() | binary(), decode_options()) -> json_value(). decode(Data, Opts) when is_binary(Data), is_list(Opts) -> case nif_decode_init(Data, Opts) of {error, _} = Error -> @@ -26,11 +54,11 @@ decode(Data, Opts) when is_binary(Data), is_list(Opts) -> decode(Data, Opts) when is_list(Data) -> decode(iolist_to_binary(Data), Opts). - +-spec encode(json_value()) -> iolist(). encode(Data) -> encode(Data, []). - +-spec encode(json_value(), encode_options()) -> iolist(). encode(Data, Options) -> ForceUTF8 = lists:member(force_utf8, Options), case nif_encode_init(Data, Options) of