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

我正在尝试将 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) 填充;


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


喵喵时光机
浏览 66回答 1
1回答

米脂

这不是问题的答案,但我最初的目标是:我正在尝试将 Json 反序列化为我进程中的现有实例。因此,如果不存在,它只会创建一个新实例。所有对象都包含一个 id 来识别它们。对于每个尝试完成相同任务的人,以下是我的实现方式:public class ModelInstantiator extends StdValueInstantiator {&nbsp; private static final long serialVersionUID = -7760885448565898117L;&nbsp; private Class<? extends ModelObject> clazz;&nbsp; /**&nbsp; &nbsp;* @param config&nbsp; &nbsp;* @param valueType&nbsp; &nbsp;*/&nbsp; public ModelInstantiator(DeserializationConfig config, Class<? extends ModelObject> clazz) {&nbsp; &nbsp; super(config, config.constructType(clazz));&nbsp; &nbsp; this.clazz = clazz;&nbsp; }&nbsp; @Override&nbsp; public boolean canCreateFromObjectWith() {&nbsp; &nbsp; return true;&nbsp; }&nbsp; @Override&nbsp; public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) throws IOException, JsonProcessingException {&nbsp; &nbsp; UUID id = (UUID) args[0];&nbsp; &nbsp; // get local object&nbsp; &nbsp; ModelObject object = ...&nbsp; &nbsp; // if id was not found => create and add&nbsp; &nbsp; if (object == null) {&nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; object = clazz.newInstance();&nbsp; &nbsp; &nbsp; } catch (InstantiationException | IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IOException(e);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; object.setId(id);&nbsp; &nbsp; &nbsp; // add to local list&nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; return object;&nbsp; }&nbsp; @Override&nbsp; public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {&nbsp; &nbsp; CreatorProperty idProp = new CreatorProperty(new PropertyName("id"), config.constructType(UUID.class), null, null, null, null,&nbsp; &nbsp; &nbsp; &nbsp; 0, null, PropertyMetadata.STD_REQUIRED);&nbsp; &nbsp; return new SettableBeanProperty[] { idProp };&nbsp; }}我不得不拆分本地和 json id。Ohterwise 数组中的 id 为空。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java