View Source Renewex.Serializer (renewex v0.10.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

Link to this function

%Renewex.Serializer{}

View Source (struct)

The state of the serializer consists of 4 fields:

  • grammar: The grammar definition to be used for serialization.
  • refs: The list of all Renewex.Storable.__struct__/0s that might take part the serialization process. This list is used to allow Renewex.Storable.__struct__/0s 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 given Renewex.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. The used_refs map keeps track of already serialized references and their corresponding index.
  • output: An iolist into which the result of the serialization is accumulated.
Link to this function

append_token(serializer, arg)

View Source

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.

Link to this function

get_output_string(serializer_or_ok_serializer)

View Source

Retreive the accumulated output of the serializer as binary string.

Parameters

  • serializer_or_ok_serializer: A tuple {:ok, serializer} or the serializer 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 of Renewex.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.

Link to this function

prepend_token(serializer, arg)

View Source

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.

Link to this function

serialize_grammar_rule(serializer, rule, fields)

View Source

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 serializers outout if successful. Or {:error, reason} if the serialization failed for some reason.

Link to this function

serialize_list(serializer, list, ser_fn)

View Source

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 serialized
  • ser_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 serializers outout if successful. Or {:error, reason} if the serialization failed for some reason.

Link to this function

serialize_storable(serializer, storable, expected_type \\ nil)

View Source

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. See Renewex.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 serializers outout if successful. Or {:error, reason} if the serialization failed for some reason.