|
@@ -6,10 +6,8 @@
|
|
|
* [Function Index](#index)
|
|
|
* [Function Details](#functions)
|
|
|
|
|
|
-
|
|
|
JSON decoding/encoding module.
|
|
|
|
|
|
-
|
|
|
<a name="types"></a>
|
|
|
|
|
|
## Data Types ##
|
|
@@ -20,14 +18,11 @@ JSON decoding/encoding module.
|
|
|
### <a name="type-decode_option">decode_option()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
decode_option() = {object_format, tuple | proplist | map}
|
|
|
</code></pre>
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- object_format: <br />
|
|
|
+ object_format: <br />
|
|
|
- Decoded JSON object format <br />
|
|
|
- `tuple`: An object is decoded as `{[]}` if it is empty, otherwise `{[{Key, Value}]}`. <br />
|
|
|
- `proplist`: An object is decoded as `[{}]` if it is empty, otherwise `[{Key, Value}]`. <br />
|
|
@@ -39,30 +34,21 @@ decode_option() = {object_format, tuple | proplist | map}
|
|
|
### <a name="type-encode_option">encode_option()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
encode_option() = native_utf8 | {float_format, [<a href="#type-float_format_option">float_format_option()</a>]} | {space, non_neg_integer()} | {indent, non_neg_integer()}
|
|
|
</code></pre>
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
`native_utf8`: <br />
|
|
|
- Encodes UTF-8 characters as a human-readable(non-escaped) string <br />
|
|
|
|
|
|
-
|
|
|
-
|
|
|
`{float_format, Optoins}`:
|
|
|
- Encodes a `float()` value in the format which specified by `Options` <br />
|
|
|
- default: `[{scientific, 20}]` <br />
|
|
|
|
|
|
-
|
|
|
-
|
|
|
`{space, N}`: <br />
|
|
|
- Inserts `N` spaces after every commna and colon <br />
|
|
|
- default: `0` <br />
|
|
|
|
|
|
-
|
|
|
`{indent, N}`: <br />
|
|
|
- Inserts a newline and `N` spaces for each level of indentation <br />
|
|
|
- default: `0` <br />
|
|
@@ -72,29 +58,19 @@ encode_option() = native_utf8 | {float_format, [<a href="#type-float_format_opti
|
|
|
### <a name="type-float_format_option">float_format_option()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
float_format_option() = {scientific, Decimals::0..249} | {decimals, Decimals::0..253} | compact
|
|
|
</code></pre>
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
`scientific`: <br />
|
|
|
- The float will be formatted using scientific notation with `Decimals` digits of precision. <br />
|
|
|
|
|
|
-
|
|
|
-
|
|
|
`decimals`: <br />
|
|
|
- The encoded string will contain at most `Decimals` number of digits past the decimal point. <br />
|
|
|
- If `compact` is provided the trailing zeros at the end of the string are truncated. <br />
|
|
|
|
|
|
-
|
|
|
-
|
|
|
For more details, see [erlang:flaot_to_list/2](http://erlang.org/doc/man/erlang.md#float_to_list-2).
|
|
|
|
|
|
-
|
|
|
-
|
|
|
```
|
|
|
> jsone:encode(1.23).
|
|
|
<<"1.22999999999999998224e+00">>
|
|
@@ -110,11 +86,9 @@ For more details, see [erlang:flaot_to_list/2](http://erlang.org/doc/man/erlang.
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_array">json_array()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_array() = [<a href="#type-json_value">json_value()</a>]
|
|
|
</code></pre>
|
|
@@ -122,11 +96,9 @@ json_array() = [<a href="#type-json_value">json_value()</a>]
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_boolean">json_boolean()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_boolean() = boolean()
|
|
|
</code></pre>
|
|
@@ -134,11 +106,9 @@ json_boolean() = boolean()
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_number">json_number()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_number() = number()
|
|
|
</code></pre>
|
|
@@ -146,11 +116,9 @@ json_number() = number()
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_object">json_object()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_object() = <a href="#type-json_object_format_tuple">json_object_format_tuple()</a> | <a href="#type-json_object_format_proplist">json_object_format_proplist()</a> | <a href="#type-json_object_format_map">json_object_format_map()</a>
|
|
|
</code></pre>
|
|
@@ -158,11 +126,9 @@ json_object() = <a href="#type-json_object_format_tuple">json_object_format_tupl
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_object_format_map">json_object_format_map()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_object_format_map() = #{}
|
|
|
</code></pre>
|
|
@@ -170,11 +136,9 @@ json_object_format_map() = #{}
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_object_format_proplist">json_object_format_proplist()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_object_format_proplist() = [{}] | <a href="#type-json_object_members">json_object_members()</a>
|
|
|
</code></pre>
|
|
@@ -182,11 +146,9 @@ json_object_format_proplist() = [{}] | <a href="#type-json_object_members">json_
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_object_format_tuple">json_object_format_tuple()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_object_format_tuple() = {<a href="#type-json_object_members">json_object_members()</a>}
|
|
|
</code></pre>
|
|
@@ -194,11 +156,9 @@ json_object_format_tuple() = {<a href="#type-json_object_members">json_object_me
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_object_members">json_object_members()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_object_members() = [{<a href="#type-json_string">json_string()</a>, <a href="#type-json_value">json_value()</a>}]
|
|
|
</code></pre>
|
|
@@ -206,30 +166,24 @@ json_object_members() = [{<a href="#type-json_string">json_string()</a>, <a href
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
### <a name="type-json_string">json_string()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_string() = binary() | atom()
|
|
|
</code></pre>
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- NOTE: `decode/1` always returns `binary()` value
|
|
|
+NOTE: `decode/1` always returns `binary()` value
|
|
|
|
|
|
|
|
|
|
|
|
### <a name="type-json_value">json_value()</a> ###
|
|
|
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
json_value() = <a href="#type-json_number">json_number()</a> | <a href="#type-json_string">json_string()</a> | <a href="#type-json_array">json_array()</a> | <a href="#type-json_object">json_object()</a> | <a href="#type-json_boolean">json_boolean()</a> | null
|
|
|
</code></pre>
|
|
|
|
|
|
-
|
|
|
<a name="index"></a>
|
|
|
|
|
|
## Function Index ##
|
|
@@ -246,32 +200,26 @@ json_value() = <a href="#type-json_number">json_number()</a> | <a href="#type-js
|
|
|
|
|
|
### decode/1 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
decode(Json::binary()) -> <a href="#type-json_value">json_value()</a>
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
Equivalent to [`decode(Json, [])`](#decode-2).
|
|
|
+
|
|
|
<a name="decode-2"></a>
|
|
|
|
|
|
### decode/2 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
decode(Json::binary(), Options::[<a href="#type-decode_option">decode_option()</a>]) -> <a href="#type-json_value">json_value()</a>
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
-
|
|
|
Decodes an erlang term from json text (a utf8 encoded binary)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
Raises an error exception if input is not valid json
|
|
|
|
|
|
-
|
|
|
-
|
|
|
```
|
|
|
> jsone:decode(<<"1">>, []).
|
|
|
1
|
|
@@ -286,32 +234,26 @@ Raises an error exception if input is not valid json
|
|
|
|
|
|
### encode/1 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
encode(JsonValue::<a href="#type-json_value">json_value()</a>) -> binary()
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
Equivalent to [`encode(JsonValue, [])`](#encode-2).
|
|
|
+
|
|
|
<a name="encode-2"></a>
|
|
|
|
|
|
### encode/2 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
encode(JsonValue::<a href="#type-json_value">json_value()</a>, Options::[<a href="#type-encode_option">encode_option()</a>]) -> binary()
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
-
|
|
|
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()`
|
|
|
|
|
|
-
|
|
|
-
|
|
|
```
|
|
|
> jsone:encode([1, null, 2]).
|
|
|
<<"[1,null,2]">>
|
|
@@ -326,28 +268,24 @@ Raises an error exception if input is not an instance of type `json_value()`
|
|
|
|
|
|
### try_decode/1 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
try_decode(Json::binary()) -> {ok, <a href="#type-json_value">json_value()</a>, Remainings::binary()} | {error, {Reason::term(), [<a href="erlang.md#type-stack_item">erlang:stack_item()</a>]}}
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
Equivalent to [`try_decode(Json, [])`](#try_decode-2).
|
|
|
+
|
|
|
<a name="try_decode-2"></a>
|
|
|
|
|
|
### try_decode/2 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
try_decode(Json::binary(), Options::[<a href="#type-decode_option">decode_option()</a>]) -> {ok, <a href="#type-json_value">json_value()</a>, Remainings::binary()} | {error, {Reason::term(), [<a href="erlang.md#type-stack_item">erlang:stack_item()</a>]}}
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
-
|
|
|
Decodes an erlang term from json text (a utf8 encoded binary)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
```
|
|
|
> jsone:try_decode(<<"[1,2,3] \"next value\"">>, []).
|
|
|
{ok,[1,2,3],<<" \"next value\"">>}
|
|
@@ -361,28 +299,24 @@ Decodes an erlang term from json text (a utf8 encoded binary)
|
|
|
|
|
|
### try_encode/1 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
try_encode(JsonValue::<a href="#type-json_value">json_value()</a>) -> {ok, binary()} | {error, {Reason::term(), [<a href="erlang.md#type-stack_item">erlang:stack_item()</a>]}}
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
Equivalent to [`try_encode(JsonValue, [])`](#try_encode-2).
|
|
|
+
|
|
|
<a name="try_encode-2"></a>
|
|
|
|
|
|
### try_encode/2 ###
|
|
|
|
|
|
-
|
|
|
<pre><code>
|
|
|
try_encode(JsonValue::<a href="#type-json_value">json_value()</a>, Options::[<a href="#type-encode_option">encode_option()</a>]) -> {ok, binary()} | {error, {Reason::term(), [<a href="erlang.md#type-stack_item">erlang:stack_item()</a>]}}
|
|
|
</code></pre>
|
|
|
<br />
|
|
|
|
|
|
-
|
|
|
Encodes an erlang term into json text (a utf8 encoded binary)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
```
|
|
|
> jsone:try_encode([1, null, 2]).
|
|
|
{ok,<<"[1,null,2]">>}
|