简体   繁体   中英

Using default unwrapping serializer of a Jackson ObjectMapper

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM