Just started studying serialization and serialization in xml.

Suppose we have a class Company . The company has fields parent (also an object of the class Company ) and daughters (also objects of the class Company ). Parents and daughters can also have their parents and daughters). And so we create several companies and put them in an ArrayList . Next we call the serializer in the xml file. It turns out that when you serialize each object from a sheet, the entire graph of an object is serialized, that is, the first element is serialized, the parent and daughters are serialized, and the parent and daughters are also serialized in the parent and daughters.

That is, it turns out that when serializing each element, the entire object graph is serialized. The question should be so?

    1 answer 1

    To avoid endlessly nested serialization and circular references, you need to set up the serializer accordingly. In this case, it will not put the elements describing your objects into each other, but will create links by some identifier.

    The specific implementation depends on which serializer you are using:

    • in JAXB this is done via @XmlIDREF ,

    • in XStream, via setMode(XPATH_RELATIVE_REFERENCES) , setMode(XPATH_ABSOLUTE_REFERENCES) and setMode(ID_REFERENCES) .