在自定义解串器中调用默认解串器不会影响实例

Calling the default deserializer in a custom deserializer don't effect instance

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

我使用了这个答案: 并尝试为此创建一个自定义反序列化器。

到目前为止,我已经设法创建了一个自定义反序列化器来检查现有实例,但我无法填充新实例或更改现有实例。

我的反序列化函数是:

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);

填充

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

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

'm trying to deserialize a Json into an existing instance in my process. So it only creates a new instance if none exists. Alls objects contains an id to Identifiy them.

对于所有尝试完成相同任务的人,以下是我的实现方式:

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。 Ohterwise 数组中的 id 为空。