Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java Serialization work for cyclic references?

For example: Object A contains Object B that contains Object C that contains Object A.

Will Object A serialize properly?

Comment #9 here indicates that it does not work .

In contrast, XStream indicates that it does handle cyclic references.

like image 678
Brandon Avatar asked Nov 24 '09 19:11

Brandon


People also ask

What are the disadvantages of serialization?

Since serialization does not offer any transaction control mechanisms per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.

What is cyclic reference in Java?

A circular reference happens when one object refers to another, and that other one refers to the first object.

What is the main reason of Java serialization?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

Is Java serialization deprecated?

So far, no JDK Enhancement Proposal (JEP) has been proposed publicly to deprecate the serialization mechanism in Java 11, which is expected to be released later this year.


1 Answers

Yes, the default Java serialization works for cyclic references. When you serialize object C, the field will contain a backreference to the already-serialized object A instead of serializing it again.

like image 138
Steven Schlansker Avatar answered Sep 26 '22 13:09

Steven Schlansker