IncidentIo.Json behaviour (IncidentIo v0.3.1)

View Source

Behaviour for JSON encoding/decoding operations.

The JSON backend defaults to Jason but is swappable via application config, which is useful in tests when you want to use a mock:

# config/test.exs
config :incident_io, :json_module, IncidentIo.Json.Mock

Any replacement module must implement the decode!/2 and encode!/2 callbacks defined by this behaviour.

Summary

Functions

Decodes a JSON binary into an Elixir term.

Encodes an Elixir term into a JSON binary.

Callbacks

decode!(binary, keyword)

@callback decode!(
  binary(),
  keyword()
) :: term()

encode!(term, keyword)

@callback encode!(
  term(),
  keyword()
) :: binary()

Functions

decode!(body, opts \\ [])

Decodes a JSON binary into an Elixir term.

Delegates to the configured JSON module (default: Jason).

Example

IncidentIo.Json.decode!("{"key": "value"}")
#=> %{"key" => "value"}

encode!(data, opts \\ [])

Encodes an Elixir term into a JSON binary.

Delegates to the configured JSON module (default: Jason).

Example

IncidentIo.Json.encode!(%{key: "value"})
#=> "{"key":"value"}"