Add deserialization examples demonstrating child collection iteration
Summary
The deserialization examples showed JSON being deserialized into complex objects with collections and nested types, but only printed top-level properties. This left users uncertain whether child collections (IList<DateTimeOffset>, Dictionary<string, HighLowTemps>, string[]) were actually populated.
Changes:
- C# (
DeserializeExtra.cs): Added iteration code that prints all child collection elements - VB (
DeserializeExtra.vb): Created new file matching C# functionality with equivalent iteration logic - Documentation (
deserialization.md): Updated to reference new VB example and clarified description
Before:
Date: 8/1/2019 12:00:00 AM -07:00
TemperatureCelsius: 25
Summary: Hot
After:
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
Original prompt
This section details on the original issue you should resolve
<issue_title>Example for how to deserialize complex object is missing.</issue_title> <issue_description>
Type of issue
Other (describe below)
Description
The examples show how to serialize a complex object, but they do not show how to deserialize it. They only show that the root object is deserialized. I suggest adding code like the following to show that the child objects are also deserialized:
if (weatherForecast != null) { if (weatherForecast.DatesAvailable != null) { foreach (DateTimeOffset dateTimeOffset in weatherForecast.DatesAvailable) { Console.WriteLine(value: $"DateAvailable: {dateTimeOffset}"); } } if (weatherForecast.TemperatureRanges != null) { foreach (KeyValuePair<string, HighLowTemps> weatherForecastTemperatureRange in weatherForecast.TemperatureRanges) { Console.WriteLine(value: $"TemperatureRange: {weatherForecastTemperatureRange.Key}, {weatherForecastTemperatureRange.Value.Low} - {weatherForecastTemperatureRange.Value.High}"); } } if (weatherForecast.SummaryWords != null) { foreach (string summaryWord in weatherForecast.SummaryWords) { Console.WriteLine(value: $"SummaryWord: {summaryWord}"); } } }Output would then be: Date: 8/1/2019 12:00:00 AM +02:00 TemperatureCelsius: 25 Summary: Hot DateAvailable: 8/1/2019 12:00:00 AM +02:00 DateAvailable: 8/2/2019 12:00:00 AM +02:00 TemperatureRange: Cold, -10 - 20 TemperatureRange: Hot, 20 - 60 SummaryWord: Cool SummaryWord: Windy SummaryWord: Humid
It would also be extremely helpful to have an example on how to deserialize interfaces. E.g. like at https://gist.github.com/tonysneed/5e7988516b081d454cde95b5d729e1af
Page URL
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to
Content source URL
https://github.com/dotnet/docs/blob/live/docs/standard/serialization/system-text-json/how-to.md
Document Version Independent Id
002f9f2a-48d1-0671-a079-b99b9f6bab58
Article author
gewarren
Metadata
- ID: 588ae32f-3429-143f-824c-5ab2d046d24a
- Service: dotnet-fundamentals
Associated WorkItem - 537288</issue_description>
Comments on the Issue (you are @copilot in this section)
@adegeo @gewarren I don't know if this still applies, but I found it while looking into a different PR related to JSON
- Fixes dotnet/docs#34304
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Internal previews
| 📄 File | 🔗 Preview link |
|---|---|
| docs/standard/serialization/system-text-json/deserialization.md | How to read JSON as .NET objects (deserialize) |