我在尝试反序列化我的数据结构时出错,该数据结构是一个项列表,每个项都实现了一个接口。此外,接口的一个字段是object,每次继承都将此object视为不同的字段。
在花了这么多小时讨论这个问题之后,任何答案都将不胜感激。
这是我收到的错误:
线程"main"java.lang.ClassCastException中出现异常:java.lang.String不能强制转换为java.util.Map在flexjson.factorys.BeanObjectFactory实例化(BeanObjectFactory.java:17)在flexjson。ObjectBinder.bind(ObjectBinder.java:86)在flexjson。ObjectBinder.bindIntoObject(ObjectBinder.java:139)在flexjson.factors.ClassLocatorObjectFactory.instance(ClassLocatorObjectFactory.java:38)在flexjson。ObjectBinder.bind(ObjectBinder.java:86)在flexjson。ObjectBinder.bindIntoCollection(ObjectBinder.java:101)在flexjson.factors.ListObjectFactory.instante(ListObjectFactory.java:13)在flexjson。ObjectBinder.bind(ObjectBinder.java:86)在flexjson。ObjectBinder.bind(ObjectBinder.java:65)在flexjson。JSONDeserializer.deserialize(JSONDeselializer.java:158)在testSerializeDeserializeInterface.entryPointForTestingSerialize.main(entryPointForTestingSerialize.java:34)
我举了一个例子,如果有人也愿意尝试玩它。。。
- 接口
- EPersonType
- 遗产
- 主类
- 输出
谢谢!
界面
public interface IPerson {
EPersonType getPersonType();
String getName();
void setName(String name);
int getAge();
void setAge(int age);
Object getValue();
void setValue(Object value);
}
这是一个非常简单的界面。正如我已经提到的,棘手的部分是,表示为Object的值将根据接口实现包含不同的值。
E人员类型
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}]
谢谢!
Ozrad。
从我的角度来看,我通过将基础设施更改为更好的基础设施来解决这个问题。它的名字是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);