com.fasterxml.jackson.dataformat.avro.AvroSerializerModifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-dataformat-avro Show documentation
Show all versions of jackson-dataformat-avro Show documentation
Support for reading and writing AVRO-encoded data via Jackson
abstractions.
package com.fasterxml.jackson.dataformat.avro;
import java.util.*;
import org.apache.avro.specific.SpecificRecordBase;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
/**
* Serializer modifier used to suppress serialization of "schema"
* property for Avro-generated types.
*
* @since 2.7.2
*/
public class AvroSerializerModifier
extends BeanSerializerModifier
{
private static final long serialVersionUID = 1L;
@Override
public List changeProperties(SerializationConfig config,
BeanDescription beanDesc, List beanProperties)
{
AnnotatedClass ac = beanDesc.getClassInfo();
// Couple of ways to determine if it's generated class: main alternative
// would be to look for annotation `AvroGenerated` but check for base
// class seems simpler and as robust:
if (SpecificRecordBase.class.isAssignableFrom(ac.getRawType())) {
Iterator it = beanProperties.iterator();
while (it.hasNext()) {
BeanPropertyWriter prop = it.next();
if ("schema".equals(prop.getName()) || "specificData".equals(prop.getName())) {
it.remove();
}
}
}
return beanProperties;
}
}