我遇到一个错误,试图反序列化我的数据结构,这是一个条目列表,每个项都实现了一个接口。此外,接口的一个字段是object,每个继承都将该对象视为不同的字段。
在这个问题上花了这么多时间之后,任何答案都将不胜感激。
,这是我收到的错误:
线程"main“中的异常:不能在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)上将java.lang.String转换为java.util.Map在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)
我举了个例子如果有人也想玩的话.
谢谢!
接口
public interface IPerson {
EPersonType getPersonType();
String getName();
void setName(String name);
int getAge();
void setAge(int age);
Object getValue();
void setValue(Object value);
}这是一个非常简单的界面。正如我已经提到的,棘手的部分是表示为对象的值将包含基于接口实现的不同值。
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}]谢谢!
奥兹拉德。
发布于 2014-04-28 17:26:41
从我的角度来说,我通过把基础设施改造成更好的基础设施来解决这个问题。它的名字是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);https://stackoverflow.com/questions/23305166
复制相似问题