How do I call the default deserializer from a custom deserializer in Jackson

前端 未结 11 1184
野趣味
野趣味 2020-11-22 14:26

I have a problem in my custom deserializer in Jackson. I want to access the default serializer to populate the object I am deserializing into. After the population I will do

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 14:45

    The DeserializationContext has a readValue() method you may use. This should work for both the default deserializer and any custom deserializers you have.

    Just be sure to call traverse() on the JsonNode level you want to read to retrieve the JsonParser to pass to readValue().

    public class FooDeserializer extends StdDeserializer {
    
        private static final long serialVersionUID = 1L;
    
        public FooDeserializer() {
            this(null);
        }
    
        public FooDeserializer(Class t) {
            super(t);
        }
    
        @Override
        public FooBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            JsonNode node = jp.getCodec().readTree(jp);
            FooBean foo = new FooBean();
            foo.setBar(ctxt.readValue(node.get("bar").traverse(), BarBean.class));
            return foo;
        }
    
    }
    

提交回复
热议问题