java - 在自定义反序列化器中调用默认反序列化器不会影响实例

标签 java json jackson json-deserialization

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

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

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

我的反序列化函数是:

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

}

我必须拆分本地 ID 和 json ID。否则数组中的 id 为空。

关于java - 在自定义反序列化器中调用默认反序列化器不会影响实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56402032/

相关文章:

java - 如何使用Java调用Oracle数据库中IN和OUT参数的存储函数

java - 如何向已注册的用户名发送对话框

spring Rest 动态从序列化中排除对象属性

java - 如何使用 jackson 获取 json 响应?

annotations - 在 Kotlin 中注释属性时,注释的默认目标是什么?

java - 泛型编译错误

java - 如何找到不再使用的对象?

json - 解析数据时出错 org.json.JSONException : Value Not of type java. lang.String 在从 youtube api v3 解析 json 时无法转换为 JSONObject

java - spring mvc 使用 post 方法传递对象

javascript - react : Pass Function with JSON Data