I am serializing objects (similar to EF entities) or a list of objects that have references to other objects. The references of the objects get serialized, but their references won't be serialized, instead I write an int ID. The problem is that writer.CurrentDepth starts from 1 at the first Write if we are serializing a list, and starts from 0 if we are serializing an object. So at list I should stop serializing at depth 2, and at objects at depth 1, but I don't know if I am serializing a list or an object because besides writer.CurrentDepth I don't know anything else about the state of the serialization, of where we are in the process. How to find out if we are serializing an object or an array in deeper depths? I can get this info at the first call to Write, but how to get this info at the next Write calls?
I could use a static variable or a scoped service to store if we are serializing an object or an array, but this looks very ugly.