Skip to content

Commit d32acff

Browse files
authored
Add deserialization examples demonstrating child collection iteration (#50338)
1 parent 15c0a2c commit d32acff

File tree

5 files changed

+136
-12
lines changed

5 files changed

+136
-12
lines changed

docs/standard/serialization/system-text-json/deserialization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ helpviewer_keywords:
1212
- "deserialization"
1313
ms.topic: concept-article
1414
ms.custom: copilot-scenario-highlight
15-
#customer intent: As a developer, I want to learn how to use System.Text.Json to deserialize JSON data.
1615
---
1716

1817
# 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
2827

2928
## Examples
3029

31-
The following example shows how to deserialize a JSON string:
30+
The following example shows how to deserialize a JSON string that contains collections and nested objects:
3231

3332
:::code language="csharp" source="snippets/how-to/csharp/DeserializeExtra.cs" highlight="54-55":::
34-
:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="Deserialize":::
33+
:::code language="vb" source="snippets/how-to/vb/DeserializeExtra.vb":::
3534

3635
To deserialize from a file by using synchronous code, read the file into a string, as shown in the following example:
3736

@@ -105,5 +104,6 @@ For more information about GitHub Copilot, see GitHub's [FAQs](https://github.co
105104

106105
## See also
107106

107+
- [How to deserialize to an interface](https://gist.github.com/tonysneed/5e7988516b081d454cde95b5d729e1af)
108108
- [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
109109
- [GitHub Copilot in Visual Studio Code](https://code.visualstudio.com/docs/copilot/overview)

docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,44 @@ public static void Main()
5757
Console.WriteLine($"Date: {weatherForecast?.Date}");
5858
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}");
5959
Console.WriteLine($"Summary: {weatherForecast?.Summary}");
60+
61+
if (weatherForecast?.DatesAvailable != null)
62+
{
63+
foreach (DateTimeOffset dateTimeOffset in weatherForecast.DatesAvailable)
64+
{
65+
Console.WriteLine($"DateAvailable: {dateTimeOffset}");
66+
}
67+
}
68+
69+
if (weatherForecast?.TemperatureRanges != null)
70+
{
71+
foreach (KeyValuePair<string, HighLowTemps> temperatureRange in weatherForecast.TemperatureRanges)
72+
{
73+
Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}");
74+
}
75+
}
76+
77+
if (weatherForecast?.SummaryWords != null)
78+
{
79+
foreach (string summaryWord in weatherForecast.SummaryWords)
80+
{
81+
Console.WriteLine($"SummaryWord: {summaryWord}");
82+
}
83+
}
6084
}
6185
}
6286
}
63-
// output:
64-
//Date: 8/1/2019 12:00:00 AM -07:00
65-
//TemperatureCelsius: 25
66-
//Summary: Hot
87+
88+
/* Output:
89+
*
90+
* Date: 8/1/2019 12:00:00 AM -07:00
91+
* TemperatureCelsius: 25
92+
* Summary: Hot
93+
* DateAvailable: 8/1/2019 12:00:00 AM -07:00
94+
* DateAvailable: 8/2/2019 12:00:00 AM -07:00
95+
* TemperatureRange: Cold is -10 to 20
96+
* TemperatureRange: Hot is 20 to 60
97+
* SummaryWord: Cool
98+
* SummaryWord: Windy
99+
* SummaryWord: Humid
100+
* */

docs/standard/serialization/system-text-json/snippets/how-to/csharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static async Task Main(string[] args)
4545
//Console.WriteLine("\n============================= Roundtrip enum as string\n");
4646
//RoundtripEnumAsString.Run();
4747

48-
SerializeEnumCustomName.Run();
48+
//SerializeEnumCustomName.Run();
4949

5050
//Console.WriteLine("\n============================= Roundtrip enum using JsonConverterAttribute\n");
5151
//RoundtripEnumUsingConverterAttribute.Run();

