如何使用Java和Jackson库对Json String进行多态反序列化?

如何使用Java和Jackson库对Json String进行多态反序列化?,第1张

如何使用Java和Jackson库对Json String进行多态反序列化?

目前尚不清楚原始海报有什么问题。我猜这是两件事之一:

  1. 未绑定JSON元素的反序列化问题,因为JSON包含Java中没有要绑定的元素;要么

  2. 要实现多态反序列化。

这是第一个问题的解决方案。

import static org.prehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES;import org.prehaus.jackson.map.ObjectMapper;public class Foo{  public static void main(String[] args) throws Exception  {    baseClass base = new baseClass();    A a = new A();    B b = new B();    C c = new C();    ObjectMapper mapper = new ObjectMapper();    String baseJson = mapper.writevalueAsString(base);    System.out.println(baseJson); // {"baseName":"base name"}    String aJson = mapper.writevalueAsString(a);    System.out.println(aJson); // {"baseName":"base name","aName":"a name"}    String bJson = mapper.writevalueAsString(b);    System.out.println(bJson); // {"baseName":"base name","bName":"b name"}    String cJson = mapper.writevalueAsString(c);    System.out.println(cJson); // {"baseName":"base name","cName":"c name"}    baseClass baseCopy = mapper.readValue(baseJson, baseClass.class);    System.out.println(baseCopy); // baseName: base name    // baseClass aCopy = mapper.readValue(aJson, baseClass.class);    // throws UnrecognizedPropertyException:     // Unrecognized field "aName", not marked as ignorable    // because the JSON contains elements for which no Java field    // to bind to was provided.    // Need to let Jackson know that not all JSON elements must be bound.    // To resolve this, the class can be annotated with     // @JsonIgnoreProperties(ignoreUnknown=true) or the ObjectMapper can be    // directly configured to not FAIL_ON_UNKNOWN_PROPERTIES    mapper = new ObjectMapper();    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);    baseClass aCopy = mapper.readValue(aJson, baseClass.class);    System.out.println(aCopy); // baseName: base name    baseClass bCopy = mapper.readValue(bJson, baseClass.class);    System.out.println(bCopy); // baseName: base name    baseClass cCopy = mapper.readValue(cJson, baseClass.class);    System.out.println(cCopy); // baseName: base name  }}class baseClass{  public String baseName = "base name";  @Override public String toString() {return "baseName: " + baseName;}}class A extends baseClass{  public String aName = "a name";  @Override public String toString() {return super.toString() + ", aName: " + aName;}}class B extends baseClass{  public String bName = "b name";  @Override public String toString() {return super.toString() + ", bName: " + bName;}}class C extends baseClass{  public String cName = "c name";  @Override public String toString() {return super.toString() + ", cName: " + cName;}}

这是第二个问题的解决方案。

import org.prehaus.jackson.annotate.JsonSubTypes;import org.prehaus.jackson.annotate.JsonSubTypes.Type;import org.prehaus.jackson.annotate.JsonTypeInfo;import org.prehaus.jackson.map.ObjectMapper;public class Foo{  public static void main(String[] args) throws Exception  {    baseClass base = new baseClass();    A a = new A();    B b = new B();    C c = new C();    ObjectMapper mapper = new ObjectMapper();    String baseJson = mapper.writevalueAsString(base);    System.out.println(baseJson); // {"type":"baseClass","baseName":"base name"}    String aJson = mapper.writevalueAsString(a);    System.out.println(aJson); // {"type":"a","baseName":"base name","aName":"a name"}    String bJson = mapper.writevalueAsString(b);    System.out.println(bJson); // {"type":"b","baseName":"base name","bName":"b name"}    String cJson = mapper.writevalueAsString(c);    System.out.println(cJson); // {"type":"c","baseName":"base name","cName":"c name"}    baseClass baseCopy = mapper.readValue(baseJson, baseClass.class);    System.out.println(baseCopy); // baseName: base name    baseClass aCopy = mapper.readValue(aJson, baseClass.class);    System.out.println(aCopy); // baseName: base name, aName: a name    baseClass bCopy = mapper.readValue(bJson, baseClass.class);    System.out.println(bCopy); // baseName: base name, bName: b name    baseClass cCopy = mapper.readValue(cJson, baseClass.class);    System.out.println(cCopy); // baseName: base name, cName: c name  }}@JsonTypeInfo(      use = JsonTypeInfo.Id.NAME,      include = JsonTypeInfo.As.PROPERTY,      property = "type")  @JsonSubTypes({      @Type(value = A.class, name = "a"),      @Type(value = B.class, name = "b"),      @Type(value = C.class, name = "c") }) class baseClass{  public String baseName = "base name";  @Override public String toString() {return "baseName: " + baseName;}}class A extends baseClass{  public String aName = "a name";  @Override public String toString() {return super.toString() + ", aName: " + aName;}}class B extends baseClass{  public String bName = "b name";  @Override public String toString() {return super.toString() + ", bName: " + bName;}}class C extends baseClass{  public String cName = "c name";  @Override public String toString() {return super.toString() + ", cName: " + cName;}}

如果相反,目标是在没有专门用于指示子类类型的JSON元素的情况下反序列化为子类类型,那么这也是可能的,只要可以使用JSON中的内容来确定应该使用什么子类类型。
。我在http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-
into.html上
发布了这种方法的示例。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5622842.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存