I'm trying to serialise objects (C#) that I have no control over using Json.NET. These objects have many properties that are references to other objects, which in turn have many such properties themselves, and so on. A plain call to JsonConvert.SerializeObject doesn't seem to finish. I stop it after several minutes.
I can't know the names of the properties I need but there are ways I can define what I need (like hierarchy or specific functions) so that I can "prune this tree" significantly. The problem is that JsonConvert seems to "traverse the tree" no matter what. I have defined my own JsonConverter and overrode CanConvert. Following the code using a break shows me that, even when CanConvert returns false for an object property, JsonConvert still goes inside that object and starts checking its properties.
If I was to define CanConvert such that it simply returns false, the call to serialise still never finishes. I would have expected JsonConvert to only look at "the first level" of objects and never look inside them once it determines that they don't need to be serialised.
Am I using the wrong functionality to achieve what I need or am I missing something? I have looked into ContractResolvers but don't think they are what I'm looking for. I could use a different framework rather than Json.NET or even something XML-based. Any suggestions?