I want to create a custom serializer which does a tiny bit of work and then leaves the rest for default serialization.
For example:
@JsonSerialize(using = MyClassSerializer.class) public class MyClass { ... } public class MyClassSerializer extends JsonSerializer<MyClass> { @Override public void serialize(MyClass myClass, JsonGenerator generator, SerializerProvider provider) throws JsonGenerationException, IOException { if (myClass.getSomeProperty() == someCalculationResult) { provider.setAttribute("special", true); } generator.writeObject(myClass); } }
With the idea of creating other custom serializers for aggregated objects which behave differently based on the 'special' attribute value. However, the above code does not work, as it unsurprisingly goes into an infinite recursion.
Is there a way to tell jackson to use default serialization once I have set the attribute? I don't really want enumerate all the properties like many custom serializers as the class is fairly complex and I don't want to have to do dual maintenance with the serializer every time I change the class.
Note that Jackson does not use java. io. Serializable for anything: there is no real value for adding that. It gets ignored.
@JsonSerialize is used to specify custom serializer to marshall the json object.
A BeanSerializerModifier
will provide you access to the default serialization.
public class MyClassSerializer extends JsonSerializer<MyClass> { private final JsonSerializer<Object> defaultSerializer; public MyClassSerializer(JsonSerializer<Object> defaultSerializer) { this.defaultSerializer = checkNotNull(defaultSerializer); } @Override public void serialize(MyClass myclass, JsonGenerator gen, SerializerProvider provider) throws IOException { if (myclass.getSomeProperty() == true) { provider.setAttribute("special", true); } defaultSerializer.serialize(myclass, gen, provider); } }
BeanSerializerModifier
for MyClass
public class MyClassSerializerModifier extends BeanSerializerModifier { @Override public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) { if (beanDesc.getBeanClass() == MySpecificClass.class) { return new MyClassSerializer((JsonSerializer<Object>) serializer); } return serializer; } }
ObjectMapper om = new ObjectMapper() .registerModule(new SimpleModule() .setSerializerModifier(new MyClassSerializerModifier()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With