Class AvroPayloadCodec

java.lang.Object
org.mockserver.async.serde.AvroPayloadCodec

public final class AvroPayloadCodec extends Object
Converts message payloads between JSON text and Avro binary using Apache Avro's GenericDatum reader/writer — no code generation and no Confluent serde stack required.

MockServer speaks JSON everywhere (example generation, verification, recorded-message payloads). This codec bridges that to the Avro binary carried in the Confluent wire format:

JSON encoding note: Avro's JSON representation is stricter than plain JSON for union-typed (nullable/optional) fields — those require the {"fieldType": value} form. Flat records of primitive fields (the common case for AsyncAPI example payloads) map straight across.

  • Method Details

    • parseSchema

      public static org.apache.avro.Schema parseSchema(String schemaJson)
      Parse an Avro schema from its JSON text.
    • jsonToAvro

      public static byte[] jsonToAvro(org.apache.avro.Schema schema, String jsonPayload) throws IOException
      Encode a JSON payload as Avro binary for the given schema.
      Parameters:
      schema - the Avro schema
      jsonPayload - the payload as JSON text (Avro JSON encoding for unions)
      Returns:
      the Avro binary encoding
      Throws:
      IOException - if the JSON does not conform to the schema
    • avroToJson

      public static String avroToJson(org.apache.avro.Schema schema, byte[] avroBinary) throws IOException
      Decode Avro binary back into JSON text for the given schema.
      Parameters:
      schema - the Avro schema
      avroBinary - the Avro binary encoding
      Returns:
      the payload as JSON text
      Throws:
      IOException - if the bytes do not conform to the schema