Skip to content

Latest commit

 

History

History
113 lines (79 loc) · 4.04 KB

mjson.md

File metadata and controls

113 lines (79 loc) · 4.04 KB

The mjson module

Types

Represents a JSON value.

-type val() :: obj() | array() | num() | str() | boolean() | null.

Represents a JSON value for encoding. Just like a val(), but also accepts datetime tuples, atoms as strings and iodata as object member names.

-type enc_val() :: val() | calendar:datetime() | enc_obj() | enc_str().

Represents a JSON object.

-type obj()     :: #{str() => val()}.

Represents a JSON array.

-type array()   :: [val()].

Represents a JSON number.

-type num()     :: number().

Represents a JSON string.

-type str()     :: binary().

When encoding, object member names can be atoms and iodata.

-type enc_obj() :: #{enc_str() | iodata() => enc_val()}.

When encoding, atoms and binaries will be encoded as JSON strings.

-type enc_str() :: binary() | atom().

Options for encode/2.

-type encode_opts() ::
        #{
           %% Set to `true` to encode without any whitespace.
           compact => boolean()

           %% Indent all rows with this whitespace.
         , indent => iodata()

           %% Print object members sorted by their name.
         , sort_objects => boolean()

           %% Timezone used in encoding of datetime values (default `utc`).
           %% If a `TzStr` is used, it should be on the form "HH:MM", to
           %% be RFC3339/ISO8601-compatible.
         , tz => utc | local | OffsetMinutes :: integer() | TzStr :: iodata()
         }.

Functions

-spec decode(binary()) -> {ok, val()}.
-spec decode(binary(), decode_opts()) -> {ok, val()}.

If Bin does not contain valid JSON, decode/1 crashes.

-spec decode(binary()) -> {ok, val()}.
-spec decode(binary(), decode_opts()) -> {ok, val()}.

If Bin does not contain valid JSON, decode/1 crashes.

-spec encode(enc_val()) -> iodata().
-spec encode(enc_val(), encode_opts()) -> iodata().
-spec encode(enc_val()) -> iodata().
-spec encode(enc_val(), encode_opts()) -> iodata().