From a3ce430025e3799809d68b858ca69c6c51668d2b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Dec 2025 23:27:53 +0000 Subject: [PATCH 1/6] Initial plan From fb918d934a1d734bc882728ec9a19d664bdb78aa Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Dec 2025 23:39:09 +0000 Subject: [PATCH 2/6] Add deserialization examples for child collections and nested objects Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../system-text-json/deserialization.md | 4 +- .../how-to/csharp/DeserializeExtra.cs | 31 ++++++++ .../snippets/how-to/vb/DeserializeExtra.vb | 73 +++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb diff --git a/docs/standard/serialization/system-text-json/deserialization.md b/docs/standard/serialization/system-text-json/deserialization.md index cb1cf82a10a0b..01331afa04158 100644 --- a/docs/standard/serialization/system-text-json/deserialization.md +++ b/docs/standard/serialization/system-text-json/deserialization.md @@ -28,10 +28,10 @@ Any JSON properties that aren't represented in your class are ignored [by defaul ## Examples -The following example shows how to deserialize a JSON string: +The following example shows how to deserialize a JSON string that contains collections and nested objects: :::code language="csharp" source="snippets/how-to/csharp/DeserializeExtra.cs" highlight="54-55"::: -:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="Deserialize"::: +:::code language="vb" source="snippets/how-to/vb/DeserializeExtra.vb"::: To deserialize from a file by using synchronous code, read the file into a string, as shown in the following example: diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs index 779808eb11294..4aa793602aa8b 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs @@ -57,6 +57,30 @@ public static void Main() Console.WriteLine($"Date: {weatherForecast?.Date}"); Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}"); Console.WriteLine($"Summary: {weatherForecast?.Summary}"); + + if (weatherForecast?.DatesAvailable != null) + { + foreach (DateTimeOffset dateTimeOffset in weatherForecast.DatesAvailable) + { + Console.WriteLine($"DateAvailable: {dateTimeOffset}"); + } + } + + if (weatherForecast?.TemperatureRanges != null) + { + foreach (KeyValuePair temperatureRange in weatherForecast.TemperatureRanges) + { + Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}"); + } + } + + if (weatherForecast?.SummaryWords != null) + { + foreach (string summaryWord in weatherForecast.SummaryWords) + { + Console.WriteLine($"SummaryWord: {summaryWord}"); + } + } } } } @@ -64,3 +88,10 @@ public static void Main() //Date: 8/1/2019 12:00:00 AM -07:00 //TemperatureCelsius: 25 //Summary: Hot +//DateAvailable: 8/1/2019 12:00:00 AM -07:00 +//DateAvailable: 8/2/2019 12:00:00 AM -07:00 +//TemperatureRange: Cold, -10 - 20 +//TemperatureRange: Hot, 20 - 60 +//SummaryWord: Cool +//SummaryWord: Windy +//SummaryWord: Humid diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb new file mode 100644 index 0000000000000..8415dd2cffd55 --- /dev/null +++ b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb @@ -0,0 +1,73 @@ +Imports System.Text.Json + +Namespace SystemTextJsonSamples + + Public NotInheritable Class DeserializeExtra + + Public Shared Sub Run() + Dim jsonString As String = + "{ + ""Date"": ""2019-08-01T00:00:00-07:00"", + ""TemperatureCelsius"": 25, + ""Summary"": ""Hot"", + ""DatesAvailable"": [ + ""2019-08-01T00:00:00-07:00"", + ""2019-08-02T00:00:00-07:00"" + ], + ""TemperatureRanges"": { + ""Cold"": { + ""High"": 20, + ""Low"": -10 + }, + ""Hot"": { + ""High"": 60, + ""Low"": 20 + } + }, + ""SummaryWords"": [ + ""Cool"", + ""Windy"", + ""Humid"" + ] + }" + + Dim weatherForecast As WeatherForecastWithPOCOs = + JsonSerializer.Deserialize(Of WeatherForecastWithPOCOs)(jsonString) + + Console.WriteLine($"Date: {weatherForecast?.Date}") + Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}") + Console.WriteLine($"Summary: {weatherForecast?.Summary}") + + If weatherForecast?.DatesAvailable IsNot Nothing Then + For Each dateTimeOffset As DateTimeOffset In weatherForecast.DatesAvailable + Console.WriteLine($"DateAvailable: {dateTimeOffset}") + Next + End If + + If weatherForecast?.TemperatureRanges IsNot Nothing Then + For Each temperatureRange As KeyValuePair(Of String, HighLowTemps) In weatherForecast.TemperatureRanges + Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}") + Next + End If + + If weatherForecast?.SummaryWords IsNot Nothing Then + For Each summaryWord As String In weatherForecast.SummaryWords + Console.WriteLine($"SummaryWord: {summaryWord}") + Next + End If + End Sub + + End Class + +End Namespace +' output: +'Date: 8/1/2019 12:00:00 AM -07:00 +'TemperatureCelsius: 25 +'Summary: Hot +'DateAvailable: 8/1/2019 12:00:00 AM -07:00 +'DateAvailable: 8/2/2019 12:00:00 AM -07:00 +'TemperatureRange: Cold, -10 - 20 +'TemperatureRange: Hot, 20 - 60 +'SummaryWord: Cool +'SummaryWord: Windy +'SummaryWord: Humid From 3ed1f8bdf78cded1451b96abc9af34b4a64cce43 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Dec 2025 23:41:51 +0000 Subject: [PATCH 3/6] Fix VB namespace and class naming to match C# example structure Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../snippets/how-to/vb/DeserializeExtra.vb | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb index 8415dd2cffd55..833f2a4a82157 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb +++ b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb @@ -1,8 +1,23 @@ Imports System.Text.Json -Namespace SystemTextJsonSamples +Namespace DeserializeExtra - Public NotInheritable Class DeserializeExtra + Public Class WeatherForecast + Public Property [Date] As DateTimeOffset + Public Property TemperatureCelsius As Integer + Public Property Summary As String + Public SummaryField As String + Public Property DatesAvailable As IList(Of DateTimeOffset) + Public Property TemperatureRanges As Dictionary(Of String, HighLowTemps) + Public Property SummaryWords As String() + End Class + + Public Class HighLowTemps + Public Property High As Integer + Public Property Low As Integer + End Class + + Public NotInheritable Class Program Public Shared Sub Run() Dim jsonString As String = @@ -31,8 +46,8 @@ Namespace SystemTextJsonSamples ] }" - Dim weatherForecast As WeatherForecastWithPOCOs = - JsonSerializer.Deserialize(Of WeatherForecastWithPOCOs)(jsonString) + Dim weatherForecast As WeatherForecast = + JsonSerializer.Deserialize(Of WeatherForecast)(jsonString) Console.WriteLine($"Date: {weatherForecast?.Date}") Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}") From 064eddc11f59728f9b393ddb0a56fbc051293e14 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 4 Dec 2025 16:12:49 -0800 Subject: [PATCH 4/6] human edits --- .../system-text-json/deserialization.md | 2 +- .../how-to/csharp/DeserializeExtra.cs | 27 ++++++++++--------- .../snippets/how-to/csharp/Program.cs | 4 +-- .../snippets/how-to/csharp/SerializeExtra.cs | 8 +++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/docs/standard/serialization/system-text-json/deserialization.md b/docs/standard/serialization/system-text-json/deserialization.md index 01331afa04158..5c04c52f08f4c 100644 --- a/docs/standard/serialization/system-text-json/deserialization.md +++ b/docs/standard/serialization/system-text-json/deserialization.md @@ -12,7 +12,6 @@ helpviewer_keywords: - "deserialization" ms.topic: concept-article ms.custom: copilot-scenario-highlight -#customer intent: As a developer, I want to learn how to use System.Text.Json to deserialize JSON data. --- # How to read JSON as .NET objects (deserialize) @@ -105,5 +104,6 @@ For more information about GitHub Copilot, see GitHub's [FAQs](https://github.co ## See also +- [How to deserialize to an interface](https://gist.github.com/tonysneed/5e7988516b081d454cde95b5d729e1af) - [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) - [GitHub Copilot in Visual Studio Code](https://code.visualstudio.com/docs/copilot/overview) diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs index 4aa793602aa8b..7cbdfae51be76 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs @@ -70,7 +70,7 @@ public static void Main() { foreach (KeyValuePair temperatureRange in weatherForecast.TemperatureRanges) { - Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}"); + Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}"); } } @@ -84,14 +84,17 @@ public static void Main() } } } -// output: -//Date: 8/1/2019 12:00:00 AM -07:00 -//TemperatureCelsius: 25 -//Summary: Hot -//DateAvailable: 8/1/2019 12:00:00 AM -07:00 -//DateAvailable: 8/2/2019 12:00:00 AM -07:00 -//TemperatureRange: Cold, -10 - 20 -//TemperatureRange: Hot, 20 - 60 -//SummaryWord: Cool -//SummaryWord: Windy -//SummaryWord: Humid + +/* Output: + * + * Date: 8/1/2019 12:00:00 AM -07:00 + * TemperatureCelsius: 25 + * Summary: Hot + * DateAvailable: 8/1/2019 12:00:00 AM -07:00 + * DateAvailable: 8/2/2019 12:00:00 AM -07:00 + * TemperatureRange: Cold is -10 to 20 + * TemperatureRange: Hot is 20 to 60 + * SummaryWord: Cool + * SummaryWord: Windy + * SummaryWord: Humid + * */ diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs index 7468bca6174ff..d268a486ca7a9 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs @@ -13,7 +13,7 @@ static async Task Main(string[] args) //SerializeBasic.Program.Main(); //SerializeWithGenericParameter.Program.Main(); //SerializeWriteIndented.Program.Main(); - //SerializeExtra.Program.Main(); + SerializeExtra.Program.Main(); //DeserializeExtra.Program.Main(); //Console.WriteLine("\n============================= Roundtrip to UTF-8 byte array\n"); @@ -45,7 +45,7 @@ static async Task Main(string[] args) //Console.WriteLine("\n============================= Roundtrip enum as string\n"); //RoundtripEnumAsString.Run(); - SerializeEnumCustomName.Run(); + //SerializeEnumCustomName.Run(); //Console.WriteLine("\n============================= Roundtrip enum using JsonConverterAttribute\n"); //RoundtripEnumUsingConverterAttribute.Run(); diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/SerializeExtra.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/SerializeExtra.cs index a6c597342744b..6932dfc3cd017 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/SerializeExtra.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/SerializeExtra.cs @@ -29,14 +29,13 @@ public static void Main() TemperatureCelsius = 25, Summary = "Hot", SummaryField = "Hot", - DatesAvailable = new List() - { DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") }, + DatesAvailable = [DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02")], TemperatureRanges = new Dictionary { ["Cold"] = new HighLowTemps { High = 20, Low = -10 }, ["Hot"] = new HighLowTemps { High = 60 , Low = 20 } }, - SummaryWords = new[] { "Cool", "Windy", "Humid" } + SummaryWords = ["Cool", "Windy", "Humid"] }; var options = new JsonSerializerOptions { WriteIndented = true }; @@ -46,7 +45,8 @@ public static void Main() } } } -// output: + +// Output: //{ // "Date": "2019-08-01T00:00:00-07:00", // "TemperatureCelsius": 25, From 3ab56e706529df4e72be9488ef79d63894e3e974 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 4 Dec 2025 16:17:09 -0800 Subject: [PATCH 5/6] match vb example --- .../snippets/how-to/vb/DeserializeExtra.vb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb index 833f2a4a82157..1c84064bd82ed 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb +++ b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb @@ -61,7 +61,7 @@ Namespace DeserializeExtra If weatherForecast?.TemperatureRanges IsNot Nothing Then For Each temperatureRange As KeyValuePair(Of String, HighLowTemps) In weatherForecast.TemperatureRanges - Console.WriteLine($"TemperatureRange: {temperatureRange.Key}, {temperatureRange.Value.Low} - {temperatureRange.Value.High}") + Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}") Next End If @@ -75,14 +75,16 @@ Namespace DeserializeExtra End Class End Namespace -' output: + +' Output: +' 'Date: 8/1/2019 12:00:00 AM -07:00 'TemperatureCelsius: 25 'Summary: Hot 'DateAvailable: 8/1/2019 12:00:00 AM -07:00 'DateAvailable: 8/2/2019 12:00:00 AM -07:00 -'TemperatureRange: Cold, -10 - 20 -'TemperatureRange: Hot, 20 - 60 +'TemperatureRange: Cold is -10 to 20 +'TemperatureRange: Hot is 20 to 60 'SummaryWord: Cool 'SummaryWord: Windy 'SummaryWord: Humid From 919c26309aff6e5c7724caa4e13395998a7fd199 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 4 Dec 2025 16:35:26 -0800 Subject: [PATCH 6/6] Update docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs --- .../system-text-json/snippets/how-to/csharp/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs index d268a486ca7a9..ef787b147e4f4 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs @@ -13,7 +13,7 @@ static async Task Main(string[] args) //SerializeBasic.Program.Main(); //SerializeWithGenericParameter.Program.Main(); //SerializeWriteIndented.Program.Main(); - SerializeExtra.Program.Main(); + //SerializeExtra.Program.Main(); //DeserializeExtra.Program.Main(); //Console.WriteLine("\n============================= Roundtrip to UTF-8 byte array\n");