|
@@ -42,11 +42,13 @@
|
|
|
json_array/0,
|
|
|
json_object/0,
|
|
|
json_object_members/0,
|
|
|
- json_boolean/0
|
|
|
+ json_boolean/0,
|
|
|
+
|
|
|
+ encode_option/0
|
|
|
]).
|
|
|
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
-%% Types
|
|
|
+%% Types & Macros
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
-type json_value() :: json_number() | json_string() | json_array() | json_object() | json_boolean() | null.
|
|
|
-type json_boolean() :: boolean().
|
|
@@ -56,10 +58,10 @@
|
|
|
-type json_object() :: {json_object_members()}.
|
|
|
-type json_object_members() :: [{json_string(), json_value()}].
|
|
|
|
|
|
--type option() :: {atom(), atom()|boolean()}.
|
|
|
--export_type([option/0]).
|
|
|
+-type encode_option() :: native_utf8.
|
|
|
+%% native_utf8: Encodes UTF-8 characters as a human-readable(non-escaped) string
|
|
|
|
|
|
--define(DEFAULT_OPTIONS, []).
|
|
|
+-define(DEFAULT_ENCODE_OPTIONS, []).
|
|
|
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Exported Functions
|
|
@@ -71,7 +73,7 @@
|
|
|
%% ```
|
|
|
%% > jsone:decode(<<"1">>).
|
|
|
%% 1
|
|
|
-%%
|
|
|
+%%
|
|
|
%% > jsone:decode(<<"wrong json">>).
|
|
|
%% ** exception error: bad argument
|
|
|
%% in function jsone_decode:number_integer_part/4
|
|
@@ -93,7 +95,7 @@ decode(Json) ->
|
|
|
%% ```
|
|
|
%% > jsone:try_decode(<<"[1,2,3] \"next value\"">>).
|
|
|
%% {ok,[1,2,3],<<" \"next value\"">>}
|
|
|
-%%
|
|
|
+%%
|
|
|
%% > jsone:try_decode(<<"wrong json">>).
|
|
|
%% {error,{badarg,[{jsone_decode,number_integer_part,
|
|
|
%% [<<"wrong json">>,1,[],<<>>],
|
|
@@ -103,6 +105,11 @@ decode(Json) ->
|
|
|
try_decode(Json) ->
|
|
|
jsone_decode:decode(Json).
|
|
|
|
|
|
+%% @equiv encode(JsonValue, [])
|
|
|
+-spec encode(json_value()) -> binary().
|
|
|
+encode(JsonValue) ->
|
|
|
+ encode(JsonValue, ?DEFAULT_ENCODE_OPTIONS).
|
|
|
+
|
|
|
%% @doc Encodes an erlang term into json text (a utf8 encoded binary)
|
|
|
%%
|
|
|
%% Raises an error exception if input is not an instance of type `json_value()'
|
|
@@ -110,18 +117,14 @@ try_decode(Json) ->
|
|
|
%% ```
|
|
|
%% > jsone:encode([1, null, 2]).
|
|
|
%% <<"[1,null,2]">>
|
|
|
-%%
|
|
|
+%%
|
|
|
%% > jsone:encode([1, hoge, 2]). % 'hoge' atom is not a json value
|
|
|
%% ** exception error: bad argument
|
|
|
%% in function jsone_encode:value/3
|
|
|
%% called as jsone_encode:value(hoge,[{array_values,[2]}],<<"[1,">>)
|
|
|
%% in call from jsone:encode/1 (src/jsone.erl, line 97)
|
|
|
%% '''
|
|
|
--spec encode(json_value()) -> binary().
|
|
|
-encode(JsonValue) ->
|
|
|
- encode(JsonValue, ?DEFAULT_OPTIONS).
|
|
|
-
|
|
|
--spec encode(json_value(), [option()]) -> binary().
|
|
|
+-spec encode(json_value(), [encode_option()]) -> binary().
|
|
|
encode(JsonValue, Options) ->
|
|
|
try
|
|
|
{ok, Binary} = try_encode(JsonValue, Options),
|
|
@@ -131,21 +134,22 @@ encode(JsonValue, Options) ->
|
|
|
erlang:raise(error, Reason, [StackItem | erlang:get_stacktrace()])
|
|
|
end.
|
|
|
|
|
|
+%% @equiv try_encode(JsonValue, [])
|
|
|
+-spec try_encode(json_value()) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
+try_encode(JsonValue) ->
|
|
|
+ jsone_encode:encode(JsonValue, ?DEFAULT_ENCODE_OPTIONS).
|
|
|
+
|
|
|
%% @doc Encodes an erlang term into json text (a utf8 encoded binary)
|
|
|
%%
|
|
|
%% ```
|
|
|
%% > jsone:try_encode([1, null, 2]).
|
|
|
%% {ok,<<"[1,null,2]">>}
|
|
|
-%%
|
|
|
+%%
|
|
|
%% > jsone:try_encode([1, hoge, 2]). % 'hoge' atom is not a json value
|
|
|
%% {error,{badarg,[{jsone_encode,value,
|
|
|
%% [hoge,[{array_values,[2]}],<<"[1,">>],
|
|
|
%% [{line,86}]}]}}
|
|
|
%% '''
|
|
|
--spec try_encode(json_value()) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
-try_encode(JsonValue) ->
|
|
|
- jsone_encode:encode(JsonValue, ?DEFAULT_OPTIONS).
|
|
|
-
|
|
|
--spec try_encode(json_value(), [option()]) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
+-spec try_encode(json_value(), [encode_option()]) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
try_encode(JsonValue, Options) ->
|
|
|
jsone_encode:encode(JsonValue, Options).
|