Jackson Deserializer for one custom field?

Deadly 提交于 2019-12-23 07:48:21

问题


I believe we need a custom deserializer to do something specific with one field on our class. It appears once I do this, I am now responsible for deserializing all the other fields. Is there a way to have Jackson deserialize all the fields except the one I am concerned with here?

public class ThingDeseralizer extends StdDeserializer<Thing> {
    @Override
    public Thing deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ObjectCodec oc = p.getCodec();
        JsonNode node = oc.readTree(p);

        String special = node.get("special").asText();

        Thing thing = new Thing()
        thing.doSomethignWithSpecial(special)
        return thing;
    }
}

Thanx


回答1:


On your field in POJO add @JsonDeserialize(using = ThingDeseralizer.class) annotation.

This will tell Jackson how to deserialze that particular field, rest all will go as default.



来源:https://stackoverflow.com/questions/41001302/jackson-deserializer-for-one-custom-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!