System.Text.Json – How to serialize non-public properties

By default, the System.Text.Json.JsonSerializer only serializes public properties. If you want to serialize non-public properties, you can create a custom converter and use reflection to get the non-public properties too, like this: Use the custom converter by adding it to JsonSerializerOptions.Converters and passing the options in when serializing, like this: In this article, I’ll show … Read more

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property

Problem When you try to serialize an object using Newtonsoft.Json and there’s a circular reference, you get the following exception: Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property For example, I have a Parent class and Child class. The Parent object has a reference to a Child object, which has a back reference to the Parent … Read more