Use default serializer in a custom serializer #291
-
I have a custom serializer that should only do its custom serialization when a) the object mapper has been configured with a certain attribute and b) when a field has an annotation. If those conditions don't exist I want to fall back to Jackson's default serialization.
Example class:
If I write a test where
Instead of using an
Is there a way to let Jackson do its default serialization from a custom serializer? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Yes; but it needs to be done bit differently -- you need to get hold of the default serializer during serializer initialization, as it's too late in So you need to register |
Beta Was this translation helpful? Give feedback.
-
I found an example of using
If I understand how |
Beta Was this translation helpful? Give feedback.
-
Thank you. Got it working with:
|
Beta Was this translation helpful? Give feedback.
-
Ok good! Great you got it working & thank you for sharing for others! |
Beta Was this translation helpful? Give feedback.
Yes.
Right,
@JsonSerialize
won't work probably: not all callbacks are necessarily called, and specifically no default serializer likely passed even if they were. SoBeanSerializerModifier
works for globally registered serializers.You can introspect annotations on
createContextual()
method if implementing "ContextualSerializer".That gets called once per serializer, before first use.
You can then access default per-type Serializer since one being used is not globally register (no infinite looping :) ).