0

I am trying to visualize a C# object which is a SyntaxTree. JSON would be nice, but not required, I am just having a hard time dealing with the VS UI and all the circular references in the object. when I try to use JSON.NET to serialize it, I get a circular reference error. This isn't the first time I have run into a similar problem, but I thought I would work on it for a bit.

I know which keys need to be removed, they are ContainingElement, ContainingNamSpace, ContainingSourceUnit, ContainingType.

I was trying to create a function that could be reused to recursively remove those keys from the object before I pass it to JSON.NET, but I need some help.

public static object removeObjectsByName(object incoming, string WildcardRemove){
        List<object newObject = new List<object>();
        foreach (var item in incoming)
        {
            if (subItem.ToString().Contains(WildcardRemove))
            {
                continue;
            }
        }

        removeObject(newObject);
        return newObject;
}

I don't typically do a lot with generic types, but I don't know what all the incoming types of data will be, especially as we call it recursively. Should we create a List, Array, or leave it an object? Here is a sample of what I am looking at: enter image description here

The root of the object is named tree, I am specifically interested in the Functions which has 61 items in the array. As you dig into those you start to find the Containing* keys that need to be removed. Trouble is that they will also exist under Body and Properties. Then under Body, there is another Properties which will have it's Containing*.

I don't need any of that back referencing stuff, I just want each child without any keys named Containing*.

Any tips? Again, I don't care if the output is JSON, to the Console, as a List, if we use Linq or what, just trying to see my way through this.

Alan
  • 2,046
  • 2
  • 20
  • 43
  • Annotate the properties with `[JsonIgnore]` – Andrew Williamson Oct 27 '20 at 20:53
  • These aren't static classes that I have built, can you use `JsonIgnore` on a generic object? – Alan Oct 27 '20 at 23:25
  • 1
    You can tell Json.NET to ignore circular references during serialization by setting `ReferenceLoopHandling = ReferenceLoopHandling.Ignore`. See: [JSON.NET Error Self referencing loop detected for type](https://stackoverflow.com/q/7397207/3744182). You can tell Json.NET to ignore certain property names in runtime by using a custom contract resolver. See [How can I tell Json.NET to ignore properties in a 3rd-party object?](https://stackoverflow.com/q/25749509/3744182) for details. – dbc Oct 27 '20 at 23:41
  • 1
    If those answers don't also answer your question, might you please clarify your problem, ideally without using code screenshot images? (See [here](https://meta.stackoverflow.com/a/285557) for why.) – dbc Oct 27 '20 at 23:41
  • Thank you DBC, I will give that a try with contract resolver. I tried `ReferenceLoopHandling = ReferenceLoopHandling.Ignore` earlier today and I still got the same circular reference errors. I didn’t want to post an image, but I couldn’t ever get it to dump in a text format, which is actually the problem. – Alan Oct 28 '20 at 03:12
  • @Alan - you can get a circular reference error even with `ReferenceLoopHandling.Ignore` if you are using entity framework and enable dynamic proxies. See: [Circular reference detected exception while serializing object to JSON](https://stackoverflow.com/q/16949520/3744182). Also, if you need a traceback and can't get one because of an uncaught stack overflow, see [json.net limit maxdepth when serializing](https://stackoverflow.com/q/29651433/3744182) to artificially limit the serialization depth. – dbc Oct 29 '20 at 06:42

0 Answers0