View Source Renewex.Serializer (renewex v0.7.0)
This module implements the core of the serializer for generating Renew *.rnw
files.
The grammar to be used for serialization is defined by Renewex.Grammar
.
The counter part for reading/parsing Renew *.rnw
files is defined in Renewex.Parser
.
This Renewex.Serializer
manages the overall serializer state and provides function for converting
Renewex.Storable.__struct__/0
structs into strings.
Summary
Functions
The state of the serializer consists of 4 fields
Appends some token to the output of the serializer.
Retreive the accumulated output of the serializer as binary string.
Initiliaze a serializer state from a list of refs
and a grammar
definition.
Prepends some token to the output of the serializer.
Serialize a map of fields
according to the grammar rule
into the serializer
.
Serialize a list
of values according to callback (ser_fn
) into the serializer
.
Serialize a Storable
of expected_type
into the serializer
.
Functions
The state of the serializer consists of 4 fields:
grammar
: The grammar definition to be used for serialization.refs
: The list of allRenewex.Storable.__struct__/0
s that might take part the serialization process. This list is used to allowRenewex.Storable.__struct__/0
s to reference each other via index into this list. This may occure because the Renew file format is based a Java object graph that might contain cyclic references.used_refs
: A map to mark already serialized references. Any givenRenewex.Storable.__struct__/0
is serialized the first time it occures during the serialization process. Afterwards it is back referenced via integer index pointing at its first occurence. Theused_refs
map keeps track of already serialized references and their corresponding index.output
: An iolist into which the result of the serialization is accumulated.
Appends some token to the output of the serializer.
Parameters
serializer
: The serializer to which output to append the token.token
:{type, value}
tuple representing the token to append.
Returns
The serializer with the appended output.
Retreive the accumulated output of the serializer as binary string.
Parameters
serializer_or_ok_serializer
: A tuple{:ok, serializer}
or theserializer
to get the output from.
Returns
The result of the serialization as binary string.
Initiliaze a serializer state from a list of refs
and a grammar
definition.
Parameters:
refs
: A list ofRenewex.Storable.__struct__/0
that might reference each other via index into this list.grammar
: The grammar definition be used for serialization.
Returns
A fresh %Serializer{}
struct with empty output and no used refs.
Prepends some token to the output of the serializer.
Parameters
serializer
: The serializer to which output to prepend the token.token
:{type, value}
tuple representing the token to prepend.
Returns
The serializer with the prepended output.
Serialize a map of fields
according to the grammar rule
into the serializer
.
Parameters
serializer
: The serializer to write the result into.rule
: The name of the grammar rule to use (the grammar definition itself comes from the serializer).fields
: The map containing the data to be serialized.
Returns
Either a tuple {:ok, serializer}
with the serialized data appended to the serializer
s outout if successful.
Or {:error, reason}
if the serialization failed for some reason
.
Serialize a list
of values according to callback (ser_fn
) into the serializer
.
Parameters
serializer
: The serializer to write the result into.list
: The list to be serializedser_fn/2
: The function the defined the serialization of each list item. Receives two arguments:list_item, serializer
, where serializer is the accumulated state of the serializer.
Returns
Either a tuple {:ok, serializer}
with the serialized data appended to the serializer
s outout if successful.
Or {:error, reason}
if the serialization failed for some reason
.
Serialize a Storable
of expected_type
into the serializer
.
Parameters
serializer
: The serializer to write the result into.storable
: The struct to serialize.expected_type
: The name of a Java class or interface that the storable is expected to be a subtype of. SeeRenewex.Grammar
for details on how the Renew file format is based on a Java class hierarchy that is emulated here.
Returns
Either a tuple {:ok, serializer}
with the serialized data appended to the serializer
s outout if successful.
Or {:error, reason}
if the serialization failed for some reason
.