{"id":114995,"date":"2024-04-08T07:56:16","date_gmt":"2024-04-08T05:56:16","guid":{"rendered":"https:\/\/code-maze.com\/?p=114995"},"modified":"2024-04-08T07:56:16","modified_gmt":"2024-04-08T05:56:16","slug":"aspnetcore-handling-circular-references-when-working-with-json","status":"publish","type":"post","link":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/","title":{"rendered":"Handling Circular References When Working With JSON in .NET"},"content":{"rendered":"<p>When working with JSON (JavaScript Object Notation) in .NET applications, developers often encounter challenges related to circular references, where objects circularly reference each other.\u00a0<\/p>\n<p>In this article, we&#8217;ll explore common scenarios involving circular references in .NET applications and discuss techniques for effectively handling them. We will use System.Text.Json for serialization and deserialization, one of the most popular NuGet libraries.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/json-csharp\/HandlingCircularRefsWhenWorkingWithJson\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s begin!<\/p>\n<h2>Understanding Circular References<a id=\"UnderstandingCircularReferences\"><\/a><\/h2>\n<p><strong>Circular references<\/strong> <strong>occur when an object refers back to itself through a reference directly or indirectly.<\/strong> In the context of JSON serialization, circular references arise when serializing complex object graphs where objects cyclically reference each other.<\/p>\n<p>As an example of a circular reference, let&#8217;s define a class and refer to a property of the same type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Employee\r\n{\r\n    public required string Name { get; set; }\r\n    public required string Surname { get; set; }\r\n    public required string Title { get; set; }\r\n    public Employee? Manager { get; set; }\r\n    public Collection&lt;Employee&gt; DirectReports { get; set; } = new Collection&lt;Employee&gt;();\r\n}<\/pre>\n<p>Here, we define the <code>Employee<\/code> class with a property <code>Employee? Manager<\/code> that holds a reference to the employee&#8217;s direct supervisor. In addition, we also define a <code>Collection&lt;Employee&gt; DirectReports<\/code> property that keeps a collection of the employee&#8217;s direct reports.<\/p>\n<p>These two properties form a circular reference:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/04\/json_circular_dependency.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-114997\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/04\/json_circular_dependency.png\" alt=\"Circular reference shown between Employee objects\" width=\"536\" height=\"499\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/04\/json_circular_dependency.png 536w, https:\/\/code-maze.com\/wp-content\/uploads\/2024\/04\/json_circular_dependency-300x279.png 300w\" sizes=\"auto, (max-width: 536px) 100vw, 536px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s see in the next sections what it means to return a JSON payload of the <code>Employee<\/code> class.<\/p>\n<h2><a id=\"DefaultJSONSerializationBehavior\"><\/a>Default JSON Serialization Behavior<\/h2>\n<p>Firstly, let&#8217;s see the default behavior of .NET <a href=\"https:\/\/code-maze.com\/introduction-system-text-json-examples\/\" target=\"_blank\" rel=\"noopener\">System.Text.Json<\/a>\u00a0when serializing the <code>Employee<\/code> class.<\/p>\n<p>Let&#8217;s create an ASP.NET Core Web API project with <code>Controllers<\/code>. We will use a <code>Services<\/code> layer to return the data to the <code>Controller<\/code> to implement the <a href=\"https:\/\/code-maze.com\/single-responsibility-principle\/\" target=\"_blank\" rel=\"noopener\">single responsibility principle<\/a>.<\/p>\n<p>Let&#8217;s now create an <code>EmployeeService<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class EmployeeService : IEmployeeService\r\n{\r\n    public IReadOnlyCollection&lt;Employee&gt; GetEmployees()\r\n    {\r\n        var manager = new Employee() { Name = \"Kate\", Surname = \"Wilson\", Title = \"Development Manager\" };\r\n        var engineer = new Employee() \r\n        { \r\n           Name = \"Adam\", Surname = \"Smith\", Title = \"Software Engineer\", Manager = manager\r\n        };\r\n        manager.DirectReports.Add(engineer);\r\n\r\n        return new[] { manager, engineer}.AsReadOnly();\r\n    }\r\n}<\/pre>\n<p>Here, we define the <code>EmployeeService<\/code> class and implement the <code>GetEmployees()<\/code> method.<\/p>\n<p>In our method, we create an array of two <code>Employee<\/code> objects and return it as a <code>ReadOnlyCollection<\/code>. The <code>manager<\/code> and <code>engineer<\/code> employees we define in the collection form a circular dependency.<\/p>\n<p>Let&#8217;s now create our <code>EmployeeController<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Route(\"api\/[controller]\")]\r\n[ApiController]\r\npublic class EmployeeController(IEmployeeService employeeService) : ControllerBase\r\n{\r\n    [HttpGet]\r\n    [ProducesResponseType(StatusCodes.Status200OK)]\r\n    public IActionResult GetEmployees() =&gt; Ok(employeeService.GetEmployees());\r\n}<\/pre>\n<p>Here, we use <a href=\"https:\/\/code-maze.com\/dependency-injection-aspnet\/\" target=\"_blank\" rel=\"noopener\">dependency injection<\/a> to pass our <code>IEmployeeService<\/code> interface into the <code>EmployeeController<\/code> class. Also, we define the <code>GetEmployees()<\/code> method that listens to <code>HttpGet<\/code> web calls and returns an <code>OkObjectResult<\/code> that contains the result of the <code>IEmployeeService<\/code> \u00a0<code>GetEmployees()<\/code> method.<\/p>\n<p>In the <code>Program<\/code> class, let&#8217;s register the dependency between <code>IEmployeeService<\/code> and <code>EmployeeService<\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">builder.Services.AddScoped&lt;IEmployeeService, EmployeeService&gt;();<\/code><\/p>\n<p>Now, let&#8217;s build and run our project, and<span style=\"font-weight: 400;\">\u00a0execute a <code>GET<\/code> request to our <code>\/api\/employee<\/code> endpoint:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Status: 500 Internal Server Error\r\nSystem.Text.Json.JsonException: A possible object cycle was detected. \r\nThis can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. \r\nConsider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.\r\nPath: $.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.Name.\r\n   at System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected(Int32 maxDepth)\r\n...<\/pre>\n<p>We get a <code>JsonException<\/code> error <code>A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32<\/code>.<\/p>\n<p>Additionally, we see the error path <code>$.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.DirectReports.Manager.Name...<\/code>\u00a0indicating the properties that caused the error.<\/p>\n<p>Also, the framework suggests a way to resolve the error <code>Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles<\/code>, so next let&#8217;s look at how to resolve it.<\/p>\n<h2><a id=\"HandlingCircularReferences\"><\/a>Handling Circular References<\/h2>\n<p>Now, let&#8217;s see what our options are to resolve the issue caused by the circular reference.<\/p>\n<p>The <code>System.Text.Json<\/code> library allows us to configure certain aspects of C# objects serialization into JSON. We can use it to configure things like formatting, encoding, trailing commas, and also the way that the library will handle circular references.\u00a0<\/p>\n<p><strong>Circular references<\/strong> are handled via the <code>ReferenceHandler<\/code> configuration option which can take two values <code>ReferenceHandler.IgnoreCycles<\/code> or <code>ReferenceHandler.Preserve<\/code>.<\/p>\n<p>Let&#8217;s try them out starting with the <strong><code>IgnoreCycles<\/code> <\/strong>option.<\/p>\n<h3><a id=\"ReferenceHandler.IgnoreCycles\"><\/a>ReferenceHandler.IgnoreCycles<\/h3>\n<p>In our <code>Program<\/code> class, let&#8217;s add <code>IgnoreCycles<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">builder.Services.AddControllers().AddJsonOptions(options =&gt;\r\n   options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);<\/pre>\n<p>The .NET framework will try to resolve the error by breaking the circular reference and returning <code>null<\/code> instead:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-highlight=\"12, 27\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"name\": \"Kate\",\r\n\u00a0\u00a0\u00a0\u00a0\"surname\": \"Wilson\",\r\n\u00a0\u00a0\u00a0\u00a0\"title\": \"Development Manager\",\r\n\u00a0\u00a0\u00a0\u00a0\"manager\": null,\r\n\u00a0\u00a0\u00a0\u00a0\"directReports\": [\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"name\": \"Adam\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"surname\": \"Smith\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"title\": \"Software Engineer\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"manager\": null,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"directReports\": []\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0]\r\n\u00a0\u00a0},\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"name\": \"Adam\",\r\n\u00a0\u00a0\u00a0\u00a0\"surname\": \"Smith\",\r\n\u00a0\u00a0\u00a0\u00a0\"title\": \"Software Engineer\",\r\n\u00a0\u00a0\u00a0\u00a0\"manager\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"name\": \"Kate\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"surname\": \"Wilson\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"title\": \"Development Manager\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"manager\": null,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"directReports\": [\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0null\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0]\r\n\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\"directReports\": []\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><code>\"manager\":{...}<\/code> and <code>\"directReports\":[...]<\/code> properties are replaced with <code>null<\/code>, which breaks the circular references as the objects no longer reference back to themselves.<\/p>\n<p>This works well and we can see the output in a simple format. However, the drawback of this solution is that it is difficult to understand the relationships by looking at the JSON result.<\/p>\n<p>Let&#8217;s now try the <code>ReferenceHandler.Preserve<\/code> option of <code>ReferenceHandler<\/code>.<\/p>\n<h3><a id=\"ReferenceHandler.Preserve\"><\/a>ReferenceHandler.Preserve<\/h3>\n<p>In our <code>Program<\/code> class, let&#8217;s change the configuration code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">builder.Services.AddControllers().AddJsonOptions(options =&gt;\r\n   options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);<\/pre>\n<p>With this approach, <span style=\"font-weight: 400;\">we try to resolve our issue with the <code>Preserve<\/code> handler. This handler introduces two new fields, <code>$id<\/code>, and <code>$ref<\/code><\/span>\u00a0which is used to preserve the relationships of the objects while breaking the circular reference:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-highlight=\"5,11,19,30\">{\r\n\u00a0\u00a0\"$id\": \"1\",\r\n\u00a0\u00a0\"$values\": [\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$id\": \"2\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"name\": \"Kate\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"surname\": \"Wilson\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"title\": \"Development Manager\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"manager\": null,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"directReports\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$id\": \"3\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$values\": [\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$id\": \"4\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"name\": \"Adam\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"surname\": \"Smith\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"title\": \"Software Engineer\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"manager\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$ref\": \"2\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"directReports\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$id\": \"5\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$values\": []\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"$ref\": \"4\"\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0]\r\n}<\/pre>\n<p>Each object is now declared once and assigned an <code>$id<\/code> which is used to reference the same object in other places in the JSON via a <code>$ref<\/code> field.<\/p>\n<p>This mechanism resolves the circular reference issue and also preserves relationships. However, its downside is that it makes the JSON output a bit more complicated to read and deserialize.\u00a0<\/p>\n<h2><a id=\"Conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we&#8217;ve seen how we can configure JSON serialization with <strong>System.Text.Json<\/strong> to work with circular references in C# and . NET. We&#8217;ve discussed what a circular reference is and observed the default behavior of the <strong>System.Text.Json<\/strong> library when working with circular references. Finally, we&#8217;ve also discussed two different options to resolve circular references, <strong>IgnoreCycles<\/strong> and <strong>Preserve<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with JSON (JavaScript Object Notation) in .NET applications, developers often encounter challenges related to circular references, where objects circularly reference each other.\u00a0 In this article, we&#8217;ll explore common scenarios involving circular references in .NET applications and discuss techniques for effectively handling them. We will use System.Text.Json for serialization and deserialization, one of the [&hellip;]<\/p>\n","protected":false},"author":131,"featured_media":62187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,1369],"tags":[10,1811,2157,927,1158],"class_list":["post-114995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-json","tag-net","tag-c","tag-circular-references","tag-json","tag-system-text-json","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Circular References When Working With JSON in .NET - Code Maze<\/title>\n<meta name=\"description\" content=\"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Circular References When Working With JSON in .NET - Code Maze\" \/>\n<meta property=\"og:description\" content=\"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-08T05:56:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Georgios Panagopoulos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Georgios Panagopoulos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\"},\"author\":{\"name\":\"Georgios Panagopoulos\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/375f2c440e54965e0e72ae38d4eb39b8\"},\"headline\":\"Handling Circular References When Working With JSON in .NET\",\"datePublished\":\"2024-04-08T05:56:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\"},\"wordCount\":728,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"keywords\":[\".NET\",\"C#\",\"Circular References\",\"JSON\",\"System.Text.Json\"],\"articleSection\":[\"C#\",\"JSON\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\",\"url\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\",\"name\":\"Circular References When Working With JSON in .NET - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"datePublished\":\"2024-04-08T05:56:16+00:00\",\"description\":\"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"width\":1100,\"height\":620,\"caption\":\"ASP.NET Core\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Handling Circular References When Working With JSON in .NET\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/375f2c440e54965e0e72ae38d4eb39b8\",\"name\":\"Georgios Panagopoulos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/03\/Georgios-Panagopoulos-400px.jpg-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/03\/Georgios-Panagopoulos-400px.jpg-150x150.png\",\"caption\":\"Georgios Panagopoulos\"},\"description\":\"Georgios is a software engineer and technical leader with over 15 years of experience. He has worked primarily with .NET technologies and specializes in C#, .NET &amp; .NET Core, ASP.NET Core, WPF, WCF, SQL, Azure, and Angular. In addition to coding, Georgios also loves mentoring and enjoys working on system design &amp; solution architecture, evaluating trade-offs for optimal outcomes.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/gpanagopoulos\/\"],\"url\":\"https:\/\/code-maze.com\/author\/georgios-panagopoulos\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Circular References When Working With JSON in .NET - Code Maze","description":"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/","og_locale":"en_US","og_type":"article","og_title":"Circular References When Working With JSON in .NET - Code Maze","og_description":"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.","og_url":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/","og_site_name":"Code Maze","article_published_time":"2024-04-08T05:56:16+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","type":"image\/png"}],"author":"Georgios Panagopoulos","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Georgios Panagopoulos","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/"},"author":{"name":"Georgios Panagopoulos","@id":"https:\/\/code-maze.com\/#\/schema\/person\/375f2c440e54965e0e72ae38d4eb39b8"},"headline":"Handling Circular References When Working With JSON in .NET","datePublished":"2024-04-08T05:56:16+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/"},"wordCount":728,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","keywords":[".NET","C#","Circular References","JSON","System.Text.Json"],"articleSection":["C#","JSON"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/","url":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/","name":"Circular References When Working With JSON in .NET - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","datePublished":"2024-04-08T05:56:16+00:00","description":"Resolving circular references with JSON in .NET that occur when objects circularly reference each other, whether directly or indirectly.","breadcrumb":{"@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","width":1100,"height":620,"caption":"ASP.NET Core"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/aspnetcore-handling-circular-references-when-working-with-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Handling Circular References When Working With JSON in .NET"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/375f2c440e54965e0e72ae38d4eb39b8","name":"Georgios Panagopoulos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/03\/Georgios-Panagopoulos-400px.jpg-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/03\/Georgios-Panagopoulos-400px.jpg-150x150.png","caption":"Georgios Panagopoulos"},"description":"Georgios is a software engineer and technical leader with over 15 years of experience. He has worked primarily with .NET technologies and specializes in C#, .NET &amp; .NET Core, ASP.NET Core, WPF, WCF, SQL, Azure, and Angular. In addition to coding, Georgios also loves mentoring and enjoys working on system design &amp; solution architecture, evaluating trade-offs for optimal outcomes.","sameAs":["https:\/\/www.linkedin.com\/in\/gpanagopoulos\/"],"url":"https:\/\/code-maze.com\/author\/georgios-panagopoulos\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/114995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=114995"}],"version-history":[{"count":5,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/114995\/revisions"}],"predecessor-version":[{"id":116331,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/114995\/revisions\/116331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62187"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=114995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=114995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=114995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}