docs icon indicating copy to clipboard operation
docs copied to clipboard

Example for how to deserialize complex object is missing.

Open MichaelSchreiber opened this issue 2 years ago • 1 comments

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

MichaelSchreiber avatar Feb 28 '23 22:02 MichaelSchreiber