示例#1
0
        public static WeatherForecastWithPOCOs CreateWeatherForecastWithPOCOs()
        {
            var weatherForecast = new WeatherForecastWithPOCOs
            {
                Date = DateTime.Parse("2019-08-01"),
                TemperatureCelsius = 25,
                Summary            = "Hot",
                SummaryField       = "Hot",
                DatesAvailable     = new List <DateTimeOffset>()
                {
                    DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02")
                },
                TemperatureRanges = new Dictionary <string, HighLowTemps>
                {
                    { "Cold", new HighLowTemps {
                          High = 20, Low = -10
                      } },
                    { "Hot", new HighLowTemps {
                          High = 60, Low = 20
                      } }
                },
                SummaryWords = new string[] { "Cool", "Windy", "Humid" }
            };

            return(weatherForecast);
        }
示例#2
0
        public static void Run()
        {
            WeatherForecastWithPOCOs weatherForecast =
                WeatherForecastFactories.CreateWeatherForecastWithPOCOs();

            weatherForecast.DisplayPropertyValues();

            // <Serialize>
            string jsonString = JsonSerializer.Serialize(weatherForecast);

            // </Serialize>

            // <SerializeWithGenericParameter>
            jsonString = JsonSerializer.Serialize <WeatherForecastWithPOCOs>(weatherForecast);
            // </SerializeWithGenericParameter>

            Console.WriteLine($"JSON output:\n{jsonString}\n");

            // <SerializePrettyPrint>
            var options = new JsonSerializerOptions
            {
                WriteIndented = true,
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </SerializePrettyPrint>
            Console.WriteLine($"Pretty-printed JSON output:\n{jsonString}\n");

            // <Deserialize>
            weatherForecast = JsonSerializer.Deserialize <WeatherForecastWithPOCOs>(jsonString);
            // </Deserialize>
            weatherForecast.DisplayPropertyValues();
        }
示例#3
0
 public static void DisplayPropertyValues(this WeatherForecastWithPOCOs wf)
 {
     Utilities.DisplayPropertyValues(wf);
     Console.WriteLine($"SummaryField: {wf.SummaryField}");
     Console.WriteLine($"TemperatureRanges:");
     foreach (KeyValuePair <string, HighLowTemps> kvp in wf.TemperatureRanges)
     {
         Console.WriteLine($"  {kvp.Key} {kvp.Value.Low} {kvp.Value.High}");
     }
     Console.WriteLine($"SummaryWords:");
     foreach (string word in wf.SummaryWords)
     {
         Console.WriteLine($"  {word}");
     }
     Console.WriteLine();
 }