Boltx.ResponseEncoder.Json protocol (Boltx v0.0.2)
Protocol controlling how a value is made jsonable.
Its only purpose is to convert Bolt Boltx specific structures into elixir buit-in types which can be encoed in json by Jason.
Deriving
If the provided default implementation don't fit your need, you can override with your own implementation.
Example
Let's assume that you don't want Node's id available as they are Neo4j's ones and are not
reliable because of id reuse and you want to have you own uuid
in place.
Instead of:
{
id: 0,
labels: ["TestNode"],
properties: {
uuid: "837806a7-6c37-4630-9f6c-9aa7ad0129ed"
value: "my node"
}
}
you want:
{
uuid: "837806a7-6c37-4630-9f6c-9aa7ad0129ed",
labels: ["TestNode"],
properties: {
value: "my node"
}
}
You can achieve that with the following implementation:
defimpl Boltx.ResponseEncoder.Json, for: Boltx.Types.Node do
def encode(node) do
new_props = Map.drop(node.properties, :uuid)
node
|> Map.from_struct()
|> Map.put(:uuid, node.properties.uuid)
|> Map.put(:properties, new_props)
end
end
It is also possible to provide implementation that returns structs or updated Boltx.Types,
the use of a final Boltx.ResponseEncoder.Json.encode()
will ensure that these values will
be converted to jsonable ones.
Summary
Functions
Convert a value in a jsonable format
Types
@type t() :: term()
All the types that implement this protocol.
Functions
encode(value)
Convert a value in a jsonable format