简体   繁体   中英

Configure objectmapper inside Custom serializer jackson?

I have a CustomSerializer for a particular field written. I call the custom serializer on an ObjectMapper with certain configurations like WRAP_ROOT_VALUE , PropertyNameStrategy , Inclusion.NON_NULL .

Now inside my custom serializer I want all these properties while serializing my custom class except one ( WRAP_ROOT_VALUE ).

public class CustomSerializer extends JsonSerializer<Object>{

    @Override
    public void serialize(Object obj, JsonGenerator jgen,
            SerializerProvider arg2) throws IOException,
            JsonProcessingException {
//.......
        jgen.writeObject(obj);
//...       
    }

So my obj here gets serialized with root value wrapped which I don't want.

I cannot edit my POJO for some reason.

How can I disable only a single (or some) property of Objectmapper inside a CustomSerializer ???

Getting the ObjectMapper

From within a custom JsonSerializer , you can get the ObjectMapper using:

ObjectMapper mapper = ((ObjectMapper) jgen.getCodec());

Setting the ObjectMapper

You also can define a new ObjectMapper within your custom JsonSerializer using:

ObjectMapper mapper = new ObjectMapper();
jgen.setCodec(mapper);

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