diff --git a/docs/standard/serialization/system-text-json/deserialization.md b/docs/standard/serialization/system-text-json/deserialization.md index cb1cf82a10a0b..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) @@ -28,10 +27,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: @@ -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 779808eb11294..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 @@ -57,10 +57,44 @@ 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} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}"); + } + } + + if (weatherForecast?.SummaryWords != null) + { + foreach (string summaryWord in weatherForecast.SummaryWords) + { + Console.WriteLine($"SummaryWord: {summaryWord}"); + } + } } } } -// output: -//Date: 8/1/2019 12:00:00 AM -07:00 -//TemperatureCelsius: 25 -//Summary: Hot + +/* 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..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 @@ -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, 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..1c84064bd82ed --- /dev/null +++ b/docs/standard/serialization/system-text-json/snippets/how-to/vb/DeserializeExtra.vb @@ -0,0 +1,90 @@ +Imports System.Text.Json + +Namespace 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 = + "{ + ""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 WeatherForecast = + JsonSerializer.Deserialize(Of WeatherForecast)(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} is {temperatureRange.Value.Low} to {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 is -10 to 20 +'TemperatureRange: Hot is 20 to 60 +'SummaryWord: Cool +'SummaryWord: Windy +'SummaryWord: Humid