Class AvroObjectConverter
java.lang.Object
net.sourceforge.jbizmo.commons.avro.util.AvroObjectConverter
Utility class that converts Avro objects into similar objects and vice versa by using reflection. The converter methods expect
that both classes have the same fields with the same name. The respective field types can be different. When converting an
object into its Avro counterpart the fields of type Date and GregorianCalendar are converted to
Instant. The converter also supports converting complex structures (e.g. an Avro object that contains a field of type
SpecificRecordBase or a list with SpecificRecordBase objects). If different enum fields must be converted the
literals of both enums must be equal!
Copyright 2021 (C) by Martin Ganserer
- Version:
- 1.0.0
- Author:
- Martin Ganserer
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends org.apache.avro.specific.SpecificRecordBase>
TConvert the given object into an Avro objectstatic <T extends org.apache.avro.specific.SpecificRecordBase,S>
List<T>toAvroList(List<S> objectList, Class<T> avroType) Convert an object list into a list of Avro objectsstatic <T> TConvert the given Avro object into an object of the desired typestatic <T,S extends org.apache.avro.specific.SpecificRecordBase>
List<T>toObjectList(List<S> avroList, Class<T> objectType) Convert an Avro object list into a list of objects
-
Method Details
-
toAvro
public static <T extends org.apache.avro.specific.SpecificRecordBase> T toAvro(Object object, Class<T> avroType) Convert the given object into an Avro object- Type Parameters:
T- the desired Avro type- Parameters:
object- the source objectavroType- the Avro class- Returns:
- the converted Avro object
- Throws:
AvroObjectConversionException- if the conversion has failed
-
toAvroList
public static <T extends org.apache.avro.specific.SpecificRecordBase,S> List<T> toAvroList(List<S> objectList, Class<T> avroType) Convert an object list into a list of Avro objects- Type Parameters:
T- the desired Avro typeS- the type of the objects that are contained in the list- Parameters:
objectList- the list that contains all objects that should be convertedavroType- the Avro class- Returns:
- a list with the converted Avro objects
- Throws:
AvroObjectConversionException- if the conversion has failed
-
toObject
Convert the given Avro object into an object of the desired type- Type Parameters:
T- the desired object type- Parameters:
avroRecord- the Avro object to be convertedtype- the object class- Returns:
- the target object
- Throws:
AvroObjectConversionException- if the conversion has failed
-
toObjectList
public static <T,S extends org.apache.avro.specific.SpecificRecordBase> List<T> toObjectList(List<S> avroList, Class<T> objectType) Convert an Avro object list into a list of objects- Type Parameters:
T- the desired object typeS- the type of the objects that are contained in the list- Parameters:
avroList- the list that contains all Avro objects that should be convertedobjectType- the object class- Returns:
- a list with the converted objects
- Throws:
AvroObjectConversionException- if the conversion has failed
-