View Source Syrup (Syrup v0.4.0)

A Syrup parser and generator in pure Elixir.

Syrup is a simple data serialization format that is easy to read and write by humans. See https://github.com/ocapn/syrup for more information.

See the Syrup.Encoder module for information on implementing the protocol for structures.

Summary

Functions

Forces encoding a value as a Syrup binary.

Forces encoding a value as a Syrup record.

Forces encoding a value as a Syrup string.

Forces encoding as a symbol without creating an atom.

Parses a Syrup value from input iodata.

Parses a Syrup value from input iodata.

Encodes a value to Syrup.

Functions

as_binary(value)

@spec as_binary(binary()) :: %Syrup.Types.Binary{binary: binary()}

Forces encoding a value as a Syrup binary.

as_record(value)

@spec as_record(tuple()) :: %Syrup.Types.Record{tuple: tuple()}

Forces encoding a value as a Syrup record.

as_string(value)

@spec as_string(binary()) :: %Syrup.Types.String{string: binary()}

Forces encoding a value as a Syrup string.

as_symbol(value)

@spec as_symbol(binary()) :: %Syrup.Types.Symbol{symbol: binary()}

Forces encoding as a symbol without creating an atom.

decode(input, opts \\ [])

Parses a Syrup value from input iodata.

Options

  • :symbols - controls how symbols are decoded. Possible values are:

Decoding symbols to atoms

The :atoms option uses the String.to_atom/1 call that can create atoms at runtime. Since the atoms are not garbage collected, this can pose a DoS attack vector when used on user-controlled data.

Examples

iex> Syrup.decode("{}")
{:ok, %{}}

iex> Syrup.decode("invalid")
{:error, %Syrup.DecodeError{message: "invalid data"}}

decode!(input, opts \\ [])

Parses a Syrup value from input iodata.

Raises an exception if the input is invalid.

encode(value, opts \\ [])

Encodes a value to Syrup.

Options

  • :binaries - controls how binaries are encoded. Possible values are:
    • :strings (default) - encodes Elixir binaries as Syrup strings if they are valid UTF-8 sequences
    • :binaries - encodes Elixir binaries as Syrup binaries regardless of their UTF-8 validity

encode!(value, opts \\ [])