JSON Serializer System.Text.Json
,NuGet Packages) received some enhancements in version 8.0.
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.
Unlike other parts of .NET 8.0 such as Entity Framework Core 8.0 System.Text.Json
Not only on .NET 8.0, but on older .NET versions as well. System.Text.Json
Works with classic .NET Framework from version 4.6.2 and modern .NET from version 2.0.
Version 8.0 uses naming conventions to serialize object properties to JSON and to deserialize from JSON KebabCaseLower
, KebabCaseUpper
, SnakeCaseLower
And SnakeCaseUpper
In the calculation JsonNamingPolicy
as an alternative to what was previously prescribed CamelCasing
,
Examples of these naming conventions include:
PascalCase
: Full NameCamelCase
: Full NameKebabCaseLower
: Full NameKebabCaseUpper
: Full NameSnakeCaseLower
: Full NameSnakeCaseUpper
: Full Name
comes in the following list KebabCaseUpper
When serializing and deserializing an instance of a class Consultant
for use:
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 ist CamelCase
};
…
var consultant = new Consultant() { ID = 42, FullName = "Holger Schwichtenberg", Salutation = "Dr.", PersonalWebsite = "www.dotnet-doktor.de" };
consultant.Languages.AddRange(("C#", "JavaScript", "TypeScript"));
consultant.Address = new Address() { City = "Essen", Country = "Germany" };
Console.WriteLine("Objekt im RAM:");
Console.WriteLine(consultant);
Console.WriteLine();
CUI.H3("Serialisierung (KebabCaseUpper):");
string json1 = JsonSerializer.Serialize(consultant, options);
Console.WriteLine(json1); // {"ID":42,"FULL-NAME":"Holger Schwichtenberg","SALUTATION":"Dr.","PERSONAL-WEBSITE":"www.dotnet-doktor.de"}
…
CUI.H3("\nDeserialisierung: (vollständig)");
try
{
var jsonString = """
{"ID":42,"FULL-NAME":"Holger Schwichtenberg","SALUTATION":"Dr.",
"PERSONAL-WEBSITE":"www.dotnet-doktor.de",
"ADDRESS":{"COUNTRY":"Germany","CITY":"Essen"}}
""";
Console.WriteLine(jsonString);
var obj = JsonSerializer.Deserialize(jsonString, options);
if (obj != null) CUI.Success(obj.ToString());
}
catch (Exception ex)
{
CUI.PrintError(ex.Message);
}
Listing 2 below shows data objects Person
And Consultant
And their interface:
using System.Text.Json.Serialization;
namespace FCL_JSON;
public interface IBusinessObject
{
UInt128 ID { get; init; }
}
public interface IPerson : IBusinessObject
{
string? Salutation { get; set; }
string? FullName { get; set; }
public Address Address { get; set; }
}
public interface IConsultant : IPerson
{
string? PersonalWebsite { get; set; }
public List Languages { get; }
}
public class Address
{
public string Country { get; set; }
public string City { get; set; }
}
public class Person : IPerson
{
public Person() { }
public required UInt128 ID { get; init; }
public required string FullName { get; set; }
public string? Salutation { get; set; }
public Address Address { get; set; }
public override string ToString()
{
return $"Person {ID}: {(!String.IsNullOrEmpty(Salutation) ? Salutation + " " : "")}{FullName} wohnt in {Address?.City ?? "UNBEKANNT"} ";
}
}
public class Consultant : Person, IConsultant
{
public Consultant() { }
public List Languages { get; } = new();
public string? PersonalWebsite { get; set; }
public override string ToString()
{
return $"Consultant {ID}: {(!String.IsNullOrEmpty(Salutation) ? Salutation + " " : "")}{FullName} wohnt in {Address.City} -> {PersonalWebsite} Sprachen: {String.Join('+', Languages)}";
}
}
(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)