gpt4 book ai didi

java - FlexJson 序列化和反序列化接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:02 27 4
gpt4 key购买 nike

我在尝试反序列化我的数据结构时遇到错误,该数据结构是一个项目列表,其中每个项目都实现一个接口(interface)。另外,接口(interface)的字段之一是object,每个继承都将这个Object视为不同的字段。

在这个问题上花费了这么多时间之后,我们将不胜感激任何答案。

这是我收到的错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17) at flexjson.ObjectBinder.bind(ObjectBinder.java:86) at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:139) at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38) at flexjson.ObjectBinder.bind(ObjectBinder.java:86) at flexjson.ObjectBinder.bindIntoCollection(ObjectBinder.java:101) at flexjson.factories.ListObjectFactory.instantiate(ListObjectFactory.java:13) at flexjson.ObjectBinder.bind(ObjectBinder.java:86) at flexjson.ObjectBinder.bind(ObjectBinder.java:65) at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158) at testSerizlizeDeserializeInterface.entryPointForTestingSerialize.main(entryPointForTestingSerialize.java:34)

我做了一个例子,如果有人也想尝试和玩它......

  1. 界面
  2. EPersonType
  3. 继承
  4. 主类
  5. 输出

谢谢!

界面

    public interface IPerson {
EPersonType getPersonType();
String getName();
void setName(String name);
int getAge();
void setAge(int age);
Object getValue();
void setValue(Object value);
}

它的界面非常简单。正如我已经提到的,棘手的部分是表示为对象的值将包含基于接口(interface)实现的不同值。

EPersonType

public enum EPersonType {
Father,
Mother,
}

继承

    public class Father implements IPerson {

private String name;
private int age;
private String value;

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public int getAge() {
return age;
}

@Override
public void setAge(int age) {
this.age = age;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(Object value) {
this.value = (String) value;
}
@Override
public EPersonType getPersonType() {
return EPersonType.Father;
}


}

还有另一个实例

public class Mother implements IPerson {
private String name;
private int age;
private boolean value;

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public int getAge() {
return age;
}

@Override
public void setAge(int age) {
this.age = age;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(Object value) {
this.value = (boolean) value;
}
@Override
public EPersonType getPersonType() {
return EPersonType.Mother;
}


}

主类

public class entryPointForTestingSerialize {

public static void main(String[] args) {
List<IPerson> family = new ArrayList<IPerson>();

IPerson father = new Father();
father.setAge(50);
father.setName("Oz");
father.setValue("Hello");

IPerson mother = new Mother();
mother.setAge(50);
mother.setName("Mother");
mother.setValue(false);

family.add(father);
family.add(mother);

String serialized = new JSONSerializer().deepSerialize(family);

System.out.println(serialized);

List<IPerson> deserialized = (List<IPerson>) new flexjson.JSONDeserializer<List<IPerson>>()
.use("values", new TypeLocator<String>("personType")
.add("Mother", Mother.class).add("Father", Father.class))
.deserialize(serialized);

System.out.println(deserialized);
}
}

输出

[{"age":50,"class":"testSerizlizeDeserializeInterface.Father","name":"Oz","personType":"Father","value":"Hello"},{"age":50,"class":"testSerizlizeDeserializeInterface.Mother","name":"Mother","personType":"Mother","value":false}]

谢谢!

奥兹拉德。

最佳答案

从我的角度来看,我通过将基础设施改为更好的基础设施来解决这个问题。它的名字叫XStream,它处理一切都顺利而快速。这几行代码,就全部完成了:

    XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
String xml = xstream.toXML(family);

并获取数据:

    List<IPerson> familyAfterSerialize = (List<IPerson>)xstream.fromXML(xml);

关于java - FlexJson 序列化和反序列化接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305166/

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