private static void SerializeToUtf8(WeatherForecastMin weatherForecast) { PrintTitle("Serialize to UTF8"); PrintPropertyValues(weatherForecast); Console.WriteLine("With generic type parameter:"); byte[] jsonUTF8 = JsonSerializer.SerializeToUtf8Bytes <WeatherForecastMin>(weatherForecast); PrintFormattedJsonOutput(Encoding.UTF8.GetString(jsonUTF8)); Console.WriteLine("With generic type inference:"); jsonUTF8 = JsonSerializer.SerializeToUtf8Bytes((weatherForecast)); PrintFormattedJsonOutput(Encoding.UTF8.GetString(jsonUTF8)); }
private static void WriteIndentedJSON(WeatherForecastMin weatherForecast) { PrintTitle("Write indented JSON"); PrintPropertyValues(weatherForecast); var options = new JsonSerializerOptions { WriteIndented = true, }; string json = JsonSerializer.Serialize(weatherForecast, options); PrintFormattedJsonOutput(json); }
private static void IncludePropertiesOfDerivedClass(WeatherForecastDerived weatherForecast2b) { string json; WeatherForecastMin weatherForecast = weatherForecast2b; PrintTitle("Include properties of a derived class"); PrintPropertyValues(weatherForecast); json = JsonSerializer.Serialize(weatherForecast); PrintFormattedJsonOutput(json); json = JsonSerializer.Serialize <WeatherForecastMin>(weatherForecast); PrintFormattedJsonOutput(json); json = JsonSerializer.Serialize(weatherForecast, weatherForecast.GetType()); PrintFormattedJsonOutput(json); }
private static void ExcludeNullValueProperties(WeatherForecastMin weatherForecast) { string json; PrintTitle("Exclude properties with null value"); weatherForecast.Summary = null; PrintPropertyValues(weatherForecast); var options = new JsonSerializerOptions { IgnoreNullValues = true }; json = JsonSerializer.Serialize(weatherForecast, options); PrintFormattedJsonOutput(json); weatherForecast.Summary = "Hot"; }
private static void CustomNamingPolicy(WeatherForecastMin weatherForecast) { string json; PrintTitle("Custom JSON property naming policy"); PrintPropertyValues(weatherForecast); var options = new JsonSerializerOptions { PropertyNamingPolicy = new UpperCaseNamingPolicy() }; json = JsonSerializer.Serialize(weatherForecast, options); PrintFormattedJsonOutput(json); PrintJsonInput(json); weatherForecast = JsonSerializer.Deserialize <WeatherForecastMin>(json, options); PrintPropertyValues(weatherForecast); }
private static void CamelCasePropertyNames(WeatherForecastMin weatherForecast) { string json; PrintTitle("CamelCase JSON property names"); PrintPropertyValues(weatherForecast); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; json = JsonSerializer.Serialize(weatherForecast, options); PrintFormattedJsonOutput(json); PrintJsonInput(json); weatherForecast = JsonSerializer.Deserialize <WeatherForecastMin>(json, options); PrintPropertyValues(weatherForecast); }
private static void AllowCommentsTrailingCommas() { PrintTitle("Allow comments and trailing commas"); string json = @"{ ""Date"": ""2019-08-01T00:00:00-07:00"", ""TemperatureC"": 25, // Fahrenheit 77 ""Summary"": ""Hot"", /* Zharko */ }"; PrintJsonInput(json); var options = new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true, }; WeatherForecastMin weatherForecast = JsonSerializer.Deserialize <WeatherForecastMin>(json, options); PrintPropertyValues(weatherForecast); }
private static void CaseInsensitive(WeatherForecastMin weatherForecast) { string json; WeatherForecastMin weatherForecast1; weatherForecast1 = weatherForecast; PrintTitle("Case insensitive property name matching"); var optionsUpperCase = new JsonSerializerOptions { PropertyNamingPolicy = new UpperCaseNamingPolicy() }; json = JsonSerializer.Serialize(weatherForecast1, optionsUpperCase); PrintJsonInput(json); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }; weatherForecast = JsonSerializer.Deserialize <WeatherForecastMin>(json, options); PrintPropertyValues(weatherForecast); }
static void Main(string[] args) { // Complex object to illustrate multiple serialization capabilities. var weatherForecast = new WeatherForecast { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot", SummaryField = "Hot", DatesAvailable = new List <DateTimeOffset>() { DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") }, TemperatureRanges2 = new Dictionary <string, int> { { "Cold", 20 }, { "Hot", 40 } }, TemperatureRanges = new Dictionary <string, HighLowTemps> { { "Cold", new HighLowTemps { High = new Temperature { Celsius = 20 }, Low = new Temperature { Celsius = -10 } } }, { "Hot", new HighLowTemps { High = new Temperature { Celsius = 60 }, Low = new Temperature { Celsius = 20 } } } }, SummaryWords = new string[] { "Cool", "Windy", "Humid" } }; // Json version of the preceding object, but includes an element for a field // to illustrate that fields are ignored during deserialization. string weatherForecastJson = @"{ ""Date"": ""2019-08-01T00:00:00-07:00"", ""temperatureC"": 25, ""Summary"": ""Hot"", ""SummaryField"": ""Hot"", ""DatesAvailable"": [ ""2019-08-01T00:00:00-07:00"", ""2019-08-02T00:00:00-07:00"" ], ""TemperatureRanges"": { ""Cold"": { ""High"": { ""Celsius"": 20 }, ""Low"": { ""Celsius"": -10 } }, ""Hot"": { ""High"": { ""Celsius"": 60 }, ""Low"": { ""Celsius"": 20 } } }, ""SummaryWords"": [ ""Cool"", ""Windy"", ""Humid"" ] }"; var weatherForecastMin = new WeatherForecastMin { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot" }; var weatherForecastPptyNameAttr = new WeatherForecastPptyNameAttr { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot and windy", WindSpeed = 35 }; var weatherForecastDerived = new WeatherForecastDerived { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot and windy", WindSpeed = 35 }; var weatherForecastReadOnlyPpty = new WeatherForecastReadOnlyPpty { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot" }; var weatherForecastJsonIgnoreAttr = new WeatherForecastJsonIgnoreAttr { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot", WindSpeed = 77 }; var weatherForecastDictCamel = new WeatherForecastDictCamel { Date = DateTime.Parse("2019-08-01"), TemperatureC = 25, Summary = "Hot", TemperatureRanges = new Dictionary <string, int> { { "Cold", 20 }, { "Hot", 40 } } }; byte[] jsonUtf8 = JsonSerializer.SerializeToUtf8Bytes(weatherForecastMin, new JsonSerializerOptions { WriteIndented = true }); var weatherForecasts = new List <WeatherForecastMin> { weatherForecastMin, new WeatherForecastMin { Date = DateTime.Parse("2019-09-30"), TemperatureC = 10, Summary = "Cold" } }; CamelCaseDictionaryKeys(weatherForecast); Console.ReadLine(); int selection; do { selection = DisplayMenu(); switch (selection) { case 1: SerializeToString(weatherForecast); SerializeToUtf8(weatherForecastMin); break; case 2: DeserializeFromString(weatherForecastJson); break; case 3: WriteIndentedJSON(weatherForecastMin); break; case 4: AllowCommentsTrailingCommas(); break; case 5: SpecifyJsonPropertyNames(weatherForecastPptyNameAttr); CamelCasePropertyNames(weatherForecastMin); CustomNamingPolicy(weatherForecastMin); CamelCaseDictionaryKeys(weatherForecast); break; case 6: ExcludeSelectedProperties(weatherForecastJsonIgnoreAttr); ExcludeReadOnlyProperties(weatherForecastReadOnlyPpty); ExcludeNullValueProperties(weatherForecastMin); break; case 7: CaseInsensitive(weatherForecastMin); break; case 8: IncludePropertiesOfDerivedClass(weatherForecastDerived); break; case 9: UseJsonWriter(); break; case 10: UseJsonReader(jsonUtf8); break; } ; } while (selection != 0); }