Package io.github.microcks.util
Class AvroUtil
java.lang.Object
io.github.microcks.util.AvroUtil
Helper class using utility methods for converting Avro format from and to JSON.
- Author:
- laurent
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.avro.generic.GenericRecord
avroToAvroRecord
(byte[] avroBinary, String avroSchema) Convert an Avro binary representation into an Avro GenericRecord object using specified schema.static org.apache.avro.generic.GenericRecord
avroToAvroRecord
(byte[] avroBinary, org.apache.avro.Schema avroSchema) Convert an Avro binary representation into an Avro GenericRecord object using specified schema.static String
avroToJson
(byte[] avroBinary, String avroSchema) Convert an Avro binary representation into a JSON string using specified schema.static String
avroToJson
(byte[] avroBinary, org.apache.avro.Schema avroSchema) Convert an Avro binary representation into a JSON string using specified schema.getValidationErrors
(org.apache.avro.Schema schema, Object datum, String... fieldName) Get validation errors of a datum object regarding Avro schema.static byte[]
jsonToAvro
(String json, String avroSchema) Convert a JSON string into an Avro binary representation using specified schema.static byte[]
jsonToAvro
(String json, org.apache.avro.Schema avroSchema) Convert a JSON string into an Avro binary representation using specified schema.static org.apache.avro.generic.GenericRecord
jsonToAvroRecord
(String json, String avroSchema) Convert a JSON string into an Avro GenericRecord object using specified schema.static org.apache.avro.generic.GenericRecord
jsonToAvroRecord
(String json, org.apache.avro.Schema avroSchema) Convert a JSON string into an Avro GenericRecord object using specified schema.static boolean
Validate that a datum object (typically a GenericRecord read somewhere but the method signature is loosely coupled to make it recursive friendly) is compliant with an Avro schema.
-
Constructor Details
-
AvroUtil
public AvroUtil()
-
-
Method Details
-
jsonToAvro
public static byte[] jsonToAvro(String json, String avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert a JSON string into an Avro binary representation using specified schema.- Parameters:
json
- A JSON string to convert to AvroavroSchema
- String representation of an Avro Schema to use for conversion- Returns:
- The Avro binary representation of JSON
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between JSON string and Avro SchemaIOException
- if something goes wrong during conversion
-
jsonToAvro
public static byte[] jsonToAvro(String json, org.apache.avro.Schema avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert a JSON string into an Avro binary representation using specified schema.- Parameters:
json
- A JSON string to convert to AvroavroSchema
- The Avro Schema to use for conversion- Returns:
- The Avro binary representation of JSON
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between JSON string and Avro SchemaIOException
- if something goes wrong during conversion
-
jsonToAvroRecord
public static org.apache.avro.generic.GenericRecord jsonToAvroRecord(String json, String avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert a JSON string into an Avro GenericRecord object using specified schema.- Parameters:
json
- A JSON string to convert to AvroavroSchema
- String representation of an Avro Schema to use for conversion- Returns:
- The GenericRecord representation of JSON
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between JSON string and Avro SchemaIOException
- if something goes wrong during conversion
-
jsonToAvroRecord
public static org.apache.avro.generic.GenericRecord jsonToAvroRecord(String json, org.apache.avro.Schema avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert a JSON string into an Avro GenericRecord object using specified schema.- Parameters:
json
- A JSON string to convert to AvroavroSchema
- The Avro Schema to use for conversion- Returns:
- The GenericRecord representation of JSON
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between JSON string and Avro SchemaIOException
- if something goes wrong during conversion
-
avroToJson
public static String avroToJson(byte[] avroBinary, String avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert an Avro binary representation into a JSON string using specified schema.- Parameters:
avroBinary
- An Avro binary representation to convert in JSON stringavroSchema
- The Avro Schema to use for conversion- Returns:
- The JSON string representing Avro binary
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between Avro binary and SchemaIOException
- if something goes wrong during conversion
-
avroToJson
public static String avroToJson(byte[] avroBinary, org.apache.avro.Schema avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert an Avro binary representation into a JSON string using specified schema.- Parameters:
avroBinary
- An Avro binary representation to convert in JSON stringavroSchema
- The Avro Schema to use for conversion- Returns:
- The JSON string representing Avro binary
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between Avro binary and SchemaIOException
- if something goes wrong during conversion
-
avroToAvroRecord
public static org.apache.avro.generic.GenericRecord avroToAvroRecord(byte[] avroBinary, String avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert an Avro binary representation into an Avro GenericRecord object using specified schema.- Parameters:
avroBinary
- An Avro binary representation to convert in recordavroSchema
- The Avro Schema to use for conversion- Returns:
- The JSON string representing Avro binary
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between Avro binary and SchemaIOException
- if something goes wrong during conversion
-
avroToAvroRecord
public static org.apache.avro.generic.GenericRecord avroToAvroRecord(byte[] avroBinary, org.apache.avro.Schema avroSchema) throws org.apache.avro.AvroTypeException, IOException Convert an Avro binary representation into an Avro GenericRecord object using specified schema.- Parameters:
avroBinary
- An Avro binary representation to convert in recordavroSchema
- The Avro Schema to use for conversion- Returns:
- The JSON string representing Avro binary
- Throws:
org.apache.avro.AvroTypeException
- if there's a mismatch between Avro binary and SchemaIOException
- if something goes wrong during conversion
-
validate
Validate that a datum object (typically a GenericRecord read somewhere but the method signature is loosely coupled to make it recursive friendly) is compliant with an Avro schema.- Parameters:
schema
- The Avro Schema to validate datum againstdatum
- The Object datum to validate- Returns:
- True if the object is compliant with supplied schema, false otherwise.
-
getValidationErrors
public static List<String> getValidationErrors(org.apache.avro.Schema schema, Object datum, String... fieldName) Get validation errors of a datum object regarding Avro schema.- Parameters:
schema
- The Schema to check datum object againstdatum
- The datum object to validatefieldName
- The name of the field we're currently validating- Returns:
- A list of String representing validation errors. List may be empty if no error found.
-