Serialize Java objects into Java code?
Is there an implementation that serializes Java objects into Java code? For example, if I have an object
Map<String,Integer> m = new Map<String,Integer>(); m.put("foo",new Integer(21));
I can serialize this use
ObjectOutputStream out = new ObjectOutputStream( ... ); out.writeObject( m ); out.flush();
And the output will be, for example
java.util.Map<String,Integer> m = new java.util.Map<String,Integer>(); m.put("foo",new Integer(21));
Why are you doing this? Sometimes it's easier to partially create complex objects programmatically, and then do it manually in code The code can then be included in the source and version control of all other content Note that using external serialized objects is not distinguishable
Thank you for any help
Solution
I implemented this function in a new GitHub project You can find this project here:
https://github.com/ManuelB/java-bean-to-code-serializer
The project has no external dependencies except JUnit
At present, it does not support serialized arrays However, there are already many functions:
Object2CodeObjectOutputStream object2CodeObjectOutputStream = new Object2CodeObjectOutputStream( byteArrayOutputStream); object2CodeObjectOutputStream.writeObject(<your-java-bean>); System.out.println( byteArrayOutputStream.toString());