Example #1
0
        public static void Run()
        {
            string jsonString;
            WeatherForecastWithEnum weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithEnum();

            weatherForecast.DisplayPropertyValues();

            var options = new JsonSerializerOptions
            {
                WriteIndented = true,
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            Console.WriteLine($"JSON with enum as number:\n{jsonString}\n");

            // <SnippetSerialize>
            options = new JsonSerializerOptions();
            options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
            options.WriteIndented = true;
            jsonString            = JsonSerializer.Serialize(weatherForecast, options);
            // </SnippetSerialize>
            Console.WriteLine($"JSON with enum as string:\n{jsonString}\n");

            // <SnippetDeserialize>
            options = new JsonSerializerOptions();
            options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
            weatherForecast = JsonSerializer.Deserialize <WeatherForecastWithEnum>(jsonString, options);
            // </SnippetDeserialize>
            weatherForecast.DisplayPropertyValues();
        }
Example #2
0
 public static WeatherForecastWithEnum CreateWeatherForecastWithEnum()
 {
     var weatherForecast = new WeatherForecastWithEnum
     {
         Date = DateTime.Parse("2019-08-01"),
         TemperatureCelsius = 25,
         Summary = Summary.Hot
     };
     return weatherForecast;
 }
Example #3
0
 public static void DisplayPropertyValues(this WeatherForecastWithEnum wf)
 {
     Utilities.DisplayPropertyValues(wf);
     Console.WriteLine();
 }