1

I have a custom JsonSerializer and in the serialize() method I want to use the default serializer behavior of a registered ObjectMapper:

    class Serializer extends JsonSerializer<MyEntity> {

        private ObjectMapper myObjectMapper;

        @Override
        public void serialize(MyEntity value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            
            // after processing some logic
            
            myObjectMapper.getSerializerProviderInstance().defaultSerializeValue(value, gen);

        }

    }

The code above works fine. now I want to do a similar thing but for UnwrappingSerializer, I mean I want to delegate the unwrappingSerializer to the registered ObjectMapper something like this:

    class UnwrappingSerializer extends JsonSerializer<MyEntity> {

        private ObjectMapper myObjectMapper;

        @Override
        public boolean isUnwrappingSerializer() {
            return true;
        }

        @Override
        public void serialize(MyEntity value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

            // after processing some logic

            myObjectMapper.getDefaultUnwrappingSerializer().serialize(value); // how to do such a thing???

        }

    }

I'm not sure if Jackson supports such a thing or not. I know another option would be extending UnwrappingBeanSerializer, that's also fine, the only important thing for me is that I want to delegate the unwrapping serialization to an ObjectMapper and I don't want to do it manually. is there any way to do this?

Kian
  • 211
  • 1
  • 3
  • 17

1 Answers1

0

You can always create a new instance of ObjectMapper and use it within your serializer.

Deadron
  • 5,135
  • 1
  • 16
  • 27
  • Thanks for your answer, but please read the question carefully. Accessing the ObjectMapper is not my concern, I want to use its unwrapping serializer, that's the problem. – Kian Jan 15 '21 at 17:56
  • This doesn't make a whole lot of sense. Try clarifying your issue. – Deadron Jan 15 '21 at 17:57