diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs index 0cd904f646864..7c10a05bab851 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs @@ -16,9 +16,7 @@ public class WeatherForecast // [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(WeatherForecast))] - internal partial class SourceGenerationContext : JsonSerializerContext - { - } + internal partial class SourceGenerationContext : JsonSerializerContext { } // public class Program @@ -35,7 +33,7 @@ public static void Main() WeatherForecast? weatherForecast; // - weatherForecast = JsonSerializer.Deserialize( + weatherForecast = JsonSerializer.Deserialize( jsonString, SourceGenerationContext.Default.WeatherForecast); // Console.WriteLine($"Date={weatherForecast?.Date}"); @@ -56,9 +54,7 @@ public static void Main() { TypeInfoResolver = SourceGenerationContext.Default }; - weatherForecast = JsonSerializer.Deserialize( - jsonString, typeof(WeatherForecast), sourceGenOptions) - as WeatherForecast; + weatherForecast = JsonSerializer.Deserialize(jsonString, sourceGenOptions); // Console.WriteLine($"Date={weatherForecast?.Date}"); // output: @@ -86,8 +82,7 @@ public static void Main() TypeInfoResolver = SourceGenerationContext.Default }; - jsonString = JsonSerializer.Serialize( - weatherForecast, typeof(WeatherForecast), sourceGenOptions); + jsonString = JsonSerializer.Serialize(weatherForecast, sourceGenOptions); // Console.WriteLine(jsonString); // output: diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs deleted file mode 100644 index 0b782ed6680b3..0000000000000 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs +++ /dev/null @@ -1,59 +0,0 @@ -// -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace JsonSerializerOptionsExample -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - public int TemperatureCelsius { get; set; } - public string? Summary { get; set; } - } - - // - [JsonSerializable(typeof(WeatherForecast))] - internal partial class OptionsExampleContext : JsonSerializerContext - { - } - // - - public class Program - { - public static void Main() - { - string jsonString = """ - { - "date": "2019-08-01T00:00:00", - "temperatureCelsius": 25, - "summary": "Hot" - } - """; - WeatherForecast? weatherForecast; - - // - weatherForecast = JsonSerializer.Deserialize( - jsonString, - typeof(WeatherForecast), - new OptionsExampleContext( - JsonSerializerOptions.Web)) - as WeatherForecast; - // - Console.WriteLine($"Date={weatherForecast?.Date}"); - // output: - //Date=8/1/2019 12:00:00 AM - - // - jsonString = JsonSerializer.Serialize( - weatherForecast, - typeof(WeatherForecast), - new OptionsExampleContext( - JsonSerializerOptions.Web)); - // - Console.WriteLine(jsonString); - // output: - //{ "date":"2019-08-01T00:00:00","temperatureCelsius":25,"summary":"Hot"} - } - } -} -// diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs index 23577f182b50c..803d57a424b54 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs @@ -51,7 +51,7 @@ public static void Main() // Serialize with context that selects metadata mode only for WeatherForecast only. // jsonString = JsonSerializer.Serialize( - weatherForecast!, + weatherForecast, MetadataOnlyWeatherForecastOnlyContext.Default.WeatherForecast); // Console.WriteLine(jsonString); @@ -70,7 +70,7 @@ public static void Main() // Serialize with context that selects metadata mode only. // jsonString = JsonSerializer.Serialize( - weatherForecast!, MetadataOnlyContext.Default.WeatherForecast); + weatherForecast, MetadataOnlyContext.Default.WeatherForecast); // Console.WriteLine(jsonString); // output: diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/ObjectProperties.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/ObjectProperties.cs index 520907d06503a..6ec4ea85ce73d 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/ObjectProperties.cs +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/ObjectProperties.cs @@ -1,5 +1,4 @@ // -using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; @@ -27,7 +26,7 @@ public class Program public static void Main() { // - WeatherForecast wf = new() { Data = true, DataList = new List { true, 1 } }; + WeatherForecast wf = new() { Data = true, DataList = [true, 1] }; // string json = JsonSerializer.Serialize(wf, WeatherForecastContext.Default.WeatherForecast); Console.WriteLine(json); diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/Program.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/Program.cs index 792abc3da3428..66abebdd4f9df 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/Program.cs +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/Program.cs @@ -1,9 +1,4 @@ -using System; -using System.IO; -using System.Text; -using System.Threading.Tasks; - -namespace SystemTextJsonSamples +namespace SystemTextJsonSamples { public class Program { @@ -17,8 +12,6 @@ static void Main() MetadataOnlyNoOptions.Program.Main(); Console.WriteLine("\n============================= SerializeOnlyWithOptions\n"); SerializeOnlyWithOptions.Program.Main(); - Console.WriteLine("\n============================= JsonSerializerOptionsExample\n"); - JsonSerializerOptionsExample.Program.Main(); Console.WriteLine("\n============================= ObjectProperties\n"); ObjectProperties.Program.Main(); } diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SerializeOnlyWithOptions.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SerializeOnlyWithOptions.cs index 59f1858fd8dd0..498d145c30b93 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SerializeOnlyWithOptions.cs +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SerializeOnlyWithOptions.cs @@ -27,7 +27,9 @@ public static void Main() { string jsonString; WeatherForecast weatherForecast = new() - { Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot" }; + { Date = DateTime.Parse("2019-08-01"), + TemperatureCelsius = 25, + Summary = "Hot" }; // Serialize using TypeInfo provided by the context // and options specified by [JsonSourceGenerationOptions]. diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SystemTextJsonSamples.csproj b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SystemTextJsonSamples.csproj index 30b36c1058177..658f626f4bdd1 100644 --- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SystemTextJsonSamples.csproj +++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/SystemTextJsonSamples.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + net10.0 SystemTextJsonSamples.Program enable enable diff --git a/docs/standard/serialization/system-text-json/source-generation.md b/docs/standard/serialization/system-text-json/source-generation.md index bcd19481182cd..dc16d62c62dad 100644 --- a/docs/standard/serialization/system-text-json/source-generation.md +++ b/docs/standard/serialization/system-text-json/source-generation.md @@ -1,7 +1,7 @@ --- title: How to use source generation in System.Text.Json description: "Learn how to use source generation in System.Text.Json." -ms.date: 10/09/2023 +ms.date: 11/13/2025 no-loc: [System.Text.Json] dev_langs: - "csharp" @@ -15,7 +15,7 @@ ms.topic: how-to # How to use source generation in System.Text.Json -Source generation in System.Text.Json is available in .NET 6 and later versions. When used in an app, the app's language version must be C# 9.0 or later. This article shows you how to use source-generation-backed serialization in your apps. +This article shows you how to use source-generation-backed System.Text.Json serialization in your apps. For information about the different source-generation modes, see [Source-generation modes](source-generation-modes.md). @@ -29,15 +29,15 @@ To use source generation with all defaults (both modes, default options): - Takes a instance, or - Takes a instance, or - - Takes a instance and you've set its property to the `Default` property of the context type (.NET 7 and later only). + - Takes a instance and you've set its property to the `Default` property of the context type. -By default, both source generation modes are used if you don't specify one. For information about how to specify the mode to use, see [Specify source generation mode](#specify-source-generation-mode) later in this article. +By default, both source generation modes (*metadata-based* and *serialization optimization*) are used if you don't specify one. For information about how to specify the mode to use, see [Specify source generation mode](#specify-source-generation-mode) later in this article. -Here's the type that is used in the following examples: +Here's the type that's used in the following examples: :::code language="csharp" source="snippets/source-generation/csharp/BothModesNoOptions.cs" id="WF"::: -Here's the context class configured to do source generation for the preceding `WeatherForecast` class: +Here's the context class configured to generate source for the preceding `WeatherForecast` class: :::code language="csharp" source="snippets/source-generation/csharp/BothModesNoOptions.cs" id="DefineContext"::: @@ -45,7 +45,7 @@ The types of `WeatherForecast` members don't need to be explicitly specified wit :::code language="csharp" source="snippets/source-generation/csharp/ObjectProperties.cs" id="WF"::: -And you know that at runtime it may have `boolean` and `int` objects: +And you know that at run time it might have `boolean` and `int` objects: :::code language="csharp" source="snippets/source-generation/csharp/ObjectProperties.cs" id="WFInit"::: @@ -95,7 +95,7 @@ Here are the preceding examples in a complete program: ## Specify source-generation mode -You can specify metadata-based mode or serialization-optimization mode for an entire context, which may include multiple types. Or you can specify the mode for an individual type. If you do both, the mode specification for a type wins. +You can specify metadata-based mode or serialization-optimization mode for an entire context, which might include multiple types. Or you can specify the mode for an individual type. If you do both, the mode specification for a type wins. - For an entire context, use the property. - For an individual type, use the property. @@ -161,9 +161,7 @@ services.AddControllers().AddJsonOptions( ``` > [!NOTE] -> , or fast-path serialization, isn't supported for asynchronous serialization. -> -> In .NET 7 and earlier versions, this limitation also applies to synchronous overloads of that accept a . Starting with .NET 8, even though streaming serialization requires metadata-based models, it will fall back to fast-path if the payloads are known to be small enough to fit in the predetermined buffer size. For more information, see . +> , or *fast-path* serialization, isn't supported for asynchronous serialization. Even though streaming serialization requires metadata-based models, it falls back to fast-path if the payloads are known to be small enough to fit in the predetermined buffer size. For more information, see . ## Disable reflection defaults @@ -218,7 +216,7 @@ If you call a method that lets you pass in your own instance of `Utf8JsonWriter` If you create and use a context instance by calling the constructor that takes a `JsonSerializerOptions` instance, the supplied instance will be used instead of the options specified by `JsonSourceGenerationOptionsAttribute`. -Here are the preceding examples in a complete program: +The following code shows the preceding examples in a complete program: :::code language="csharp" source="snippets/source-generation/csharp/SerializeOnlyWithOptions.cs" id="All":::