c# - Approaches to Serializing an Object Graph with Circular References -


let's suppose want implement serializer (c#) sake of practice, , want said serializer not fail on circular references.

the apparent solution serialize objects not yet encountered , skipping objects were. accomplished hashing instances (in 1 way or another).

the proposed solution bids question: "what defines object's identity?" 1 - leave gethashcode , equals methods. acceptable solution- conserves time on serialization , conserves memory on de-serialization.

however, not desirable outcome, since many instances may have same identity yet used different things in serialized domain, de-serializing them later on same instance violate domain logic.

so, author of such serializer, must leave caller make such decisions.

one approach solve hash collection per said type, , distinguishing between serialized , non serialized instances iterating collection , invoking referenceequals on each contained element. works, sub-optimal - performance wise.

another approach pin objects in unmanaged heap, , use pinned object address identity, seems kind of overkill , has lot of overhead well.

another approach use reflection invoke object.equals , object.gethashcode default implementations of every instance- seems solve problem, has own little overhead.

my questions are:

1)are there caveats have missed approaches i've suggested?
2)are there more additional approaches might have not thought of?

take @ system.runtime.serialization.objectidgenerator. that.

according msdn page:

using hash table, objectidgenerator retains id assigned object. object references, uniquely identify each object, addresses in runtime garbage-collected heap. object reference values can change during serialization, table updated automatically information correct.

the source code available here.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -