Configure a custom JSON codec for Elastix
masterBy default, Elastix uses a standard JSON library, but you can swap it for a different one (like Jiffy or Jason) by configuring the json_codec and json_options keys in your application configuration.
To use a custom library, you must provide a module that implements the Elastix.JSON.Codec behavior. This behavior requires two functions:
encode!(data): Encodes Elixir data to a JSON string.decode(json, opts \ []): Decodes a JSON string into Elixir data, returning{:ok, decoded_data}or an error tuple.
If the library you are using already implements this interface (for example, Jason), you can pass the module name directly.
# Example: Using Jiffy with specific options
config :elastix,
json_codec: JiffyCodec,
json_options: [:return_maps]
# Example: Implementing the Elastix.JSON.Codec behavior
defmodule JiffyCodec do
@behaviour Elastix.JSON.Codec
def encode!(data), do: :jiffy.encode(data)
def decode(json, opts \ []), do: {:ok, :jiffy.decode(json, opts)}
end
# Example: Using Jason (which already implements the interface)
config :elastix,
json_codec: Jason