# `JSONAPIPlug.Resource.Attribute`
[🔗](https://github.com/lucacorti/jsonapi_plug/blob/main/lib/jsonapi_plug/resource/attribute.ex#L1)

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.

```elixir
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
```

# `t`

```elixir
@type t() :: term()
```

All the types that implement this protocol.

# `deserialize`

```elixir
@spec deserialize(
  JSONAPIPlug.Resource.t(),
  JSONAPIPlug.Resource.field_name(),
  term(),
  Plug.Conn.t()
) ::
  term()
```

Customize deserialization of resource attribute value from the request

# `serialize`

```elixir
@spec serialize(
  JSONAPIPlug.Resource.t(),
  JSONAPIPlug.Resource.field_name(),
  term(),
  Plug.Conn.t()
) ::
  term()
```

Customize serialization of resource attribute value in the response

---

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