コード例 #1
0
        public static void Run()
        {
            string jsonString;
            WeatherForecastWithPropertyNameAttribute weatherForecast =
                WeatherForecastFactories.CreateWeatherForecastWithPropertyNameAttribute();

            weatherForecast.DisplayPropertyValues();

            // <Serialize>
            var serializeOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented        = true
            };

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

            // <Deserialize>
            var deserializeOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            weatherForecast =
                JsonSerializer.Deserialize <WeatherForecastWithPropertyNameAttribute>(
                    jsonString, deserializeOptions);
            // </Deserialize>
            weatherForecast.DisplayPropertyValues();
        }
コード例 #2
0
 public static WeatherForecastWithPropertyNameAttribute CreateWeatherForecastWithPropertyNameAttribute()
 {
     var weatherForecast = new WeatherForecastWithPropertyNameAttribute
     {
         Date = DateTime.Parse("2019-08-01"),
         TemperatureCelsius = 25,
         Summary = "Hot",
         WindSpeed = 35
     };
     return weatherForecast;
 }
コード例 #3
0
ファイル: WeatherForecast.cs プロジェクト: hieppdc/samples
 public static void DisplayPropertyValues(this WeatherForecastWithPropertyNameAttribute wf)
 {
     Utilities.DisplayPropertyValues(wf);
     Console.WriteLine();
 }