2

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?

Serital
  • 343
  • 3
  • 13
  • 2
    Have you tried setting `ReferenceLoopHandling` to `Ignore` or setting `PreserveReferencesHandling` to `Objects`? See [What is the difference between PreserveReferencesHandling and ReferenceLoopHandling in Json.Net?](http://stackoverflow.com/q/23453977/10263) for a full explanation of what these settings do. You can also use a ContractResolver to turn off serialization of certain properties within your objects. See [How do I make JSON.NET ignore object relationships?](http://stackoverflow.com/q/21292010/10263) for an example of this idea. – Brian Rogers Oct 11 '16 at 14:41
  • 1
    If you make `CanConvert` return false, what you are doing is telling Json.NET to fall back to default serialization, which is not what you want. Instead, you would need to write an empty object. Or, alternatively, you could use `DepthPruningContractResolver` from [Json.NET serialize by depth and attribute](https://stackoverflow.com/questions/36159424/). – dbc Oct 11 '16 at 17:17
  • @BrianRogers The problem is not circular referencing. The object I'm dealing with is simply an API into a very large cloud application. It is a giant tree of objects, all "empty shells" until they are populated with data with a web call. – Serital Oct 12 '16 at 08:27
  • @dbc Although that solution might keep the unnecessary objects out of the JSON output, I don't think it will solve the performance issue. If it checks all objects and properties if they should be serialised (and I can't possibly know the Type of all properties in all objects) then the problem will persist. I need the checks to be cut off at certain "branches of the tree", since the tree is simply massive – Serital Oct 12 '16 at 08:31

0 Answers0