FlexJson序列化和反序列化接口

时间:2014-04-26 00:34:56

标签: java inheritance serialization flexjson

我在尝试反序列化我的数据结构时出错,这是一个项目列表,每个项目实现一个接口。此外,接口的一个字段是object,每个继承都将此Object视为不同的字段。

在这个问题上花了这么多时间后,我们将不胜感激。

这是我收到的错误:

  

线程“main”中的异常java.lang.ClassCastException:   java.lang.String无法强制转换为java.util.Map         在flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17)         在flexjson.ObjectBinder.bind(ObjectBinder.java:86)         在flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:139)         在flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38)         在flexjson.ObjectBinder.bind(ObjectBinder.java:86)         在flexjson.ObjectBinder.bindIntoCollection(ObjectBinder.java:101)         在flexjson.factories.ListObjectFactory.instantiate(ListObjectFactory.java:13)         在flexjson.ObjectBinder.bind(ObjectBinder.java:86)         在flexjson.ObjectBinder.bind(ObjectBinder.java:65)         在flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158)         在testSerizlizeDeserializeInterface.entryPointForTestingSerialize.main(entryPointForTestingSerialize.java:34)

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

  1. 界面
  2. EPersonType
  3. 继承
  4. 主要班级
  5. 输出
  6. 谢谢!

    界面

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

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

    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}]
    

    谢谢!

    Ozrad。

1 个答案:

答案 0 :(得分:0)

从我的角度来看,我通过将基础设施更改为更好的基础设施来解决这个问题。它的名字是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);