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(); }
public static WeatherForecastWithPropertyNameAttribute CreateWeatherForecastWithPropertyNameAttribute() { var weatherForecast = new WeatherForecastWithPropertyNameAttribute { Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot", WindSpeed = 35 }; return weatherForecast; }
public static void DisplayPropertyValues(this WeatherForecastWithPropertyNameAttribute wf) { Utilities.DisplayPropertyValues(wf); Console.WriteLine(); }