# `ApifyClient.JSON`
[🔗](https://github.com/social-growth-studio/apify-client-ex/blob/v1.0.0/lib/apify_client/json.ex#L1)

Behaviour for the JSON library used to encode request bodies and decode
API responses.

By default the client uses `ApifyClient.JSON.Native`, which delegates to
Elixir's built-in `JSON` module, so no extra dependency is required.

## Configuration

Set the adapter for every client in your application config:

    config :apify_client, :json_library, ApifyClient.JSON.Jason

or for a single call:

    ApifyClient.Actors.start("apify/web-scraper", input, json_library: MyApp.ApifyJSON)

## Built-in adapters

  * `ApifyClient.JSON.Native` - Elixir's built-in `JSON` (default)
  * `ApifyClient.JSON.Jason` - [Jason](https://hex.pm/packages/jason), available
    when `:jason` is a dependency of your application

## Custom adapters

    defmodule MyApp.ApifyJSON do
      @behaviour ApifyClient.JSON

      @impl ApifyClient.JSON
      def encode_to_iodata!(term), do: MyJSON.encode_to_iodata!(term)

      @impl ApifyClient.JSON
      def decode(binary), do: MyJSON.decode(binary)
    end

# `decode`

```elixir
@callback decode(binary()) :: {:ok, term()} | {:error, Exception.t()}
```

Decodes a JSON binary.

Returns `{:ok, term}` on success or `{:error, exception}` on failure.

# `encode_to_iodata!`

```elixir
@callback encode_to_iodata!(term()) :: iodata()
```

Encodes `term` as JSON. Raises if the term cannot be encoded.

# `default_adapter`

```elixir
@spec default_adapter() :: module()
```

Returns the adapter configured under `config :apify_client, :json_library`,
falling back to `ApifyClient.JSON.Native`.

# `encode!`

```elixir
@spec encode!(module(), term()) :: binary()
```

Encodes `term` to a JSON binary with the given `adapter`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
