View Source JSONAPIPlug.Resource.Attribute protocol (jsonapi_plug v2.0.0)

Custom Resource attributes serialization and deserialization

This protocol allows to customize how individual resource attributes are serialized to in responses and deserialized from requests.

The default implementation serializes and deserializes attributes as they appear in the resource.

The implementation for MyApp.Post below, serializes the excerpt attribute by taking the first 10 characters of the post body value, and preserves all other attributes values in other cases.

defimpl JSONAPIPlug.Resource.Attribute, for: MyApp.Post do
  def serialize(%@for{}, :excerpt, _value, _conn),
    do: String.slice(post.body, 0..9)

  def serialize(%@for{}, _attribute, _value, _conn), do: value

  def deserialize(%@for{}, _attribute, _value, _conn), do: value
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Customize deserialization of resource attribute value from the request

Customize serialization of resource attribute value in the response

Types

@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

deserialize(resource, field_name, value, conn)

View Source

Customize deserialization of resource attribute value from the request

Link to this function

serialize(resource, field_name, value, conn)

View Source

Customize serialization of resource attribute value in the response