gpt4 book ai didi

java - jackson 反序列化抽象类

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:27 31 4
gpt4 key购买 nike

我正在尝试使用 JSON ObjectMapper 反序列化一个对象。我在尝试反序列化时看到以下错误

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.phoenix.types.OrderItem: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: java.io.StringReader@4bb33f74; line: 112, column: 7] (through reference chain: com.phoenix.types.GenerateRequest["order"]->com.phoenix.types.Order["orderItems"]->Object[][0]) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261) at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012) at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:149) at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:196) at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:20) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:101) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:101) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)

我发现这个 post 用于执行多态反序列化。这基本上提供了解决上面列出的错误的解决方案。我用于反序列化的类(在本例中为 OrderItem 等)是 jar 文件的一部分。但是,在尝试反序列化而不是向类本身添加注释时,有没有一种方法可以将 JsonDeserialize 定义为 objectmapper 的一部分,因为我无权访问它。

最佳答案

是的,你可以自己写Custom Deserializer对于抽象类。这个反序列化器必须确定 JSON 代表哪个具体类,并实例化它的一个实例。

可能有更惯用的方法来做到这一点,但这里有一个简单的例子:

public class Test {
public static void main(String... args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addDeserializer(Animal.class, new AnimalDeserializer());
mapper.registerModule(module);

final String json = "{\"aGoodBoy\": true}";
final Animal animal = mapper.readValue(json, Animal.class);
System.out.println(animal.talk());
}

public static abstract class Animal {
public abstract String talk();
}

public static class Fish extends Animal {
@Override
public String talk() {
return "blub blub I'm a dumb fish";
}
}

public static class Dog extends Animal {
public boolean aGoodBoy;

@Override
public String talk() {
return "I am a " + (aGoodBoy ? "good" : "bad") + " dog";
}
}

public static class AnimalDeserializer extends StdDeserializer<Animal> {
protected AnimalDeserializer() {
this(null);
}

protected AnimalDeserializer(final Class<?> vc) {
super(vc);
}

@Override
public Animal deserialize(final JsonParser parser, final DeserializationContext context)
throws IOException, JsonProcessingException {
final JsonNode node = parser.getCodec().readTree(parser);
final ObjectMapper mapper = (ObjectMapper)parser.getCodec();
if (node.has("aGoodBoy")) {
return mapper.treeToValue(node, Dog.class);
} else {
return mapper.treeToValue(node, Fish.class);
}
}
}
}

关于java - jackson 反序列化抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374706/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com