Package org.mockserver.async.serde
Class AvroPayloadCodec
java.lang.Object
org.mockserver.async.serde.AvroPayloadCodec
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:
- publish:
jsonToAvro(Schema, String)turns a JSON example into Avro binary before framing. - subscribe/verify:
avroToJson(Schema, byte[])turns consumed Avro binary back into JSON so the existing substring / JSON-path verification works unchanged.
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 Summary
Modifier and TypeMethodDescriptionstatic StringavroToJson(org.apache.avro.Schema schema, byte[] avroBinary) Decode Avro binary back into JSON text for the given schema.static byte[]jsonToAvro(org.apache.avro.Schema schema, String jsonPayload) Encode a JSON payload as Avro binary for the given schema.static org.apache.avro.SchemaparseSchema(String schemaJson) Parse an Avro schema from its JSON text.
-
Method Details
-
parseSchema
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 schemajsonPayload- 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 schemaavroBinary- the Avro binary encoding- Returns:
- the payload as JSON text
- Throws:
IOException- if the bytes do not conform to the schema
-