简体   繁体   English

在Custom serializer jackson中配置objectmapper?

[英]Configure objectmapper inside Custom serializer jackson?

I have a CustomSerializer for a particular field written. 我有一个CustomSerializer用于编写特定字段。 I call the custom serializer on an ObjectMapper with certain configurations like WRAP_ROOT_VALUE , PropertyNameStrategy , Inclusion.NON_NULL . 我在ObjectMapper使用WRAP_ROOT_VALUEPropertyNameStrategyInclusion.NON_NULL等特定配置调用自定义序列化程序。

Now inside my custom serializer I want all these properties while serializing my custom class except one ( WRAP_ROOT_VALUE ). 现在在我的自定义序列化程序中,我希望在序列化我的自定义类时除了一个( 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. 所以我的obj在这里被序列化了root值包装,这是我不想要的。

I cannot edit my POJO for some reason. 我出于某种原因无法编辑我的POJO。

How can I disable only a single (or some) property of Objectmapper inside a CustomSerializer ??? 如何在CustomSerializer中Objectmapper的单个(或某些)属性?

Getting the ObjectMapper 获取ObjectMapper

From within a custom JsonSerializer , you can get the ObjectMapper using: 在自定义JsonSerializer ,您可以使用以下命令获取ObjectMapper

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

Setting the ObjectMapper 设置ObjectMapper

You also can define a new ObjectMapper within your custom JsonSerializer using: 您还可以使用以下命令在自定义JsonSerializer定义新的ObjectMapper

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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