Search documentation of Localize

# Localize.Utils.Json (Localize v0.39.0)

Copy Markdown View Source

JSON decoding utilities wrapping the OTP `:json` module.

Provides a `Jason`-compatible `decode!/1,2` interface suitable for decoding JSON data in Localize. Uses the built-in `:json` module available in OTP 27+.

## Summary

### Functions

decode!(string)

Decodes a JSON string into an Elixir term.

decode!(string, options)

## Functions

# decode!(string)

    @spec decode!(String.t() | charlist()) :: term()

Decodes a JSON string into an Elixir term.

JSON `null` values are decoded as `nil`. By default, object keys are strings. Pass `keys: :atoms` to decode keys as atoms.

### Arguments

- `string` — a JSON-encoded string or charlist.

### Options

- `:keys` — when set to `:atoms`, decodes object keys as atoms.

### Returns

- The decoded Elixir term.

### Examples

    iex> Localize.Utils.Json.decode!(~s({"foo": 1}))
    %{"foo" => 1}

    iex> Localize.Utils.Json.decode!(~s({"foo": 1}), keys: :atoms)
    %{foo: 1}

    iex> Localize.Utils.Json.decode!(~s({"bar": null}))
    %{"bar" => nil}

# decode!(string, options)

    @spec decode!(
      String.t() | charlist(),
      keyword()
    ) :: term()