Home DEVELOPER New in .NET 8.0 (28): Extensions for deserialization of JSON objects

New in .NET 8.0 (28): Extensions for deserialization of JSON objects

0


with the newly introduced function UnmappedMemberHandling It’s about how System.Text.Json The behavior occurs when the object has more properties than the properties when deserializing.

Advertisement





Dr. Holger Schwittenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies through consulting and training as well as software development with 53 renowned experts. Through his appearances at numerous national and international conferences as well as over 90 expert books and over 1,500 expert articles, Holger Schwittenberg is one of the best-known experts for .NET and Web technologies in Germany.

In the example code below, this JSON string has a property “CITY” but no corresponding property exists City in class Consultant and its base class Person,

Classes Consultant And Person was already used in the previous section and is not reproduced here. Discontinuation of the naming convention was also discussed.

ECMAScript 2024: New version of the JavaScript standard released
try
{
 var jsonString = """
 {
 "ID":42,
 "FULL-NAME":"Holger Schwichtenberg",
 "PERSONAL-WEBSITE":"www.dotnet-doktor.de",
 "CITY":"Essen"
 }
 """;
 Console.WriteLine("JSON: " + jsonString);
 var obj = JsonSerializer.Deserialize(jsonString, options);
 if (obj != null) CUI.Success(obj.ToString());
}
catch (Exception ex)
{
 CUI.PrintError(ex.Message); 
}

would have happened till now System.Text.Json The extra information is easily overlooked. This is still the case in the standard edition. Through annotations (JsonUnmappedMemberHandling) on the class or property to be deserialized UnmappedMemberHandling in object JsonSerializerOptions Developers can now change this: The allowed values ​​are Skip (ignore as before) and Disallow,

The following shot of Disallow So the above solves it Deserialize() A runtime error: “The JSON property ‘CITY’ could not be mapped to any .NET member contained in the type ‘FCL_JSON.Consultant’.”

var options = new JsonSerializerOptions
{
 // NEU: Enumeration JsonNamingPolicy mit Namenskonventionen 
 // KebabCaseLower, KebabCaseUpper, SnakeCaseLower 
 // und SnakeCaseUppter als Alternative zum 
 // bisher fest gesetzten CamelCase
 PropertyNamingPolicy = JsonNamingPolicy.KebabCaseUpper, 
 // Standard wäre CamelCase
 
 // NEU: Einstellungen zur Handhabung zusätzlicher Daten
 // bei der Deserialisierung von JSON in Objekten 
 // via Annotation (JsonUnmappedMemberHandling) 
 // oder Eigenschaft UnmappedMemberHandling 
 // im Objekt JsonSerializerOptions.
 UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, 
 // Default wäre Skip
};




(Image: Dmytro Vikarchuk/Shutterstock))

In Online Conference BetterCode() .NET 9.0 On November 19, 2024 by iX and dpunkt.verlag, .NET experts from www.IT-Visions.de will present a ready-made version of .NET 9.0 using practical examples. These include the .NET 9.0 SDK, C# 13.0, ASP.NET Core 9.0, Blazor 9.0, Windows Forms 9.0, WPF 9.0, WinUI, innovations in .NET MAUI 9.0 and the integration of artificial intelligence into .NET applications. Program Offers six lectures, one discussion, and six workshops.

There are tickets Available at starting price,


(RME)

X/Twitter | Do you know what a tweet is called now? This is the definitive answer

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version