Class YamlCodec

java.lang.Object
org.acemq.amqp.codec.yaml.YamlCodec
All Implemented Interfaces:
Codec

public final class YamlCodec extends Object implements Codec
Reads and writes YAML.

Chosen when a message is meant to be read by a person as much as by a program: a configuration change broadcast to a fleet, a deployment instruction, a command replayed by hand from a dead-letter queue. It is worth saying plainly that YAML costs more to parse than JSON and is a poor choice for high volume; it earns its place where somebody will actually look at the message.

Written in block style rather than flow style, which is the whole reason to pick YAML. Flow style would produce something very close to JSON and would leave nothing to justify the cost.

This codec never volunteers for a message whose sender set no content type. YAML is a superset of JSON, so its parser accepts JSON bytes quite happily and would answer for messages meant for the JSON codec. It would even give the right value — while recording that a YAML message had arrived, which is the sort of wrong that is discovered much later. So it claims only content types that say YAML.

  • Constructor Details

    • YamlCodec

      public YamlCodec()
      Uses a mapper that writes block-style YAML and ignores unknown keys.
    • YamlCodec

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

    • defaultMapper

      public static com.fasterxml.jackson.databind.ObjectMapper defaultMapper()
      Returns:
      a mapper that writes readable YAML and tolerates keys it does not know
    • 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