Traversing an Object Graph

The object being serialized corresponds to the root node in the object graph. All other nodes in the graph represent an object that's reachable from the object being serialized, either directly as a member or indirectly via a member of a referenced object. The basic procedure to traverse an object graph for serialization is to start at the root object and obtain its serializable members. Then you traverse each serializable member's object graph and so forth until all nodes in the graph have been traversed. For acyclic object graphs, such as the one shown in Figure 8-2, the exercise is fairly simple.

Figure 8-2: An acyclic object graph

However, some object graphs might contain cycles. A cycle occurs when one object references another object that directly or indirectly references the original object. Cycles are problematic because if you don't detect them, you'll end up traversing the cycle forever. Figure 8-3 shows an abstract view of an object graph that contains a cycle.

0 0

Post a comment