From 1cfd117dfa4464bfab43e7da1b3bde76df7de313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20=C3=87A=C4=9EDA=C5=9E?= Date: Mon, 6 Nov 2023 11:40:08 +0300 Subject: [PATCH 1/2] Added community article for .NET 8 Serialization Improvements --- .../POST.md | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md diff --git a/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md b/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md new file mode 100644 index 0000000000..07d678b5a5 --- /dev/null +++ b/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md @@ -0,0 +1,100 @@ +# .NET 8: Serialization Improvements + +The [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json) namespace provides functionality for serializing to and deserializing from JSON. In .NET 8, there have been many improvements to System.Text.Json serialization and deserialization functionality. + +## Source generator + +NET 8 includes enhancements of the System.Text.Json source generator that are aimed at making the Native AOT experience on par with the reflection-based serializer. This is important because, now you can select source generator for System.Text.Json. To see a comparison of Reflection versus source generation in System.Text.Json, check out comparison [documentation](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/reflection-vs-source-generation?pivots=dotnet-8-0#reflection). + +## Interface hierarchies + +.NET 8 adds support for serializing properties from interface hierarchies. Here is a code sample that shows this feature. + +```csharp +ICar car = new MyCar { Color = "Red", NumberOfWheels = 4 }; +JsonSerializer.Serialize(car); // {"Color":"Red","NumberOfWheels":4} + +public interface IVehicle +{ + public string Color { get; set; } +} + +public interface ICar : IVehicle +{ + public int NumberOfWheels { get; set; } +} + +public class MyCar : ICar +{ + public string Color { get; set; } + public int NumberOfWheels { get; set; } +} +``` + +## Naming policies + +Two new naming policies `snake_case` and `kebab-case` have been added. You can use them as shown below; + +```csharp +var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; +JsonSerializer.Serialize(new { CreationTime = new DateTime(2023,11,6) }, options); // {"creation_time":"2023-11-06T00:00:00"} +``` + +## Read-only properties + +With .NET 8, you can deserialize onto read-only fields or properties. This feature can be globally enabled by setting `PreferredObjectCreationHandling` to `JsonObjectCreationHandling.Populate`. You can also enable this feature for a class or one of its members by adding `[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]` attribute. Here is a sample; + +```csharp +using System.Text.Json; +using System.Text.Json.Serialization; + +var book = JsonSerializer.Deserialize("""{"Contributors":["John Doe"],"Author":{"Name":"Sample Author"}}""")!; +Console.WriteLine(JsonSerializer.Serialize(book)); + +class Author +{ + public required string Name { get; set; } +} + +[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] +class Book +{ + // Both of these properties are read-only. + public List Contributors { get; } = new(); + public Author Author { get; } = new() {Name = "Undefined"}; +} +``` + +Before .NET 8, the output was like this; + +```json +{"Contributors":[],"Author":{"Name":"Undefined"}} +``` + +With .NET 8, the output now looks like this; + +```json +{"Contributors":["John Doe"],"Author":{"Name":"Sample Author"}} +``` + +## Disable reflection-based default + +One of the nice features about Serialization is, now you can disable using the reflection-based serializer by default. To disable default reflection-based serialization, set the `JsonSerializerIsReflectionEnabledByDefault` MSBuild property to `false` in your project file. + +## Streaming deserialization APIs + +.NET 8 includes new `IAsyncEnumerable` streaming deserialization extension methods. The new extension methods invoke streaming APIs and return `IAsyncEnumerable`. Here is a sample code that uses this new feature. + +```csharp +const string RequestUri = "https://yourwebsite.com/api/saas/tenants?skipCount=0&maxResultCount=10"; +using var client = new HttpClient(); +IAsyncEnumerable tenants = client.GetFromJsonAsAsyncEnumerable(RequestUri); + +await foreach (Tenant tenant in tenants) +{ + Console.WriteLine($"* '{tenant.name}' uses '{tenant.editionName +}' edition"); +} +``` + +I have mentioned some of the items for Serialization Improvements in .NET 8. If you want to check the full list, you can read it .NET 8's [What's new document](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#serialization). \ No newline at end of file From 67d2653eb59810cfd49db4573b1cc4d196f4d4c5 Mon Sep 17 00:00:00 2001 From: Hamza Albreem <94292623+braim23@users.noreply.github.com> Date: Tue, 7 Nov 2023 08:22:23 +0300 Subject: [PATCH 2/2] Update POST.md --- .../POST.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md b/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md index 07d678b5a5..74022bd572 100644 --- a/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md +++ b/docs/en/Community-Articles/2023-11-06- SerializationDeserialization-Improvements/POST.md @@ -1,10 +1,10 @@ # .NET 8: Serialization Improvements -The [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json) namespace provides functionality for serializing to and deserializing from JSON. In .NET 8, there have been many improvements to System.Text.Json serialization and deserialization functionality. +The [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json) namespace provides functionality for serializing to and deserializing from JSON. In .NET 8, there have been many improvements to the System.Text.Json serialization and deserialization functionality. ## Source generator -NET 8 includes enhancements of the System.Text.Json source generator that are aimed at making the Native AOT experience on par with the reflection-based serializer. This is important because, now you can select source generator for System.Text.Json. To see a comparison of Reflection versus source generation in System.Text.Json, check out comparison [documentation](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/reflection-vs-source-generation?pivots=dotnet-8-0#reflection). +.NET 8 includes enhancements of the System.Text.Json source generator that are aimed at making the Native AOT experience on par with the reflection-based serializer. This is important because now you can select the source generator for System.Text.Json. To see a comparison of Reflection versus source generation in System.Text.Json, check out the comparison [documentation](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/reflection-vs-source-generation?pivots=dotnet-8-0#reflection). ## Interface hierarchies @@ -33,7 +33,7 @@ public class MyCar : ICar ## Naming policies -Two new naming policies `snake_case` and `kebab-case` have been added. You can use them as shown below; +Two new naming policies `snake_case` and `kebab-case` have been added. You can use them as shown below: ```csharp var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; @@ -42,7 +42,7 @@ JsonSerializer.Serialize(new { CreationTime = new DateTime(2023,11,6) }, options ## Read-only properties -With .NET 8, you can deserialize onto read-only fields or properties. This feature can be globally enabled by setting `PreferredObjectCreationHandling` to `JsonObjectCreationHandling.Populate`. You can also enable this feature for a class or one of its members by adding `[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]` attribute. Here is a sample; +With .NET 8, you can deserialize onto read-only fields or properties. This feature can be globally enabled by setting `PreferredObjectCreationHandling` to `JsonObjectCreationHandling.Populate`. You can also enable this feature for a class or one of its members by adding the `[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]` attribute. Here is a sample: ```csharp using System.Text.Json; @@ -65,13 +65,13 @@ class Book } ``` -Before .NET 8, the output was like this; +Before .NET 8, the output was like this: ```json {"Contributors":[],"Author":{"Name":"Undefined"}} ``` -With .NET 8, the output now looks like this; +With .NET 8, the output now looks like this: ```json {"Contributors":["John Doe"],"Author":{"Name":"Sample Author"}} @@ -97,4 +97,4 @@ await foreach (Tenant tenant in tenants) } ``` -I have mentioned some of the items for Serialization Improvements in .NET 8. If you want to check the full list, you can read it .NET 8's [What's new document](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#serialization). \ No newline at end of file +I have mentioned some of the items for Serialization Improvements in .NET 8. If you want to check the full list, you can read it in the .NET 8's [What's new document](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#serialization).