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

时间:2019-05-31 21:45:06

标签: java json jackson json-deserialization

我正在尝试在过程中将Json反序列化为现有实例。因此,它仅在不存在新实例的情况下创建它。 Alls对象包含一个ID以对其进行识别。

我使用了以下答案:https://stackoverflow.com/a/18405958/11584969,并试图为此创建一个custon Deserializer。

到目前为止,我已经设法创建了一个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);

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

如果我不使用自定义解串器,则创建的实例将按预期填充。

1 个答案:

答案 0 :(得分:0)

这不是一个很好的答案,但我的最初目标是:

  

试图在我的流程中将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 };
  }

}

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

相关问题