bboss 序列化功能详解
bboss 序列化功能详解,bboss序列化组件是bbossgroups框架体系中的又一个非常给力的功能构件,可以非常方便地实现对象到xml的相互转换功能,本文详细介绍bboss序列化功能的特点及使用方法。
1.Bboss 序列化功能组件及依赖的jar包
1.1 主要组件和注解
组件
org.frameworkset.soa.ObjectSerializable
ObjectSerializable组件提供了序列化和反序列化的主要api:
序列化api
//将对象obj序列化成xml串并返回该串public final static String toXML(Object obj)//将对象obj序列化成xml串,并将该串写入到Writer对象out中public final static void toXML(Object obj, Writer out)
//将beanxml参数对应的xml串转换为beantype类型的对象并返回该对象,采用泛型方式public static <T> T toBean(String beanxml, Class<T> beantype)//将instream参数对应的xml字符流转换为beantype类型的对象并返回该对象,采用泛型方式public static <T> T toBean(InputStream instream, Class<T> beantype)
@ExcludeFieldprivate String excludeField
//构建和初始化要序列化的简单对象实例TransientFieldBean transientFieldBean = new TransientFieldBean("onlyField");transientFieldBean.setExcludeField("exccc");transientFieldBean.setStaticFiled("staticFiled");transientFieldBean.setTransientField("transientField");//对象序列化String xml = ObjectSerializable.toXML(transientFieldBean);//反序列化TransientFieldBean transientFieldBean_new = ObjectSerializable.toBean(xml, TransientFieldBean.class);
//构建和初始化要序列化的复杂对象实例,对象test1引用对象test2和test3,test2引//用test1,对象test3引用对象test2,这样就构造了一个具有相互引用的关系网,//bboss序列化组件可以非常方便地对这种结构的对象进行序列化和反序列化,而且能够//很好地保持这种复杂的引用关系Test1 test1 = new Test1();Test2 test2 = new Test2();Test3 test3 = new Test3();test2.setTest1(test1);test1.setTest2(test2);test1.setTest3(test3);test3.setTest2(test2);//序列化test1对象String ss = ObjectSerializable.toXML(test1);//反序列化Test1 test1_ = (Test1)ObjectSerializable.toBean(ss,Test1.class);
//构造一个带文件属性的对象joeFile fileData = new File("D:\\workspace\\bbossgroups-3.2\\bboss-soa\\test\\org\\frameworkset\\soa\\testxstream.xml");FilePerson joe = new FilePerson();joe.setFileData(fileData);//序列化对象joeString xml = ObjectSerializable.toXML(joe);//反序列化FilePerson person = ObjectSerializable.toBean( xml, FilePerson.class);
@Testpublic void testHessianSerializable() throws Exception{Test1 test1 = new Test1();Test2 test2 = new Test2();Test3 test3 = new Test3();test2.setTest1(test1);test1.setTest2(test2);test1.setTest3(test3);test3.setTest2(test2);try{String bigcontent = FileUtil.getFileContent(new File("D:\\workspace\\bbossgroups-3.5\\bboss-soa\\test\\org\\frameworkset\\soa\\testxstream.xml"), "GBK");//testxstream.xml是一个47K大小的xml文件test1.setXmlvalue(bigcontent);long s = System.currentTimeMillis();String xml = ObjectSerializable.toXML(test1);//bboss 序列化long e = System.currentTimeMillis();System.out.println("bboss:"+xml.getBytes().length + ",times:" + (e - s));s = System.currentTimeMillis();Test1 test1_ = (Test1)ObjectSerializable.toBean(xml,Test1.class);//bboss 反序列化e = System.currentTimeMillis();System.out.println("bboss de times:" + (e - s));s = System.currentTimeMillis();//hessian序列化ByteArrayOutputStream os = new ByteArrayOutputStream(); HessianOutput ho = new HessianOutput(os); ho.writeObject(test1); byte[] cs = os.toByteArray(); e = System.currentTimeMillis();System.out.println("hessian:"+cs.length+ ",times:" + (e - s));s = System.currentTimeMillis();//hessian反序列化ByteArrayInputStream is = new ByteArrayInputStream(cs); HessianInput hi = new HessianInput(is); test1_ = (Test1) hi.readObject(); e = System.currentTimeMillis();System.out.println("hessian de times:" + (e - s));//测试用例结束}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}}