docs/standard/serialization/system-text-json/snippets/how-to/csharp/SerializeExtra.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ public static void Main()
2929
TemperatureCelsius = 25,
3030
Summary = "Hot",
3131
SummaryField = "Hot",
32-
DatesAvailable = new List<DateTimeOffset>()
33-
{ DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") },
32+
DatesAvailable = [DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02")],
3433
TemperatureRanges = new Dictionary<string, HighLowTemps>
3534
{
3635
["Cold"] = new HighLowTemps { High = 20, Low = -10 },
3736
["Hot"] = new HighLowTemps { High = 60 , Low = 20 }
3837
},
39-
SummaryWords = new[] { "Cool", "Windy", "Humid" }
38+
SummaryWords = ["Cool", "Windy", "Humid"]
4039
};
4140

4241
var options = new JsonSerializerOptions { WriteIndented = true };
@@ -46,7 +45,8 @@ public static void Main()
4645
}
4746
}
4847
}
49-
// output:
48+
49+
// Output:
5050
//{
5151
// "Date": "2019-08-01T00:00:00-07:00",
5252
// "TemperatureCelsius": 25,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Imports System.Text.Json
2+
3+
Namespace DeserializeExtra
4+
5+
Public Class WeatherForecast
6+
Public Property [Date] As DateTimeOffset
7+
Public Property TemperatureCelsius As Integer
8+
Public Property Summary As String
9+
Public SummaryField As String
10+
Public Property DatesAvailable As IList(Of DateTimeOffset)
11+
Public Property TemperatureRanges As Dictionary(Of String, HighLowTemps)
12+
Public Property SummaryWords As String()
13+
End Class
14+
15+
Public Class HighLowTemps
16+
Public Property High As Integer
17+
Public Property Low As Integer
18+
End Class
19+
20+
Public NotInheritable Class Program
21+
22+
Public Shared Sub Run()
23+
Dim jsonString As String =
24+
"{
25+
""Date"": ""2019-08-01T00:00:00-07:00"",
26+
""TemperatureCelsius"": 25,
27+
""Summary"": ""Hot"",
28+
""DatesAvailable"": [
29+
""2019-08-01T00:00:00-07:00"",
30+
""2019-08-02T00:00:00-07:00""
31+
],
32+
""TemperatureRanges"": {
33+
""Cold"": {
34+
""High"": 20,
35+
""Low"": -10
36+
},
37+
""Hot"": {
38+
""High"": 60,
39+
""Low"": 20
40+
}
41+
},
42+
""SummaryWords"": [
43+
""Cool"",
44+
""Windy"",
45+
""Humid""
46+
]
47+
}"
48+
49+
Dim weatherForecast As WeatherForecast =
50+
JsonSerializer.Deserialize(Of WeatherForecast)(jsonString)
51+
52+
Console.WriteLine($"Date: {weatherForecast?.Date}")
53+
Console.WriteLine($"TemperatureCelsius: {weatherForecast?.TemperatureCelsius}")
54+
Console.WriteLine($"Summary: {weatherForecast?.Summary}")
55+
56+
If weatherForecast?.DatesAvailable IsNot Nothing Then
57+
For Each dateTimeOffset As DateTimeOffset In weatherForecast.DatesAvailable
58+
Console.WriteLine($"DateAvailable: {dateTimeOffset}")
59+
Next
60+
End If
61+
62+
If weatherForecast?.TemperatureRanges IsNot Nothing Then
63+
For Each temperatureRange As KeyValuePair(Of String, HighLowTemps) In weatherForecast.TemperatureRanges
64+
Console.WriteLine($"TemperatureRange: {temperatureRange.Key} is {temperatureRange.Value.Low} to {temperatureRange.Value.High}")
65+
Next
66+
End If
67+
68+
If weatherForecast?.SummaryWords IsNot Nothing Then
69+
For Each summaryWord As String In weatherForecast.SummaryWords
70+
Console.WriteLine($"SummaryWord: {summaryWord}")
71+
Next
72+
End If
73+
End Sub
74+
75+
End Class
76+
77+
End Namespace
78+
79+
' Output:
80+
'
81+
'Date: 8/1/2019 12:00:00 AM -07:00
82+
'TemperatureCelsius: 25
83+
'Summary: Hot
84+
'DateAvailable: 8/1/2019 12:00:00 AM -07:00
85+
'DateAvailable: 8/2/2019 12:00:00 AM -07:00
86+
'TemperatureRange: Cold is -10 to 20
87+
'TemperatureRange: Hot is 20 to 60
88+
'SummaryWord: Cool
89+
'SummaryWord: Windy
90+
'SummaryWord: Humid

0 commit comments

Comments
 (0)