Class TomlCodec

java.lang.Object
org.acemq.amqp.codec.toml.TomlCodec
All Implemented Interfaces:
Codec

public final class TomlCodec extends Object implements Codec
Reads and writes TOML.

The same audience as YAML — a message a person will read and edit — with the ambiguity removed. TOML has one way to write a string, no significant indentation, and no Norway problem: country = NO is a bare key error rather than a boolean that used to be a country. Where a human edits the message and a machine acts on it, that matters more than terseness.

Reach for it for configuration broadcast to a fleet, feature flags, deployment instructions and anything replayed by hand from a dead-letter queue. It is a poor choice for high volume: it is text, it is larger than JSON, and it parses more slowly.

The shape of the data has to suit it. TOML is a table format, so a message body must be an object at the top level — a bare list or a bare number is not a TOML document and this codec will say so rather than inventing a wrapper. Deep nesting reads poorly too; where the payload is a tree rather than a table, JSON is the honest answer.

Like the YAML codec, this one never volunteers for a message whose sender set no content type. Guessing wrong here would record a TOML message arriving where a JSON one did.

  • Constructor Details

    • TomlCodec

      public TomlCodec()
      Uses a mapper that ignores unknown keys and writes dates as text.
    • TomlCodec

      public TomlCodec(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Parameters:
      mapper - the mapper to use; it must be built on a TomlFactory, or this codec will announce TOML and write something else
  • Method Details

    • defaultMapper

      public static com.fasterxml.jackson.databind.ObjectMapper defaultMapper()
      Returns:
      a mapper that tolerates keys it does not know and writes readable timestamps
    • contentType

      public String contentType()
      Specified by:
      contentType in interface Codec
      Returns:
      the content type this codec produces, for example application/json
    • encode

      public byte[] encode(Object payload)
      Specified by:
      encode in interface Codec
      Parameters:
      payload - object to encode; never null
      Returns:
      encoded bytes
    • decode

      public <T> T decode(byte[] body, Class<T> target)
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      Returns:
      the decoded payload
    • canDecode

      public boolean canDecode(@Nullable String contentType)
      Specified by:
      canDecode in interface Codec
      Parameters:
      contentType - content type of a received message, or null when the sender did not set one
      Returns:
      whether this codec can decode it
    • toString

      public String toString()
      Overrides:
      toString in class Object