| |

NET 8 – Include non-public members in JSON serialization

Starting with .NET 8 you can include non public properties in the serialization when using System.Text.Json. To do so, simply decorate the non-public property with the JsonIncludeAttribute attribute. The attribute works with any non-public modifier, such as private, protected or internal. Let’s look at an example: As expected, this will output the following:

|

dotnet workload clean

Note: this command is only available starting with .NET 8. This command cleans up workload packs that might be left over after an .NET SDK or Visual Studio update. It can be useful when encountering issues while managing workloads. dotnet workload clean will clean up orphaned packs resulted from uninstalling .NET SDKs. The command will…

|

NET 8 – Deserialize into read-only properties

Starting with .NET 8 you can deserialize into properties which do not have a set accessor. You can opt-in for this behavior using JsonSerializerOptions, or on a per-type basis using the JsonObjectCreationHandling attribute. Using JsonObjectCreationHandling attribute You can annotate your type with the System.Text.Json.Serialization.JsonObjectCreationHandling attribute, passing your option as a parameter. Using JsonSerializerOptions You can…

|

NET 8 – Handle missing members during JSON deserialization

By default, if you have additional properties in a JSON payload you are trying to deserialize, they are simply ignored. But what if you wanted the deserialization to fail and throw an exception when there are extra properties in the JSON? That is possible starting with .NET 8. There are several in which you can…

|

.NET 8 – JSON serialize property names using snake case and kebab case

.NET 8 introduces several new naming policies that can be used with the System.Text.Json serializer. To name them: Let’s look at the serialized output for each of them. For this, we’re going to use a Car class with the following definition: And we are going to serialize the following object instance: Lower snake case (snake_case)…

|

.NET 8 performance: Dictionary vs. FrozenDictionary

With .NET 8 we are introduced to a new dictionary type which improves the performance of read operations. The catch: you are not allowed to make any changes to the keys and values once the collection is created. This type is particularly useful for collections that are populated on first use and then persisted for…