# Module jsone # * [Description](#description) * [Data Types](#types) * [Function Index](#index) * [Function Details](#functions) JSON decoding/encoding module. ## Data Types ## ### datetime_encode_format() ###
datetime_encode_format() = datetime_format() | {Format::datetime_format(), TimeZone::timezone()}
Datetime encoding format.
The default value of `TimeZone` is `utc`.
```
%
% Universal Time
%
> jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, iso8601}]).
<<"\"2000-03-10T10:03:58Z\"">>
%
% Local Time (JST)
%
> jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, {iso8601, local}}]).
<<"\"2000-03-10T10:03:58+09:00\"">>
%
% Explicit TimeZone Offset
%
> jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, {iso8601, -2*60*60}}]).
<<"\"2000-03-10T10:03:58-02:00\"">>
```
### datetime_format() ###
datetime_format() = iso8601
### decode_option() ###
decode_option() = {object_format, tuple | proplist | map} | {allow_ctrl_chars, boolean()} | {keys, binary | atom | existing_atom | attempt_atom}
`object_format`:
encode_option() = native_utf8 | {float_format, [float_format_option()]} | {datetime_format, datetime_encode_format()} | {object_key_type, string | scalar | value} | {space, non_neg_integer()} | {indent, non_neg_integer()}
`native_utf8`:
float_format_option() = {scientific, Decimals::0..249} | {decimals, Decimals::0..253} | compact
`scientific`:
json_array() = [json_value()]
### json_boolean() ###
json_boolean() = boolean()
### json_number() ###
json_number() = number()
### json_object() ###
json_object() = json_object_format_tuple() | json_object_format_proplist() | json_object_format_map()
### json_object_format_map() ###
json_object_format_map() = #{}
### json_object_format_proplist() ###
json_object_format_proplist() = [{}] | json_object_members()
### json_object_format_tuple() ###
json_object_format_tuple() = {json_object_members()}
### json_object_members() ###
json_object_members() = [{json_string(), json_value()}]
### json_scalar() ###
json_scalar() = json_boolean() | json_number() | json_string()
### json_string() ###
json_string() = binary() | atom() | calendar:datetime()
NOTE: `decode/1` always returns `binary()` value
### json_term() ###
json_term() = {{json, iolist()}} | {{json_utf8, unicode:chardata()}}
`json_term()` allows inline already encoded JSON value. `json` variant
expects byte encoded utf8 data values as list members. `json_utf8` expect
Unicode code points as list members. Binaries are copied "as is" in both
variants except `json_utf8` will check if binary contain valid `UTF-8`
encoded data. In short, `json` uses `erlang:iolist_to_binary/1` and
`json_utf8` uses `unicode:chardata_to_binary/1` for encoding.
A simple example is worth a thousand words.
```
1> S = "hélo".
"hélo"
2> shell:strings(false).
true
3> S.
[104,233,108,111]
4> B = jsone:encode({{json, S}}). % invalid UTF-8
<<104,233,108,111>>
5> B2 = jsone:encode({{json_utf8, S}}). % valid UTF-8
<<104,195,169,108,111>>
6> jsone:encode({{json, B}}).
<<104,233,108,111>>
7> jsone:encode({{json_utf8, B}}).
** exception error: {invalid_json_utf8,<<104>>,<<233,108,111>>}
in function jsone_encode:value/4
called as jsone_encode:value({json_utf8,<<104,233,108,111>>},
[],<<>>,
{encode_opt_v2,false,
[{scientific,20}],
{iso8601,0},
string,0,0})
in call from jsone:encode/2 (/home/hynek/work/altworx/jsone/_build/default/lib/jsone/src/jsone.erl, line 302)
8> jsone:encode({{json_utf8, B2}}).
<<104,195,169,108,111>>
9> shell:strings(true).
false
10> jsone:encode({{json_utf8, B2}}).
<<"hélo"/utf8>>
11> jsone:encode({{json, binary_to_list(B2)}}). % UTF-8 encoded list leads to valid UTF-8
<<"hélo"/utf8>>
```
### json_value() ###
json_value() = json_number() | json_string() | json_array() | json_object() | json_boolean() | null | json_term()
### timezone() ###
timezone() = utc | local | utc_offset_seconds()
### utc_offset_seconds() ###
utc_offset_seconds() = -86399..86399
## Function Index ##
decode/1 | Equivalent to decode(Json, []). |
decode/2 | Decodes an erlang term from json text (a utf8 encoded binary). |
encode/1 | Equivalent to encode(JsonValue, []). |
encode/2 | Encodes an erlang term into json text (a utf8 encoded binary). |
try_decode/1 | Equivalent to try_decode(Json, []). |
try_decode/2 | Decodes an erlang term from json text (a utf8 encoded binary). |
try_encode/1 | Equivalent to try_encode(JsonValue, []). |
try_encode/2 | Encodes an erlang term into json text (a utf8 encoded binary). |
decode(Json::binary()) -> json_value()
decode(Json::binary(), Options::[decode_option()]) -> json_value()
encode(JsonValue::json_value()) -> binary()
encode(JsonValue::json_value(), Options::[encode_option()]) -> binary()
try_decode(Json::binary()) -> {ok, json_value(), Remainings::binary()} | {error, {Reason::term(), [erlang:stack_item()]}}
try_decode(Json::binary(), Options::[decode_option()]) -> {ok, json_value(), Remainings::binary()} | {error, {Reason::term(), [erlang:stack_item()]}}
try_encode(JsonValue::json_value()) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}
try_encode(JsonValue::json_value(), Options::[encode_option()]) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}