Package de.javakaffee.kryoserializers
Class FieldAnnotationAwareSerializer<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.FieldSerializer<T>
-
- de.javakaffee.kryoserializers.FieldAnnotationAwareSerializer<T>
-
public class FieldAnnotationAwareSerializer<T> extends com.esotericsoftware.kryo.serializers.FieldSerializer<T>A kryoFieldSerializerthat allows to exclusively include or exclude fields that are attributed with user-specific annotations. This can be for example useful when serializing beans that carry references to a dependency injection framework. As an example for Spring:
The resultingSet<Class<? extends Annotation>> marks = new HashSet<>(); marks.add(Autowired.class); SerializerFactory disregardingFactory = new FieldAnnotationAwareSerializer.Factory(marks, true); Kryo kryo = new Kryo(); kryo.setDefaultSerializer(factory);Kryoinstance would ignore all fields that are annotated with Spring's@Autowiredannotation. Similarly, it is possible to created a serializer which does the opposite such that the resulting serializer would only serialize fields that are annotated with the specified annotations.- Author:
- Rafael Winterhalter, Martin Grotzke
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFieldAnnotationAwareSerializer.FactoryA factory for creating instances ofFieldAnnotationAwareSerializer.-
Nested classes/interfaces inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
com.esotericsoftware.kryo.serializers.FieldSerializer.Bind, com.esotericsoftware.kryo.serializers.FieldSerializer.CachedField, com.esotericsoftware.kryo.serializers.FieldSerializer.FieldSerializerConfig, com.esotericsoftware.kryo.serializers.FieldSerializer.NotNull, com.esotericsoftware.kryo.serializers.FieldSerializer.Optional
-
-
Constructor Summary
Constructors Constructor Description FieldAnnotationAwareSerializer(com.esotericsoftware.kryo.Kryo kryo, Class<?> type, Collection<Class<? extends Annotation>> marked, boolean disregarding)Creates a new field annotation aware serializer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddAnnotation(Class<? extends Annotation> clazz)Adds an annotation to the annotations that are considered by this serializer.booleanremoveAnnotation(Class<? extends Annotation> clazz)Removes an annotation to the annotations that are considered by this serializer.voidupdateFields()-
Methods inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer
copy, create, createCopy, getCopyFields, getField, getFields, getFieldSerializerConfig, getKryo, getType, initializeCachedFields, log, popTypeVariables, pushTypeVariables, read, removeField, removeField, write
-
-
-
-
Constructor Detail
-
FieldAnnotationAwareSerializer
public FieldAnnotationAwareSerializer(com.esotericsoftware.kryo.Kryo kryo, Class<?> type, Collection<Class<? extends Annotation>> marked, boolean disregarding)Creates a new field annotation aware serializer.- Parameters:
kryo- TheKryoinstace.type- The type of the class being serialized.marked- The annotations this serializer considers for its serialization process. Be aware tha a serializer withdisregardingset tofalsewill never be able to serialize fields that are not annotated with any of these annotations since it is not possible to add fields to aFieldSerializeronce it is created. See the documentation toaddAnnotation(Class)andremoveAnnotation(Class)for further information.disregarding- Iftrue, the serializer will ignore all annotated fields, if set tofalseit will exclusively look at annotated fields.
-
-
Method Detail
-
updateFields
public void updateFields()
- Overrides:
updateFieldsin classcom.esotericsoftware.kryo.serializers.FieldSerializer<T>
-
addAnnotation
public boolean addAnnotation(Class<? extends Annotation> clazz)
Adds an annotation to the annotations that are considered by this serializer. Important: This will not have an effect if the serializer was configured to exclusively serialize annotated fields by settingdisregardingtofalse. This is similar to the contract of this serializer's superclassFieldSerializerwhich does not allow to add fields that were formerly removed. If this was possible, instances that were serialized before this field was added could not longer be properly deserialized. In order to make this contract break explicit, you need to create a new instance of this serializer if you want to include new fields to a serializer that exclusively serializes annotated fields.- Parameters:
clazz- The annotation class to be added.- Returns:
trueif the method call had an effect.
-
removeAnnotation
public boolean removeAnnotation(Class<? extends Annotation> clazz)
Removes an annotation to the annotations that are considered by this serializer. Important: This will not have an effect if the serializer was configured to not serialize annotated fields by settingdisregardingtotrue. This is similar to the contract of this serializer's superclassFieldSerializerwhich does not allow to add fields that were formerly removed. If this was possible, instances that were serialized before this field was added could not longer be properly deserialized. In order to make this contract break explicit, you need to create a new instance of this serializer if you want to include new fields to a serializer that ignores annotated fields for serialization.- Parameters:
clazz- The annotation class to be removed.- Returns:
trueif the method call had an effect.
-
-