From 48e273139d2e5d6f5ec403171cc650fb833af87b Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 10:37:29 -0800 Subject: [PATCH 1/8] Revise AI section Update the document to improve the section on using AI for serialization. Added details on handling nested and polymorphic types. --- .../serialization/system-text-json/how-to.md | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index aa0e4de0688fe..67230ad8ede11 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -1,7 +1,7 @@ --- title: "How to serialize JSON in C#" description: "Learn how to use the System.Text.Json namespace to serialize to JSON in .NET. Includes sample code." -ms.date: 02/11/2025 +ms.date: 11/20/2025 no-loc: [System.Text.Json, Newtonsoft.Json] dev_langs: - "csharp" @@ -108,16 +108,21 @@ Starting in .NET 9, you can also customize the indent character and size using < > [!TIP] > If you use `JsonSerializerOptions` repeatedly with the same options, don't create a new `JsonSerializerOptions` instance each time you use it. Reuse the same instance for every call. For more information, see [Reuse JsonSerializerOptions instances](configure-options.md#reuse-jsonserializeroptions-instances). -## Use AI to serialize to JSON +## Use AI to serialize nested and polymorphic types -You can use AI tools, such as GitHub Copilot, to generate code that uses `System.Text.Json` to serialize to JSON. You can customize the prompt to use object fields that suit your requirements. +You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON `System.Text.Json`. You can use it for nested and polymorphic objects. +For example, you can prompt Copilot to generate code that handles nested and polymorphic types, which is useful for creating examples or test data for hierarchical object structures. -The following text shows an example prompt for Copilot Chat: +Here's an example prompt you can use in Visual Studio Code Copilot Chat to migrate a solution. ```copilot-prompt -Generate code to use System.Text.Json to serialize an object to a JSON string. -The object contains the following fields: FirstName (string), Lastname (string), Age (int). -Provide example output. +Generate C# code using System.Text.Json to serialize a collection of Employee objects. +1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric). +2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter. +3. Create a List with sample data (one Car, one Motorcycle, one Bicycle). +4. Serialize the list to a JSON string. +5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic. +Show example output. ``` Review Copilot's suggestions before applying them. @@ -127,4 +132,4 @@ For more information about GitHub Copilot, see GitHub's [FAQs](https://github.co ## See also - [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) -- [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview) +- [GitHub Copilot in Visual Studio Code](https://code.visualstudio.com/docs/copilot/overview) From 83d447378378f83f15cd726a9edf2945296969e5 Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 10:44:02 -0800 Subject: [PATCH 2/8] Fixes --- .../serialization/system-text-json/how-to.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index 67230ad8ede11..e990d681a6d60 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -110,19 +110,19 @@ Starting in .NET 9, you can also customize the indent character and size using < ## Use AI to serialize nested and polymorphic types -You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON `System.Text.Json`. You can use it for nested and polymorphic objects. +You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON using `System.Text.Json`. You can use it for nested and polymorphic objects. For example, you can prompt Copilot to generate code that handles nested and polymorphic types, which is useful for creating examples or test data for hierarchical object structures. -Here's an example prompt you can use in Visual Studio Code Copilot Chat to migrate a solution. +Here's an example prompt you can use in Visual Studio Code Copilot Chat to generate serialization code. ```copilot-prompt -Generate C# code using System.Text.Json to serialize a collection of Employee objects. -1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric). -2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter. -3. Create a List with sample data (one Car, one Motorcycle, one Bicycle). -4. Serialize the list to a JSON string. -5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic. -Show example output. +Generate C# code using System.Text.Json to serialize a collection of Employee objects +1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric) +2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter +3. Create a List with sample data (one Car, one Motorcycle, one Bicycle) +4. Serialize the list to a JSON string +5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic +Show example output ``` Review Copilot's suggestions before applying them. From 84d8e8913a6373e1774273b41449b95f79144a05 Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 10:45:50 -0800 Subject: [PATCH 3/8] Fix anchor links --- docs/standard/serialization/system-text-json/how-to.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index e990d681a6d60..9a763445ce434 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -21,7 +21,7 @@ adobe-target: true This article shows how to use the namespace to serialize to JavaScript Object Notation (JSON). If you're porting existing code from `Newtonsoft.Json`, see [How to migrate to `System.Text.Json`](migrate-from-newtonsoft.md). > [!TIP] -> You can use AI assistance to [serialize to JSON](#use-ai-to-serialize-to-json). +> You can use AI assistance to [serialize to JSON](#use-ai-to-serialize-nested-and-polymorphic-types). To write JSON to a string or to a file, call the method. @@ -49,7 +49,7 @@ The preceding examples use type inference for the type being serialized. An over :::code language="csharp" source="snippets/how-to/csharp/SerializeWithGenericParameter.cs" highlight="23"::: :::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="SerializeWithGenericParameter"::: -You can also use AI to generate serialization code for you. For instructions, see the [Use AI](#use-ai-to-serialize-to-json) section in this article. +You can also use AI to generate serialization code for you. For instructions, see the [Use AI](#use-ai-to-serialize-nested-and-polymorphic-types) section in this article. ## Serialization behavior From 04d6f661b5b33655cab2728ec570b7eef5549ece Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 11:46:53 -0800 Subject: [PATCH 4/8] Address feedback --- .../serialization/system-text-json/how-to.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index 9a763445ce434..f1cd3d7089c58 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -110,19 +110,19 @@ Starting in .NET 9, you can also customize the indent character and size using < ## Use AI to serialize nested and polymorphic types -You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON using `System.Text.Json`. You can use it for nested and polymorphic objects. +You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON using `System.Text.Json`. For example, you can prompt Copilot to generate code that handles nested and polymorphic types, which is useful for creating examples or test data for hierarchical object structures. Here's an example prompt you can use in Visual Studio Code Copilot Chat to generate serialization code. ```copilot-prompt Generate C# code using System.Text.Json to serialize a collection of Employee objects -1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric) -2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter -3. Create a List with sample data (one Car, one Motorcycle, one Bicycle) -4. Serialize the list to a JSON string -5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic -Show example output +1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric). +2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter. +3. Create a List with sample data (one Car, one Motorcycle, one Bicycle). +4. Serialize the list to a JSON string. +5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic. +Show example output. ``` Review Copilot's suggestions before applying them. From 6a41aee5564af0745318b18ace464b15912ea94e Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 13:48:51 -0800 Subject: [PATCH 5/8] Revise AI section per feedback --- .../serialization/system-text-json/how-to.md | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index f1cd3d7089c58..52ba9adaf4c77 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -21,7 +21,7 @@ adobe-target: true This article shows how to use the namespace to serialize to JavaScript Object Notation (JSON). If you're porting existing code from `Newtonsoft.Json`, see [How to migrate to `System.Text.Json`](migrate-from-newtonsoft.md). > [!TIP] -> You can use AI assistance to [serialize to JSON](#use-ai-to-serialize-nested-and-polymorphic-types). +> You can use AI assistance to [serialize to JSON](#use-ai-to-serialize-to-json). To write JSON to a string or to a file, call the method. @@ -49,7 +49,7 @@ The preceding examples use type inference for the type being serialized. An over :::code language="csharp" source="snippets/how-to/csharp/SerializeWithGenericParameter.cs" highlight="23"::: :::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="SerializeWithGenericParameter"::: -You can also use AI to generate serialization code for you. For instructions, see the [Use AI](#use-ai-to-serialize-nested-and-polymorphic-types) section in this article. +You can also use AI to generate serialization code for you. For instructions, see the [Use AI](#use-ai-to-serialize-to-json) section in this article. ## Serialization behavior @@ -108,20 +108,17 @@ Starting in .NET 9, you can also customize the indent character and size using < > [!TIP] > If you use `JsonSerializerOptions` repeatedly with the same options, don't create a new `JsonSerializerOptions` instance each time you use it. Reuse the same instance for every call. For more information, see [Reuse JsonSerializerOptions instances](configure-options.md#reuse-jsonserializeroptions-instances). -## Use AI to serialize nested and polymorphic types +## Use AI to serialize to JSON -You can use AI tools, such as GitHub Copilot, to generate code that serializes objects to JSON using `System.Text.Json`. -For example, you can prompt Copilot to generate code that handles nested and polymorphic types, which is useful for creating examples or test data for hierarchical object structures. +You can use AI tools, such as GitHub Copilot, to generate code that uses `System.Text.Json` to serialize to JSON. You can customize the prompt to fit your object fields and serialization needs. Here's an example prompt you can use in Visual Studio Code Copilot Chat to generate serialization code. ```copilot-prompt -Generate C# code using System.Text.Json to serialize a collection of Employee objects -1. Define classes: Employee (with Id, Name, Department, Vehicle), Department (Name, Code), Vehicle (base), Car (Doors), Motorcycle (HasSidecar), Bicycle (IsElectric). -2. Use a custom JsonConverter with a $type discriminator in the output. The custom converter must implement the correct method signatures for JsonConverter. -3. Create a List with sample data (one Car, one Motorcycle, one Bicycle). -4. Serialize the list to a JSON string. -5. Provide the complete, compiling code including all using directives, class definitions, converter implementations, and serialization logic. +I have a variable named weatherForecast of type WeatherForecast. +Before serializing, generate C# code that adds a new property "FeelsLikeFahrenheit" to this object, calculated from TemperatureCelsius. +Then serialize the updated object to an indented JSON string using System.Text.Json and write the result to a file named "output.json". +Ensure the code includes all necessary using directives and compiles without errors. Show example output. ``` From 1b86ca2333c8e6334a700c5ad1602370214f42d5 Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 15:42:02 -0800 Subject: [PATCH 6/8] Revise Updated prompt to simplify serialization instructions and ensure proper formatting. --- docs/standard/serialization/system-text-json/how-to.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index 52ba9adaf4c77..c88ff76853b6b 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -116,10 +116,8 @@ Here's an example prompt you can use in Visual Studio Code Copilot Chat to gener ```copilot-prompt I have a variable named weatherForecast of type WeatherForecast. -Before serializing, generate C# code that adds a new property "FeelsLikeFahrenheit" to this object, calculated from TemperatureCelsius. -Then serialize the updated object to an indented JSON string using System.Text.Json and write the result to a file named "output.json". +Serialize the variable using System.Text.Json and write the result directly to a file stream called "output.json" with the JSON indented for pretty formatting. Ensure the code includes all necessary using directives and compiles without errors. -Show example output. ``` Review Copilot's suggestions before applying them. From 2f669c9de26a972db9bb0b2abb80ba11a2c15d2a Mon Sep 17 00:00:00 2001 From: Megha Anand <88059806+anandmeg@users.noreply.github.com> Date: Fri, 14 Nov 2025 20:53:49 -0800 Subject: [PATCH 7/8] Apply suggestion from @adegeo Co-authored-by: Andy (Steve) De George <67293991+adegeo@users.noreply.github.com> --- docs/standard/serialization/system-text-json/how-to.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index c88ff76853b6b..f45ebf60f9767 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -116,7 +116,7 @@ Here's an example prompt you can use in Visual Studio Code Copilot Chat to gener ```copilot-prompt I have a variable named weatherForecast of type WeatherForecast. -Serialize the variable using System.Text.Json and write the result directly to a file stream called "output.json" with the JSON indented for pretty formatting. +Serialize the variable using System.Text.Json and write the result directly to a file named "output.json" with the JSON indented for pretty formatting. Ensure the code includes all necessary using directives and compiles without errors. ``` From 2eed2262664e9c2890056336313e48d84000a6ee Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 18 Nov 2025 17:37:30 -0800 Subject: [PATCH 8/8] Update docs/standard/serialization/system-text-json/how-to.md --- docs/standard/serialization/system-text-json/how-to.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/serialization/system-text-json/how-to.md b/docs/standard/serialization/system-text-json/how-to.md index f45ebf60f9767..47cc7ac533523 100644 --- a/docs/standard/serialization/system-text-json/how-to.md +++ b/docs/standard/serialization/system-text-json/how-to.md @@ -112,7 +112,7 @@ Starting in .NET 9, you can also customize the indent character and size using < You can use AI tools, such as GitHub Copilot, to generate code that uses `System.Text.Json` to serialize to JSON. You can customize the prompt to fit your object fields and serialization needs. -Here's an example prompt you can use in Visual Studio Code Copilot Chat to generate serialization code. +Here's an example prompt you can use to generate serialization code: ```copilot-prompt I have a variable named weatherForecast of type WeatherForecast.