Serialization of Java, Kryo and the graph of objects

Suppose I have an array of arr objects of type A in memory, each of which has a link field pointing to the same object B

Illustration:

 A_1 A_2 A_3 ... A_N | | | | | | V | \--->\--> B <-----/ 

It should be noted that a reference to a field in each object of type A indicates the same strong> object of type B

Now I am serializing an arr array containing objects of type A to an ObjectOutputStream . Then I deserialize the bytes received this way.

I get a new arr1 array.

1) Does the arr1 array arr1 objects of type A such that they all point to the same object of type B ? (I do not mean the same object before serialization, but the only newly created object of type B )

2) In other words, does serialization / deserialization in Java cause the same object graph to be preserved as before serialization? (i.e., a new deserialized graph of objects isomorphic to the old)

3) Where is this documented? (i.e. quote)

4) The same questions 1-3, but apply to Kryo for Java.

Thanks.

+6
source share
1 answer

http://docs.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html

The default serialization mechanism for an object records the class of the object, the class signature, and the values โ€‹โ€‹of all non-transient and non-static fields. References to other objects (except transition or static fields) force these objects to be written as well. Multiple references to a single object are encoded using the link so that the graphics of the objects can be restored in the same form as when writing the original.

As for my understanding of the specification, you get references to shared objects if shared object instances pass through the same ObjectOutputStream.

Therefore, when you serialize a class containing the arr array, each recorded object receives an identifier, and for each link passing through the stream, only that identifier is written. The deserialized graph in this case remains homogeneous with the original graph.

I'm sorry, but I can not help with the serialization mechanism of the krio library, I would be very glad to know from someone who used it.

EDIT about cryo:

Some documentation I found:

  • By default, each type of object in the graph after the first is saved as an integer serial number. This allows you to serialize multiple references to the same object and cyclic graphs. It has a small amount of overhead and can be turned off to save space if it is not needed: kryo.setReferences(false);

  • This (github) is a reference resolver contract; two implementations: ArrayList for charts of small objects based on a map for larger ones

  • This is the default implementation of the object serializer (de)

  • Classes must be registered for (de) serialization; each registered class can be associated with a serializer (among which, by default, the Java engine)

+9
source

Source: https://habr.com/ru/post/925521/


All Articles