At the end of February/2023 – Tuesday, 02/21 – Microsoft made available .NET 8 Preview 1, the first release of this new version for evaluation (with launch scheduled so far for November/2023). Information about some of the new functionality and features can be found on the official .NET platform blog:
https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-1/
As with other versions of .NET, .NET 8 will feature numerous improvements involving aspects such as performance, containerization, and new features in pre-existing types.
I even installed this new version in a Windows environment that I use for testing:
Installation of .NET 8 Preview 1 can be obtained from:
https://dotnet.microsoft.com/en-us/download/dotnet/8.0
Seeking to avoid undesirable side effects that may be caused by this release, I leave here a tip… As I always do in the first Previews of a future version of .NET (.NET 5, .NET 6, .NET 7…), I recommend configuring which release should be taken as default. In the following article, I show how easy it is to perform such a configuration using the global.json file for this:
.NET Core: Setting the SDK version used by the dotnet new command
In the next sections, I cover the support offered by Visual Studio 2022 to .NET 8 Preview 1, as well as new features in JSON deserialization brought by this release.
Support in Visual Studio 2022
When using Visual Studio 2022 version 17.5.0 Preview 1.0, .NET 8 Preview 1 will appear as one of the available alternatives:
JSON deserialization improvements
.NET 8 also brings an improvement aimed at the deserialization of JSON strings into objects: this is the possibility of generating errors during this process in case an element present in the text sequence does not exist in the type that corresponds to the deserialized result.
One way to implement this practice involves using the JsonUnmappedMemberHandling attribute (the JsonUnmappedMemberHandlingAttribute type is found in the System.Text.Json.Serialization namespace). In addition to Disallow applied in the following example with the Pessoa class (which will result in an exception if a member is not defined in the type that represents the deserialized value), we also have the option of Skip (previously standard behavior, in which deserialization occurs even with a missing member in the target type):
https://gist.github.com/renatogroffe/b7fdedb902724f971c469bc30c17fe03#file-pessoa-cs
The next listing brings an example of using the Pessoa class:
-> The first string has the Peso element, which was not declared in Pessoa. By associating the Disallow value to the JsonUnmappedMemberHandling attribute, we expect an exception to be thrown for this text;
-> A second string, in which the 2 elements present were defined in the Pessoa type.
https://gist.github.com/renatogroffe/73c025ab04e449b0df7606452d39d70e#file-program-cs
When executing the corresponding application, an exception will then be thrown, deserializing the first string, as well as the normal occurrence of this process with the second value:
This example is also available on GitHub:
https://github.com/renatogroffe/DotNet8-ConsoleApp-JsonUnmappedMemberHandlingAttribute
If you find this useful, please give a ⭐️ to support. I also invite you to follow me on GitHub!
Leave a comment