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
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
@spec deserialize( JSONAPIPlug.Resource.t(), JSONAPIPlug.Resource.field_name(), term(), Plug.Conn.t() ) :: term()
Customize deserialization of resource attribute value from the request
@spec serialize( JSONAPIPlug.Resource.t(), JSONAPIPlug.Resource.field_name(), term(), Plug.Conn.t() ) :: term()
Customize serialization of resource attribute value in the response