【问题标题】:Calling the default deserializer in a custom deserializer don't effect instance在自定义反序列化器中调用默认反序列化器不会影响实例
【发布时间】:2019-05-31 21:45:06
【问题描述】:

我正在尝试将 Json 反序列化为我的流程中的现有实例。所以它只在不存在的情况下创建一个新实例。 Alls 对象包含一个 id 来识别它们。

我使用了这个答案:https://stackoverflow.com/a/18405958/11584969 并尝试为此创建一个自定义解串器。

到目前为止,我已经设法创建了一个 custon Deserializer 来检查现有实例,但我无法填充新实例或更改现有实例。

我的反序列化函数是:

public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  JsonNode node = jp.getCodec().readTree(jp);

  if (node instanceof NullNode) {
    return null;
  }

  // get id from node
  String strId = node.get("id").asText();
  UUID id = UUID.fromString(strId);

  // search for existing instance or create it
  T mObject = ...

  // fill/change instance
  return (T) defaultDeserializer.deserialize(jp, ctxt, mObject);
}

对象映射器的创建:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enableDefaultTyping();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new JavaTimeModule());

SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
  @Override
  public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
    if (beanDesc.getBeanClass() == Table.class)
      return new ModelObjectDeserializer<>(Table.class, (JsonDeserializer<Table>) deserializer);

    return deserializer;
  }
});
objectMapper.registerModule(module);

上面的代码运行没有任何错误或异常,但是来自 mObject 的实例没有被 defaultDeserializer.deserialize(jp, ctxt, mObject); 填充;

如果我不使用自定义反序列化器,创建的实例将按预期填充。

【问题讨论】:

    标签: java json jackson json-deserialization


    【解决方案1】:

    这不是问题的完全答案,但我最初的目标是:

    正在尝试将 Json 反序列化为我的流程中的现有实例。因此,如果不存在,它只会创建一个新实例。 Alls 对象包含一个 id 来识别它们。

    对于所有尝试实现相同目标的人,以下是我的实现方式:

    public class ModelInstantiator extends StdValueInstantiator {
    
      private static final long serialVersionUID = -7760885448565898117L;
    
      private Class<? extends ModelObject> clazz;
    
      /**
       * @param config
       * @param valueType
       */
      public ModelInstantiator(DeserializationConfig config, Class<? extends ModelObject> clazz) {
        super(config, config.constructType(clazz));
    
        this.clazz = clazz;
      }
    
      @Override
      public boolean canCreateFromObjectWith() {
        return true;
      }
    
      @Override
      public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) throws IOException, JsonProcessingException {
        UUID id = (UUID) args[0];
    
        // get local object
        ModelObject object = ...
    
        // if id was not found => create and add
        if (object == null) {
          try {
            object = clazz.newInstance();
          } catch (InstantiationException | IllegalAccessException e) {
            throw new IOException(e);
          }
    
          object.setId(id);
          // add to local list
          ...
        }
    
        return object;
      }
    
      @Override
      public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {
        CreatorProperty idProp = new CreatorProperty(new PropertyName("id"), config.constructType(UUID.class), null, null, null, null,
            0, null, PropertyMetadata.STD_REQUIRED);
    
        return new SettableBeanProperty[] { idProp };
      }
    
    }
    

    我不得不拆分本地和 json id。否则数组中的 id 为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 2017-03-06
      • 2017-09-10
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2021-02-06
      相关资源
      最近更新 更多