public static void DisplayPropertyValues(this WeatherForecastWithDictionary wf) { Utilities.DisplayPropertyValues(wf); Console.Write($"TemperatureRanges:\n"); foreach (KeyValuePair <string, int> kvp in wf.TemperatureRanges) { Console.Write($" {kvp.Key}, {kvp.Value}\n"); } Console.WriteLine(); }
public static WeatherForecastWithDictionary CreateWeatherForecastWithDictionary() { var weatherForecast = new WeatherForecastWithDictionary { Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot", TemperatureRanges = new Dictionary<string, int> { { "ColdMinTemp", 20 }, { "HotMinTemp", 40 } } }; return weatherForecast; }
public static void Run() { string jsonString; WeatherForecastWithDictionary weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithDictionary(); weatherForecast.DisplayPropertyValues(); // <Serialize> var options = new JsonSerializerOptions { DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); // </Serialize> Console.WriteLine($"JSON output:\n{jsonString}\n"); // <Deserialize> weatherForecast = JsonSerializer.Deserialize <WeatherForecastWithDictionary>(jsonString) !; // </Deserialize> weatherForecast !.DisplayPropertyValues(); }