public static void Run() { string jsonString = @"{ ""Date"": ""2019-08-01T00:00:00-07:00"", ""temperatureCelsius"": 25, ""Summary"": ""Hot"", ""SummaryField"": ""Hot"", ""DatesAvailable"": [ ""2019-08-01T00:00:00-07:00"", ""2019-08-02T00:00:00-07:00"" ], ""TemperatureRanges"": { ""Cold"": { ""High"": { ""Celsius"": 20 }, ""Low"": { ""Celsius"": -10 } }, ""Hot"": { ""High"": { ""Celsius"": 60 }, ""Low"": { ""Celsius"": 20 } } }, ""SummaryWords"": [ ""Cool"", ""Windy"", ""Humid"" ] }"; Console.WriteLine($"JSON input:\n{jsonString}\n"); // <Deserialize> WeatherForecastWithExtensionData weatherForecast = JsonSerializer.Deserialize <WeatherForecastWithExtensionData>(jsonString); weatherForecast.DisplayPropertyValues(); // </Deserialize> // <Serialize> var serializeOptions = new JsonSerializerOptions { WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions); // </Serialize> Console.WriteLine($"JSON output:\n{jsonString}\n"); }
public static WeatherForecastWithExtensionData CreateWeatherForecastWithExtensionData() { var weatherForecast = new WeatherForecastWithExtensionData { Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot" }; return weatherForecast; }
public static void DisplayPropertyValues(this WeatherForecastWithExtensionData wf) { Console.WriteLine($"Date: {wf.Date}"); Console.WriteLine($"TemperatureCelsius: {wf.TemperatureCelsius}"); Console.WriteLine($"Summary: {wf.Summary}"); Console.WriteLine($"ExtensionData:"); if (wf.ExtensionData != null) { foreach (KeyValuePair <string, object> kvp in wf.ExtensionData) { Console.WriteLine($" {kvp.Key} {kvp.Value}"); } } Console.WriteLine(); }