Raxol.Protocols.Serializable protocol (Raxol v2.0.1)
View SourceProtocol for serializing and deserializing data structures.
This protocol provides a unified interface for converting data structures to and from various formats like JSON, TOML, and binary formats.
Examples
defimpl Raxol.Protocols.Serializable, for: MyStruct do
def serialize(data, :json) do
data
|> Map.from_struct()
|> Jason.encode!()
end
def deserialize(data, :json, MyStruct) do
data
|> Jason.decode!()
|> then(&struct(MyStruct, &1))
end
def serializable?(data, format) do
format in [:json, :toml]
end
end
Summary
Functions
Checks if the data can be serialized to the given format.
Serializes data to the specified format.
Types
Functions
Checks if the data can be serialized to the given format.
Returns
true if the data can be serialized to the format, false otherwise.
Serializes data to the specified format.
Formats
:json- JSON string using Jason:toml- TOML string:binary- Binary format using :erlang.term_to_binary:erlang_term- Erlang external term format
Returns
A string or binary containing the serialized data, or {:error, reason}.