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

前端 未结 11 1044
野趣味
野趣味 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:40

    As StaxMan already suggested you can do this by writing a BeanDeserializerModifier and registering it via SimpleModule. The following example should work:

    public class UserEventDeserializer extends StdDeserializer implements ResolvableDeserializer
    {
      private static final long serialVersionUID = 7923585097068641765L;
    
      private final JsonDeserializer defaultDeserializer;
    
      public UserEventDeserializer(JsonDeserializer defaultDeserializer)
      {
        super(User.class);
        this.defaultDeserializer = defaultDeserializer;
      }
    
      @Override public User deserialize(JsonParser jp, DeserializationContext ctxt)
          throws IOException, JsonProcessingException
      {
        User deserializedUser = (User) defaultDeserializer.deserialize(jp, ctxt);
    
        // Special logic
    
        return deserializedUser;
      }
    
      // for some reason you have to implement ResolvableDeserializer when modifying BeanDeserializer
      // otherwise deserializing throws JsonMappingException??
      @Override public void resolve(DeserializationContext ctxt) throws JsonMappingException
      {
        ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
      }
    
    
      public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException
      {
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier()
        {
          @Override public JsonDeserializer modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer deserializer)
          {
            if (beanDesc.getBeanClass() == User.class)
              return new UserEventDeserializer(deserializer);
            return deserializer;
          }
        });
    
    
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);
        User user = mapper.readValue(new File("test.json"), User.class);
      }
    }
    

提交回复
热议问题