Class StringCodec

java.lang.Object
org.acemq.amqp.core.StringCodec
All Implemented Interfaces:
Codec

public final class StringCodec extends Object implements Codec
Reads and writes UTF-8 text.

Dependency free, so the core has a working codec whatever else is on the classpath. Structured payloads belong in JSON or a schema-aware format.

An object that is not text is refused rather than written out. This used to call String.valueOf, which turned an object with no toString into OrderPlaced@4b1210ee on the wire: published, confirmed, and useless to whoever read it, with nothing anywhere reporting a problem. A publish that cannot mean what the caller intended should fail at the publish.

  • Constructor Details

    • StringCodec

      public StringCodec()
  • Method Details

    • 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