FedecksServer.BinaryCodec (fedecks_server v0.1.0)

Provides:-

  • A thin wrapper over :erlang.binary_to_term/1 and :erlang.term_to_binary/1. The latter to add some error handling and ensure only safe decoding; the former is included for symmetry.
  • Additional support for encoding / decoding binary terms to base 64

Link to this section Summary

Functions

Wrapper for :erlang.binary_to_term/2. Will not decode and unsafe binary (ie on which will create a new atom). Instead of raising an argument error, invalid or unsafe binaries will return :error, otherwise an :ok tuple is returned

Decodes the base64 encoded binary term. Returns an ok tuple, or a different error depending on whether the issue is with base64 or the binary term part.

Encodes the term as a binary, then further encodes in base 64 for transmission where pure binary would not work, eg in HTTP headers

Link to this section Functions

@spec decode(binary()) :: :error | {:ok, term()}

Wrapper for :erlang.binary_to_term/2. Will not decode and unsafe binary (ie on which will create a new atom). Instead of raising an argument error, invalid or unsafe binaries will return :error, otherwise an :ok tuple is returned

Link to this function

decode_base64(encoded)

@spec decode_base64(String.t()) ::
  {:error, :invalid_binary_term | :not_base64} | {:ok, term()}

Decodes the base64 encoded binary term. Returns an ok tuple, or a different error depending on whether the issue is with base64 or the binary term part.

@spec encode(term()) :: binary()

Wrapper for :erlang.term_to_binary/1

Link to this function

encode_base64(term)

@spec encode_base64(term()) :: String.t()

Encodes the term as a binary, then further encodes in base 64 for transmission where pure binary would not work, eg in HTTP headers