iMasters.com - Knowledge for Developers.
Follow Us
Home Backend .NET 8 first tests with Preview 1 + JSON deserialization improvements
Backend

.NET 8 first tests with Preview 1 + JSON deserialization improvements

At the end of February/2023 – Tuesday, 02/21Microsoft 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

.NET 8 Preview 1 as default, plus .NET 7 defaulted to “D:\DotNet 7”

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!

Written by
Renato Groffe

Worked as a consultant in systems development activities for over 15 years. Microsoft MVP (Most Valuable Professional) and MTAC (Multi-Plataform Technical Audience Contributor) program participant. Bachelor in Information Systems, specialization in Software Engineering, and MBA in Business Intelligence. He is also a lecturer and technical author on portals and specialized magazines, focusing on Microsoft technologies and good practices in the software area.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles

Backend

How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...

Backend

The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....

Backend

EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...

Backend

Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...