.NET JsonSerializer is over 200x faster if you reuse JsonSerializerOptions

When you serialize an object using the built-in .NET JsonSerializer (System.Text.Json), reusing the JsonSerializerOptions object is 200x faster than serializing with a new options object each time. The JsonSerializerOptions object must have some kind of internal caching mechanism that makes subsequent serializations on the same type significantly faster. Note: Creating the new JsonSerializerOptions object adds … Read more .NET JsonSerializer is over 200x faster if you reuse JsonSerializerOptions

C# – How to copy an object

In this article I’ll explain how to copy an object. First I’ll explain the difference between shallow and deep copying. Then I’ll show how to shallow copy and four different ways to deep copy – including manually copying and using serialization. Finally I’ll show a speed and feature comparison of each approach, and a decision … Read more C# – How to copy an object

C# – How to use enum flags to pass multiple options as a single parameter

When you need to pass multiple options into a method, normally you would add a bool flag for each parameter, like: Not only is using bool flags a bit of a code smell, but when this list of parameters gets too big, you’ll probably want to refactor by moving the bool flags into an options … Read more C# – How to use enum flags to pass multiple options as a single parameter

JsonException: The JSON value could not be converted to Enum

When you’re using System.Text.Json to deserialize JSON that contains the string representation of an enum, you get the following exception: System.Text.Json.JsonException: The JSON value could not be converted to <Enum Type> The following JSON would cause this exception. Conference is an enum, and this is using the string representation “NFC” instead of the numeric value … Read more JsonException: The JSON value could not be converted to Enum

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