#947 Serialization of cyclic references.

kaushik Sat 30 Jan 2010

consider the following code

class Main 
{
   Void main()
   {
       a := A()
       b := B()
       a.b = b
       b.a = a
Sys.out.writeObj(a)
   }

}

@serializable
class A{
   B? b
}

@serializable
class B{
   A? a
}

throws a StackOverflowError. Shouldn't we handle cyclic references like java does? The use case for me is that wisp tries to serialize everything that is put into session. Thus, I cannot have two objects having a reference to each other in the session.

DanielFath Sat 30 Jan 2010

Here is an excerpt from Docs.

Serialization

Serialization in Java is graph based - it will handle an arbitrary number of
references to a particular object. Fantom serialization is strictly tree based, it
will not attempt to keep track of object references - it is up to you design your 
data models as a tree. If you need to cross reference objects in your tree, then you 
should use a Uri or some other identifier.

Reasons for this are listed here as well: 864

So yeah it doesn't work like Java :( OTOH it is faster, simpler and easier to version. If I'm not mistaken you can create a new field id which will identify an object. You have to make sure it is unique though and that might be a tough pill to swallow.

kaushik Sat 30 Jan 2010

Alright. thanks Daniel!

Login or Signup to reply.