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

Problem When you try to serialize an object with a circular, 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 … Read more Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property

C# – Case sensitivity in JSON deserialization

By default Newtonsoft does case insensitive JSON deserialization and System.Text.Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object. If you’re using case sensitive deserialization, then keys in the JSON string must match type names exactly, otherwise it won’t deserialize the class/property with the … Read more C# – Case sensitivity in JSON deserialization

System.Text.Json can’t serialize Dictionary unless it has a string key

The built-in JSON serializer in .NET Core can’t handle serializing a dictionary unless it has a string key. When I run this code I get the following exception: System.NotSupportedException: The collection type ‘System.Collections.Generic.Dictionary`2[System.Int32,System.String]’ is not supported. It can only serialize dictionaries with string keys. This is bizarre, and another reason to stick with Newtonsoft for … Read more System.Text.Json can’t serialize Dictionary unless it has a string key

Common Newtonsoft.Json options in System.Text.Json

If you’re switching from Newtonsoft.Json to System.Text.Json (or vice versa), you may be wondering how to specify the common options you’re used to using in Newtonsoft. For example, how do you specify the equivalent of Newtonsoft.Json.Converters.StringEnumConverter in System.Text.Json? The following table shows a few common serialization options used in Newtonsoft.Json and their equivalents in System.Text.Json. … Read more Common Newtonsoft.Json options in System.Text.Json

C# – Deserialize JSON to dynamic object

If you want to deserialize JSON without having to create a bunch of classes, use Newtonsoft.Json like this: Now you can use this object like any other object. Example Add the Newtonsoft.Json reference Code Output: JSON Dynamic vs ExpandoObject If you specify <dynamic> instead of <ExpandoObject> it’ll create a JObject. In older versions of Newtonsoft, … Read more C# – Deserialize JSON to dynamic object