12

Is there an implementation that will serialize a Java object as Java code? For example, if I have the object

Map<String,Integer> m = new Map<String,Integer>();
m.put("foo",new Integer(21));

I could serialize this using

ObjectOutputStream out = new ObjectOutputStream( ... );
out.writeObject( m );
out.flush();

and the output would, for example, be

java.util.Map<String,Integer> m = new java.util.Map<String,Integer>(); 
m.put("foo",new Integer(21));

Why would you want this? Sometimes it is easier to partially create complex objects programmatically and then complete the creation manually in code. This code can then be included in the source and version controlled with everything else. Note that using external serialized objects is not exceptable.

Thanks for any help you can give.

5
  • 2
    I don't think there is any (standard) way to do this Commented Jun 8, 2011 at 15:38
  • 1
    (+1) for "Why do you want this", many questions want something unusual, but, it doesn't explain why. And there is a lot of auto code generation out there, when the main stuff is generated, and only a few details are coded by hand... Commented Jun 8, 2011 at 15:38
  • 1
    Another alternative would be static code generation (by an external entity) - so you don't have to do it by hand. The benefit of this is that you don't have to spend CPU cycles deserializing it. Commented Jun 8, 2011 at 15:40
  • Although it some time ago, have you an example for "partially create complex objects programmatically" and "complete the creation manually"? Did you find a suitable alternative? Commented Nov 1, 2013 at 18:54
  • This is possible but It will work with all Java objects. Commented Dec 7, 2013 at 22:26

5 Answers 5

4

I implemented this functionality in a new github project. You can find the project here:

https://github.com/ManuelB/java-bean-to-code-serializer

The project does not have any external dependencies except junit.

Currently it is not supporting arrays for serialization yet. Nevertheless there is already a lot of functionalities:

        Object2CodeObjectOutputStream object2CodeObjectOutputStream = new Object2CodeObjectOutputStream(
            byteArrayOutputStream);
        object2CodeObjectOutputStream.writeObject(<your-java-bean>);
        System.out.println(
                byteArrayOutputStream.toString());
Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve custom serialization of your objects. You have to implement two methods in your class with the exact signature:

private void writeObject(ObjectOutputStream oos)
{
    //write your serialization code here
}


private void readObject(ObjectInputStream ois)
{
    //write your de-serialization code here
}

However the amount of flexibility that you are seeking is very doubtful.

1 Comment

You will still get an object stream header.
0

Could you use Clojure instead and integrate it with your Java code? Clojure is homoiconic - its data is identical to its code, so you can do things like this very easily.

Maps are a basic datatype in Clojure.

Comments

0

The pre-release of Long Term Persistence (java.beans.Encoder and friends) had both an XMLEncoder and a Java encoder. You can probably still download it somewhere.

Comments

0

I had a similar problem recently and the little framework testrecorder evolved from it. It does also support objects not complying to the Java Bean Conventions. Your example be serializable like this:

Map<String,Integer> m = new HashMap<String,Integer>();
m.put("bar",new Integer(21));

CodeSerializer codeSerializer = new CodeSerializer();
System.out.println(codeSerializer.serialize(m)); // of course you can put this string to a file output stream

and the output would be:

 HashMap map1 = new LinkedHashMap<>();
 map1.put("foo", 21);

One may call serialize(Type, Object) to make map1 a more generic Type (e.g. Map or Map<String, Integer>).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.