Home DEVELOPER New in .NET 8.0 (29): Improvements to the JSON source generator

New in .NET 8.0 (29): Improvements to the JSON source generator

0


System.Text.Json In the standard version, JSON is mapped to .NET objects using runtime code generation. This has existed since version 6.0 System.Text.Json Also a source generator. It generates mapping code at development time, so that no runtime code generation is necessary. The JSON source generator was (and is) an important tool for performance optimization.

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.

However, with the expansion of the native AOT compiler to ASP.NET Core Web API, this has become even more important, since runtime compilation is not possible with native AOT.

Provides JSON source generator System.Text.Json Version 8.0 contains the following new options:

AWS: Lots of Excel tables in companies – GenAI applications can help
  • Source generator for JSON (Text.Json.SourceGeneration.JsonSourceGenerator) now also supports init-only properties introduced in C# 9.0 and required properties introduced in C# 11.0.
  • Developers can ensure this through the project settings System.Text.Json Runtime code generation no longer runs at all: false
  • Developers can use a static member in program code to ask whether runtime code generation is possible: IsReflectionEnabledByDefault
  • Regarding innovations in System.Text.Json 8.0 includes that annotation too (JsonSourceGenerationOptions) Now offers all the options that Classroom does JsonSerializerOptions When programming is a must with class Text.Json. JsonSerializer allowed.

If runtime code generation is turned off in the JSON serializer, but you still try to serialize without a source generator (for example) JsonSerializer.Serialize(new { value = 42 });), you receive the following error message: “Reflection-based serialization has been disabled for this application. Either use the source generator API or explicitly configure the ‘JsonSerializerOptions.TypeInfoResolver’ property.”

If runtime code generation is turned off in the JSON serializer, you’ll need to use one of JsonSerializerContext Create derived class through annotation (JsonSerializable) which receives references to the .NET classes to be (de)serialized. Alternatively you can set the settings via (JsonSourceGenerationOptions) Make




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 an introductory price,

Classes used in the following code Consultant And Person The examples from the previous part of the series were already in the code:

(JsonSerializable(typeof(Address)))
(JsonSerializable(typeof(Person))) 
(JsonSerializable(typeof(Consultant)))
(JsonSourceGenerationOptions(PreferredObjectCreationHandling 
 =JsonObjectCreationHandling.Populate))
 
internal partial class PersonJsonContext : JsonSerializerContext
{
}

You then need to specify this class in the serialization options:

var options = new JsonSerializerOptions
{
 TypeInfoResolver = PersonJsonContext.Default
};

The following list shows that System.Text.Json Now a required member that is not filled in during deserialization causes a runtime error in the JSON source generator.

try
{
 var jsonString = """
 {"FULL-NAME":"Holger Schwichtenberg","PERSONAL-WEBSITE":"www.dotnet-doktor.de"}
 """;
 Console.WriteLine("JSON: " + jsonString);
 var obj = JsonSerializer.Deserialize(jsonString, options);
 if (obj != null) CUI.Success(obj.ToString());
}
catch (Exception ex)
{
 CUI.PrintError(ex.Message); // JSON deserialization for type 'NET8Konsole.Consultant' was missing required properties, including the following: ID !!! Vor .NET 8.0 wäre hier in Verbindung mit Source Generator KEIN Laufzeitfehler aufgetreten. ID war = 0
}


(RME)

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version