NET 8 – Serialization of Half, Int128, and UInt128
System.Text.Json brings out of the box support for additional types: Let’s look at an example:
System.Text.Json brings out of the box support for additional types: Let’s look at an example:
Starting with .NET 8, both Memory<byte[]> and ReadOnlyMemory<byte[]> are serialized as Base64 strings. Let’s look at a quick example: In contrast, Memory<int> and the likes will continue to be serialized as JSON arrays.
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:
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…
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…
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…
dotnet script enables you to run C# scripts (.CSX) from the .NET CLI. The only requirement is to have .NET 6 or newer installed on your machine. You can use the following command to install dotnet-script globally: Then to execute a script file you simply call dotnet script <file_path> like in the example below: How…
You can use the WaitForExit method to wait for the process to complete. Your code will wait synchronously for the process to finish, then it will resume execution. Let’s look at an example: The code above will start a new cmd.exe process, and execute the timeout 5 command. The process.WaitForExit() call will force your program…
.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)…
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…
The correct syntax for generating a project with AOT is –aot (with 2 hyphens). In this particular case, the correct command would be:
When using dotnet script you can pass arguments by specifying them after — (two dashes). You can the access the arguments in the script using the Args collection. Let’s take an example. Assume we have the following myScript.csx script file: We can pass parameter to this script as follows: