diff --git a/src/jiffy.erl b/src/jiffy.erl index 23a0c9d..39c3aeb 100644 --- a/src/jiffy.erl +++ b/src/jiffy.erl @@ -8,10 +8,42 @@ -on_load(init/0). +-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 + | use_nil + | {null_term, any()} + | {bytes_per_iter, non_neg_integer()}. + +-type encode_option() :: uescape + | pretty + | force_utf8 + | escape_forward_slashes + | {bytes_per_iter, non_neg_integer()}. + +-type decode_options() :: [decode_option()]. +-type encode_options() :: [encode_option()]. + +-export_type([json_value/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 -> @@ -27,10 +59,12 @@ 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