ApifyClient.JSON behaviour (apify_client v1.0.0)

Copy Markdown View Source

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

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

Summary

Callbacks

Decodes a JSON binary.

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

Functions

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

Encodes term to a JSON binary with the given adapter.

Callbacks

decode(binary)

@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!(term)

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

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

Functions

default_adapter()

@spec default_adapter() :: module()

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

encode!(adapter, term)

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

Encodes term to a JSON binary with the given adapter.