将Java对象序列化为Java代码?
发布时间:2020-12-14 16:32:41 所属栏目:Java 来源:网络整理
导读:有没有将 Java对象序列化为 Java代码的实现?例如,如果我有对象 MapString,Integer m = new MapString,Integer();m.put("foo",new Integer(21)); 我可以序列化这个使用 ObjectOutputStream out = new ObjectOutputStream( ... );out.writeObject( m );out.fl
有没有将
Java对象序列化为
Java代码的实现?例如,如果我有对象
Map<String,Integer> m = new Map<String,Integer>(); m.put("foo",new Integer(21)); 我可以序列化这个使用 ObjectOutputStream out = new ObjectOutputStream( ... ); out.writeObject( m ); out.flush(); 并且输出将例如是 java.util.Map<String,Integer> m = new java.util.Map<String,Integer>(); m.put("foo",new Integer(21)); 你为什么要这样?有时以编程方式部分创建复杂对象更容易,然后在代码中手动完成创建.然后,该代码可以包含在所有其他内容的源和版本控制中.请注意,使用外部的序列化对象不是可以区分的. 感谢您能给予任何帮助. 解决方法
我在一个新的github项目中实现了这个功能.你可以在这里找到这个项目:
https://github.com/ManuelB/java-bean-to-code-serializer 该项目除junit之外没有任何外部依赖关系. 目前它不支持串行化数组.然而,已经有很多功能: Object2CodeObjectOutputStream object2CodeObjectOutputStream = new Object2CodeObjectOutputStream( byteArrayOutputStream); object2CodeObjectOutputStream.writeObject(<your-java-bean>); System.out.println( byteArrayOutputStream.toString()); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容