ASP.NET JSON Self Referencing Properties

Posted by Joe Enos on March 31, 2021 · 1 min read

In object-oriented languages, an object can hold a reference to itself in a property, and there's no problem with that. However, when you go to serialize that object, you run into issues.

An object with a self-referencing property that loops back to itself would cause an infinite loop and crash when trying to serialize it. Here's how to avoid the error in ASP.NET.

I'd recommend against the idea in the first place, opting instead for simple data types, but this is here if you need it.

View code on GitHub