views:

276

answers:

7

I'm looking for the format that Java uses to serialize objects. The default serialization serializes the object in a binary format. In particular, I'm curious to know if two runs of a program can serialize the same object differently.

+2  A: 

If you have two objects with all properties set to identical values, then they will be serialized the same way.

If it weren't repeatable, then it wouldn't be useful!

Kieveli
@Kieveli: not true. Consider the case of HashSet and HashMap data structures.
Stephen C
+1  A: 

They will always serialize it the same way. If this wasn't the case, there would be no guarantee that another program could de-serialize the data correctly, defeating the purpose of serialization.

Pete
Does it leak any information that might change across different program executions, such as object memory addresses?
reprogrammer
No. It is as far as I know a pure representation of the object and its values only; the necessary data to recreate the object when de-serialized and nothing else.
Pete
There's no inherent reason why a serialization of the same object twice need produce the same sequence of bytes. For example, they could be encrypted with different keys. So long as the key precedes the object in the serialized data stream and the encryption algorithm was known, any other program could easily decrypt the stream and recreate the original object. There's no reason to believe that Java serialization does this, but it is conceivably possible.
Alohci
@Pete: your reasoning is wrong as @Alohci points out. Other counter-examples include differences in the internal node references in the serialization, and 'noise' included in the serialization that is ignored when the stream is deserialized.
Stephen C
@Pete: and another counter-example is hashed data structures where the hashcode is produced by the default `Object.hashcode()` method.
Stephen C
Valid points and good feedback. These differences should not matter, but you are correct that byte for byte there could be differences
Pete
A: 

I'm looking for the format that Java uses to serialize objects.

Not to be inane, it writes them somehow. How exactly that is can and probably should be determined by you. A Character maps to .... uh, it gets involved but rather than re-inventing the wheel let us ask exactly what do you need to have available to reconstruct an object to what state?

The default serialization serializes the object in a binary format.

So? ( again, not trying to be inane - sounds like we need to define a problem that may not have data concepted )

I'm curious to know if two runs of a program can serialize the same object differently.

If you had a Stream of information, how would you determine what states the object needed to be restored to?

Nicholas Jordan
A: 

If you don't want binary then you can use JSON (http://www.json.org/example.html) in java http://www.json.org/java/

Or XML for that matter http://www.developer.com/xml/article.php/1377961/Serializing-Java-Objects-as-XML.htm

divitiae
+7  A: 

You need the Java Object Serialization Specification at http://java.sun.com/javase/6/docs/platform/serialization/spec/protocol.html.

Alohci
+1  A: 

Typically running the same single-threaded algorithm with the same data will result in the same result.

However, things such as the order with which a HashSet serialises entries is not guaranteed. Indeed, an object may be subtly altered when serialised.

Tom Hawtin - tackline
A: 

I like @Stephen C's example of Object.hashCode(). If such nondeterministic hash codes are serialized, then when we deserialize, the hash codes will be of no use. For example, if we serialize a HashMap that works based on Object.hashCode(), its deserialized version would behave differently than the original map. That is, looking up the same object would give us different results in the two maps. What condition should an object satisfy so that the object maintains its behavior under Java's default serialization/deserialization round-trip?

reprogrammer
Alter your original question, do not re-ask in an answer as per StackOverflow guidelines.
Pete