diff --git a/snippets/core/system-text-json/csharp/ConvertDictionaryTkeyEnumTValue.cs b/snippets/core/system-text-json/csharp/ConvertDictionaryTkeyEnumTValue.cs index 851694efb56..d783a9cc497 100644 --- a/snippets/core/system-text-json/csharp/ConvertDictionaryTkeyEnumTValue.cs +++ b/snippets/core/system-text-json/csharp/ConvertDictionaryTkeyEnumTValue.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - public class RoundtripDictionaryTkeyEnumTValue + public class ConvertDictionaryTkeyEnumTValue { public static void Run() { diff --git a/snippets/core/system-text-json/csharp/ConvertInferredTypesToObject.cs b/snippets/core/system-text-json/csharp/ConvertInferredTypesToObject.cs index dc5efced269..efa2f7669e4 100644 --- a/snippets/core/system-text-json/csharp/ConvertInferredTypesToObject.cs +++ b/snippets/core/system-text-json/csharp/ConvertInferredTypesToObject.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - public class DeserializeInferredTypesToObject + public class ConvertInferredTypesToObject { public static void Run() { diff --git a/snippets/core/system-text-json/csharp/ConvertPolymorphic.cs b/snippets/core/system-text-json/csharp/ConvertPolymorphic.cs index 4943b888c0d..89dc2332c1c 100644 --- a/snippets/core/system-text-json/csharp/ConvertPolymorphic.cs +++ b/snippets/core/system-text-json/csharp/ConvertPolymorphic.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - class SerializePolymorphic + public class ConvertPolymorphic { public static void Run() { @@ -45,4 +45,4 @@ public static void Run() Console.WriteLine($"JSON output:\n{jsonString}\n"); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/DateTimeOffsetConverter.cs b/snippets/core/system-text-json/csharp/DateTimeOffsetConverter.cs index 507b33f8be0..48c0cec087e 100644 --- a/snippets/core/system-text-json/csharp/DateTimeOffsetConverter.cs +++ b/snippets/core/system-text-json/csharp/DateTimeOffsetConverter.cs @@ -16,9 +16,9 @@ public override DateTimeOffset Read( public override void Write( Utf8JsonWriter writer, - DateTimeOffset value, + DateTimeOffset dateTimeValue, JsonSerializerOptions options) => - writer.WriteStringValue(value.ToString( + writer.WriteStringValue(dateTimeValue.ToString( "MM/dd/yyyy", CultureInfo.InvariantCulture)); } } diff --git a/snippets/core/system-text-json/csharp/DateTimeOffsetNullHandlingConverter.cs b/snippets/core/system-text-json/csharp/DateTimeOffsetNullHandlingConverter.cs index 846f600b6b8..0c3f7dbfc3b 100644 --- a/snippets/core/system-text-json/csharp/DateTimeOffsetNullHandlingConverter.cs +++ b/snippets/core/system-text-json/csharp/DateTimeOffsetNullHandlingConverter.cs @@ -22,10 +22,10 @@ public override DateTimeOffset Read( public override void Write( Utf8JsonWriter writer, - DateTimeOffset value, + DateTimeOffset dateTimeValue, JsonSerializerOptions options) { - writer.WriteStringValue(value); + writer.WriteStringValue(dateTimeValue); } } } diff --git a/snippets/core/system-text-json/csharp/DeserializeInferredTypesToObject.cs b/snippets/core/system-text-json/csharp/DeserializeInferredTypesToObject.cs index dc5efced269..1ed4a934bb3 100644 --- a/snippets/core/system-text-json/csharp/DeserializeInferredTypesToObject.cs +++ b/snippets/core/system-text-json/csharp/DeserializeInferredTypesToObject.cs @@ -10,12 +10,11 @@ public static void Run() string jsonString; // Serialize to create input JSON - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); var serializeOptions = new JsonSerializerOptions { WriteIndented = true }; - serializeOptions.WriteIndented = true; jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions); Console.WriteLine($"JSON input:\n{jsonString}\n"); diff --git a/snippets/core/system-text-json/csharp/DeserializeNullToNonnullableType.cs b/snippets/core/system-text-json/csharp/DeserializeNullToNonnullableType.cs index 5a65982ced2..b4fc36c478e 100644 --- a/snippets/core/system-text-json/csharp/DeserializeNullToNonnullableType.cs +++ b/snippets/core/system-text-json/csharp/DeserializeNullToNonnullableType.cs @@ -12,36 +12,31 @@ public static void Run() string jsonString; var wf = WeatherForecastFactories.CreateWeatherForecast(); - var serializeOptions = new JsonSerializerOptions(); - serializeOptions.WriteIndented = true; - serializeOptions.Converters.Add(new DateTimeOffsetNullHandlingConverter()); - jsonString = JsonSerializer.Serialize(wf, serializeOptions); + var options = new JsonSerializerOptions(); + options.WriteIndented = true; + options.Converters.Add(new DateTimeOffsetNullHandlingConverter()); + jsonString = JsonSerializer.Serialize(wf, options); Console.WriteLine($"JSON with valid Date:\n{jsonString}\n"); - var deserializeOptions = new JsonSerializerOptions(); - deserializeOptions.Converters.Add(new DateTimeOffsetNullHandlingConverter()); - wf = JsonSerializer.Deserialize(jsonString, deserializeOptions); + wf = JsonSerializer.Deserialize(jsonString, options); wf.DisplayPropertyValues(); jsonString = @"{""Date"": null,""TemperatureCelsius"": 25,""Summary"":""Hot""}"; Console.WriteLine($"JSON with null Date:\n{jsonString}\n"); - // The missing-date JSON deserializes with error if the converter isn't used. + // The null-date JSON deserializes with error if the converter isn't used. try { wf = JsonSerializer.Deserialize(jsonString); } - catch (Exception ex) + catch (JsonException ex) { Console.WriteLine($"Exception thrown: {ex.Message}\n"); } Console.WriteLine("Deserialize with converter"); - deserializeOptions = new JsonSerializerOptions(); - deserializeOptions.Converters.Add(new DateTimeOffsetNullHandlingConverter()); - wf = JsonSerializer.Deserialize(jsonString, deserializeOptions); + wf = JsonSerializer.Deserialize(jsonString, options); wf.DisplayPropertyValues(); } } - } diff --git a/snippets/core/system-text-json/csharp/DeserializeRequiredProperty.cs b/snippets/core/system-text-json/csharp/DeserializeRequiredProperty.cs index fdb87b2380e..42e3240daac 100644 --- a/snippets/core/system-text-json/csharp/DeserializeRequiredProperty.cs +++ b/snippets/core/system-text-json/csharp/DeserializeRequiredProperty.cs @@ -12,15 +12,13 @@ public static void Run() string jsonString; var wf = WeatherForecastFactories.CreateWeatherForecast(); - var serializeOptions = new JsonSerializerOptions(); - serializeOptions.WriteIndented = true; - serializeOptions.Converters.Add(new WeatherForecastRequiredPropertyConverter()); - jsonString = JsonSerializer.Serialize(wf, serializeOptions); + var options = new JsonSerializerOptions(); + options.WriteIndented = true; + options.Converters.Add(new WeatherForecastRequiredPropertyConverter()); + jsonString = JsonSerializer.Serialize(wf, options); Console.WriteLine($"JSON with Date:\n{jsonString}\n"); - var deserializeOptions = new JsonSerializerOptions(); - deserializeOptions.Converters.Add(new WeatherForecastRequiredPropertyConverter()); - wf = JsonSerializer.Deserialize(jsonString, deserializeOptions); + wf = JsonSerializer.Deserialize(jsonString, options); wf.DisplayPropertyValues(); jsonString = @"{""TemperatureCelsius"": 25,""Summary"":""Hot""}"; @@ -34,11 +32,9 @@ public static void Run() Console.WriteLine("Deserialize with converter"); try { - deserializeOptions = new JsonSerializerOptions(); - deserializeOptions.Converters.Add(new WeatherForecastRequiredPropertyConverter()); - wf = JsonSerializer.Deserialize(jsonString, deserializeOptions); + wf = JsonSerializer.Deserialize(jsonString, options); } - catch (Exception ex) + catch (JsonException ex) { Console.WriteLine($"Exception thrown: {ex.Message}\n"); } @@ -46,5 +42,4 @@ public static void Run() wf.DisplayPropertyValues(); } } - } diff --git a/snippets/core/system-text-json/csharp/DictionaryTKeyEnumTValueConverter.cs b/snippets/core/system-text-json/csharp/DictionaryTKeyEnumTValueConverter.cs index 2a5e53d6102..abf9b738647 100644 --- a/snippets/core/system-text-json/csharp/DictionaryTKeyEnumTValueConverter.cs +++ b/snippets/core/system-text-json/csharp/DictionaryTKeyEnumTValueConverter.cs @@ -69,13 +69,13 @@ public override Dictionary Read( throw new JsonException(); } - Dictionary value = new Dictionary(); + Dictionary dictionary = new Dictionary(); while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { - return value; + return dictionary; } // Get the key. @@ -107,7 +107,7 @@ public override Dictionary Read( } // Add to dictionary. - value.Add(key, v); + dictionary.Add(key, v); } throw new JsonException(); @@ -115,12 +115,12 @@ public override Dictionary Read( public override void Write( Utf8JsonWriter writer, - Dictionary value, + Dictionary dictionary, JsonSerializerOptions options) { writer.WriteStartObject(); - foreach (KeyValuePair kvp in value) + foreach (KeyValuePair kvp in dictionary) { writer.WritePropertyName(kvp.Key.ToString()); diff --git a/snippets/core/system-text-json/csharp/ImmutablePoint.cs b/snippets/core/system-text-json/csharp/ImmutablePoint.cs index 9a47ed7793d..3c60f42c000 100644 --- a/snippets/core/system-text-json/csharp/ImmutablePoint.cs +++ b/snippets/core/system-text-json/csharp/ImmutablePoint.cs @@ -1,19 +1,16 @@ namespace SystemTextJsonSamples { // - public struct ImmutablePoint + public readonly struct ImmutablePoint { - private readonly int _x; - private readonly int _y; - public ImmutablePoint(int x, int y) { - _x = x; - _y = y; + X = x; + Y = y; } - public int X { get { return _x; } } - public int Y { get { return _y; } } + public int X { get; } + public int Y { get; } } // } diff --git a/snippets/core/system-text-json/csharp/ImmutablePointConverter.cs b/snippets/core/system-text-json/csharp/ImmutablePointConverter.cs index a45d657e790..a3959df3ef1 100644 --- a/snippets/core/system-text-json/csharp/ImmutablePointConverter.cs +++ b/snippets/core/system-text-json/csharp/ImmutablePointConverter.cs @@ -1,14 +1,28 @@ using System; +using System.Diagnostics; using System.Text.Json; using System.Text.Json.Serialization; namespace SystemTextJsonSamples { public class ImmutablePointConverter : JsonConverter - { - private const string XName = "X"; - private const string YName = "Y"; + private readonly JsonEncodedText XName = JsonEncodedText.Encode("X"); + private readonly JsonEncodedText YName = JsonEncodedText.Encode("Y"); + + private readonly JsonConverter _intConverter; + + public ImmutablePointConverter(JsonSerializerOptions options) + { + if (options?.GetConverter(typeof(int)) is JsonConverter intConverter) + { + _intConverter = intConverter; + } + else + { + throw new InvalidOperationException(); + } + } public override ImmutablePoint Read( ref Utf8JsonReader reader, @@ -33,13 +47,12 @@ public override ImmutablePoint Read( throw new JsonException(); } - string propertyName = reader.GetString(); - if (propertyName == XName) + if (reader.ValueTextEquals(XName.EncodedUtf8Bytes)) { x = ReadProperty(ref reader, options); xSet = true; } - else if (propertyName == YName) + else if (reader.ValueTextEquals(YName.EncodedUtf8Bytes)) { y = ReadProperty(ref reader, options); ySet = true; @@ -56,23 +69,15 @@ public override ImmutablePoint Read( throw new JsonException(); } - propertyName = reader.GetString(); - if (propertyName == XName) - { - x = ReadProperty(ref reader, options); - xSet = true; - } - else if (propertyName == YName) + if (xSet && reader.ValueTextEquals(YName.EncodedUtf8Bytes)) { y = ReadProperty(ref reader, options); - ySet = true; } - else + else if (ySet && reader.ValueTextEquals(XName.EncodedUtf8Bytes)) { - throw new JsonException(); + x = ReadProperty(ref reader, options); } - - if (!xSet || !ySet) + else { throw new JsonException(); } @@ -87,26 +92,29 @@ public override ImmutablePoint Read( return new ImmutablePoint(x, y); } - public int ReadProperty(ref Utf8JsonReader reader, JsonSerializerOptions options) + private int ReadProperty(ref Utf8JsonReader reader, JsonSerializerOptions options) { - if (options?.GetConverter(typeof(int)) is JsonConverter intConverter) - { - reader.Read(); - return intConverter.Read(ref reader, typeof(int), options); - } - else - { - throw new JsonException(); - } + Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); + + reader.Read(); + return _intConverter.Read(ref reader, typeof(int), options); + } + + private void WriteProperty(Utf8JsonWriter writer, JsonEncodedText name, int intValue, JsonSerializerOptions options) + { + writer.WritePropertyName(name); + _intConverter.Write(writer, intValue, options); } public override void Write( Utf8JsonWriter writer, - ImmutablePoint value, + ImmutablePoint point, JsonSerializerOptions options) { - // Don't pass in options when recursively calling Serialize. - JsonSerializer.Serialize(writer, value); + writer.WriteStartObject(); + WriteProperty(writer, XName, point.X, options); + WriteProperty(writer, YName, point.Y, options); + writer.WriteEndObject(); } } } diff --git a/snippets/core/system-text-json/csharp/JsonDocumentDataAccess.cs b/snippets/core/system-text-json/csharp/JsonDocumentDataAccess.cs index d04a3c3e1f3..a029b383ecb 100644 --- a/snippets/core/system-text-json/csharp/JsonDocumentDataAccess.cs +++ b/snippets/core/system-text-json/csharp/JsonDocumentDataAccess.cs @@ -4,7 +4,7 @@ namespace SystemTextJsonSamples { - class JsonDocumentDataAccess + public class JsonDocumentDataAccess { public static void Run() { @@ -73,6 +73,5 @@ private static void AverageGrades_Alternative(string jsonString) // } - } } diff --git a/snippets/core/system-text-json/csharp/JsonDocumentWriteJson.cs b/snippets/core/system-text-json/csharp/JsonDocumentWriteJson.cs index 8ea3c17cd4c..5870992f40f 100644 --- a/snippets/core/system-text-json/csharp/JsonDocumentWriteJson.cs +++ b/snippets/core/system-text-json/csharp/JsonDocumentWriteJson.cs @@ -4,7 +4,7 @@ namespace SystemTextJsonSamples { - class JsonDocumentWriteJson + public class JsonDocumentWriteJson { public static void Run() { diff --git a/snippets/core/system-text-json/csharp/LongToStringConverter.cs b/snippets/core/system-text-json/csharp/LongToStringConverter.cs index e671104602c..59846a386ba 100644 --- a/snippets/core/system-text-json/csharp/LongToStringConverter.cs +++ b/snippets/core/system-text-json/csharp/LongToStringConverter.cs @@ -23,9 +23,9 @@ public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOp return reader.GetInt64(); } - public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, long longValue, JsonSerializerOptions options) { - writer.WriteStringValue(value.ToString()); + writer.WriteStringValue(longValue.ToString()); } } } diff --git a/snippets/core/system-text-json/csharp/ObjectToInferredTypesConverter.cs b/snippets/core/system-text-json/csharp/ObjectToInferredTypesConverter.cs index 194dc67f394..8ff01afd3d7 100644 --- a/snippets/core/system-text-json/csharp/ObjectToInferredTypesConverter.cs +++ b/snippets/core/system-text-json/csharp/ObjectToInferredTypesConverter.cs @@ -50,7 +50,7 @@ public override object Read( public override void Write( Utf8JsonWriter writer, - object value, + object objectToWrite, JsonSerializerOptions options) => throw new InvalidOperationException("Should not get here."); } diff --git a/snippets/core/system-text-json/csharp/PersonConverterWithTypeDiscriminator.cs b/snippets/core/system-text-json/csharp/PersonConverterWithTypeDiscriminator.cs index d41d22a2f97..a3297a68c4a 100644 --- a/snippets/core/system-text-json/csharp/PersonConverterWithTypeDiscriminator.cs +++ b/snippets/core/system-text-json/csharp/PersonConverterWithTypeDiscriminator.cs @@ -40,27 +40,19 @@ public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS throw new JsonException(); } - Person value; TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); - switch (typeDiscriminator) + Person person = typeDiscriminator switch { - case TypeDiscriminator.Customer: - value = new Customer(); - break; - - case TypeDiscriminator.Employee: - value = new Employee(); - break; - - default: - throw new JsonException(); - } + TypeDiscriminator.Customer => new Customer(), + TypeDiscriminator.Employee => new Employee(), + _ => throw new JsonException() + }; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { - return value; + return person; } if (reader.TokenType == JsonTokenType.PropertyName) @@ -71,15 +63,15 @@ public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS { case "CreditLimit": decimal creditLimit = reader.GetDecimal(); - ((Customer)value).CreditLimit = creditLimit; + ((Customer)person).CreditLimit = creditLimit; break; case "OfficeNumber": string officeNumber = reader.GetString(); - ((Employee)value).OfficeNumber = officeNumber; + ((Employee)person).OfficeNumber = officeNumber; break; case "Name": string name = reader.GetString(); - value.Name = name; + person.Name = name; break; } } @@ -88,22 +80,22 @@ public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS throw new JsonException(); } - public override void Write(Utf8JsonWriter writer, Person value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, Person person, JsonSerializerOptions options) { writer.WriteStartObject(); - if (value is Customer customer) + if (person is Customer customer) { writer.WriteNumber("TypeDiscriminator", (int)TypeDiscriminator.Customer); writer.WriteNumber("CreditLimit", customer.CreditLimit); } - else if (value is Employee employee) + else if (person is Employee employee) { writer.WriteNumber("TypeDiscriminator", (int)TypeDiscriminator.Employee); writer.WriteString("OfficeNumber", employee.OfficeNumber); } - writer.WriteString("Name", value.Name); + writer.WriteString("Name", person.Name); writer.WriteEndObject(); } diff --git a/snippets/core/system-text-json/csharp/Program.cs b/snippets/core/system-text-json/csharp/Program.cs index 27d4e739c7f..9e8b183dc59 100644 --- a/snippets/core/system-text-json/csharp/Program.cs +++ b/snippets/core/system-text-json/csharp/Program.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - class Program + public class Program { static async Task Main(string[] args) { diff --git a/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnProperty.cs b/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnProperty.cs index d3787728afe..ac426b8b1d1 100644 --- a/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnProperty.cs +++ b/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnProperty.cs @@ -11,7 +11,7 @@ public class RegisterConverterWithAttributeOnProperty public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithConverterAttribute(); + WeatherForecastWithConverterAttribute weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithConverterAttribute(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnType.cs b/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnType.cs index a65581c50e9..95443beb26e 100644 --- a/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnType.cs +++ b/snippets/core/system-text-json/csharp/RegisterConverterWithAttributeOnType.cs @@ -8,7 +8,7 @@ class RegisterConverterWithAttributeOnType public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithTemperatureStruct(); + WeatherForecastWithTemperatureStruct weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithTemperatureStruct(); weatherForecast.DisplayPropertyValues(); // @@ -26,4 +26,4 @@ public static void Run() // } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/RegisterConverterWithConvertersCollection.cs b/snippets/core/system-text-json/csharp/RegisterConverterWithConvertersCollection.cs index fdb3e359644..934e3781c07 100644 --- a/snippets/core/system-text-json/csharp/RegisterConverterWithConvertersCollection.cs +++ b/snippets/core/system-text-json/csharp/RegisterConverterWithConvertersCollection.cs @@ -10,7 +10,7 @@ public class RegisterConverterWithConverterscollection public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/RoundtripCallbacks.cs b/snippets/core/system-text-json/csharp/RoundtripCallbacks.cs index 8e06a2d93f9..6b1d4904926 100644 --- a/snippets/core/system-text-json/csharp/RoundtripCallbacks.cs +++ b/snippets/core/system-text-json/csharp/RoundtripCallbacks.cs @@ -21,7 +21,6 @@ public static void Run() // Console.WriteLine($"JSON output:\n{jsonString}\n"); - //jsonString = @"{""Date"": null,""TemperatureCelsius"": 25,""Summary"":""Hot""}"; // var deserializeOptions = new JsonSerializerOptions(); @@ -32,5 +31,4 @@ public static void Run() wf.DisplayPropertyValues(); } } - } diff --git a/snippets/core/system-text-json/csharp/RoundtripCamelCasePropertyNames.cs b/snippets/core/system-text-json/csharp/RoundtripCamelCasePropertyNames.cs index ce776d51f29..93e140d0dfa 100644 --- a/snippets/core/system-text-json/csharp/RoundtripCamelCasePropertyNames.cs +++ b/snippets/core/system-text-json/csharp/RoundtripCamelCasePropertyNames.cs @@ -3,12 +3,12 @@ namespace SystemTextJsonSamples { - class RoundtripCamelCasePropertyNames + public class RoundtripCamelCasePropertyNames { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPropertyNameAttribute(); + WeatherForecastWithPropertyNameAttribute weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPropertyNameAttribute(); weatherForecast.DisplayPropertyValues(); // @@ -31,4 +31,4 @@ public static void Run() weatherForecast.DisplayPropertyValues(); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/RoundtripDictionaryTkeyEnumTValue.cs b/snippets/core/system-text-json/csharp/RoundtripDictionaryTkeyEnumTValue.cs index 851694efb56..8e57b60e0ed 100644 --- a/snippets/core/system-text-json/csharp/RoundtripDictionaryTkeyEnumTValue.cs +++ b/snippets/core/system-text-json/csharp/RoundtripDictionaryTkeyEnumTValue.cs @@ -9,7 +9,7 @@ public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithEnumDictionary(); + WeatherForecastWithEnumDictionary weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithEnumDictionary(); // var serializeOptions = new JsonSerializerOptions(); diff --git a/snippets/core/system-text-json/csharp/RoundtripEnumAsString.cs b/snippets/core/system-text-json/csharp/RoundtripEnumAsString.cs index c697c9f5220..f49352dc2aa 100644 --- a/snippets/core/system-text-json/csharp/RoundtripEnumAsString.cs +++ b/snippets/core/system-text-json/csharp/RoundtripEnumAsString.cs @@ -4,12 +4,12 @@ namespace SystemTextJsonSamples { - class RoundtripEnumAsString + public class RoundtripEnumAsString { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithEnum(); + WeatherForecastWithEnum weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithEnum(); weatherForecast.DisplayPropertyValues(); var options = new JsonSerializerOptions diff --git a/snippets/core/system-text-json/csharp/RoundtripExtensionData.cs b/snippets/core/system-text-json/csharp/RoundtripExtensionData.cs index 73843870220..002bafd0331 100644 --- a/snippets/core/system-text-json/csharp/RoundtripExtensionData.cs +++ b/snippets/core/system-text-json/csharp/RoundtripExtensionData.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - class RoundtripExtensionData + public class RoundtripExtensionData { public static void Run() { @@ -44,7 +44,7 @@ public static void Run() Console.WriteLine($"JSON input:\n{jsonString}\n"); // - var weatherForecast = JsonSerializer.Deserialize(jsonString); + WeatherForecastWithExtensionData weatherForecast = JsonSerializer.Deserialize(jsonString); weatherForecast.DisplayPropertyValues(); // @@ -58,4 +58,4 @@ public static void Run() Console.WriteLine($"JSON output:\n{jsonString}\n"); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/RoundtripImmutableStruct.cs b/snippets/core/system-text-json/csharp/RoundtripImmutableStruct.cs index e53ea0fa427..59b6d4e59cd 100644 --- a/snippets/core/system-text-json/csharp/RoundtripImmutableStruct.cs +++ b/snippets/core/system-text-json/csharp/RoundtripImmutableStruct.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Text.Json; -using System.Text.Json.Serialization; -using System.Xml.Schema; namespace SystemTextJsonSamples { @@ -17,12 +15,11 @@ public static void Run() var serializeOptions = new JsonSerializerOptions(); serializeOptions.WriteIndented = true; - //serializeOptions.Converters.Add(new WeatherForecastRequiredPropertyConverter()); jsonString = JsonSerializer.Serialize(points, serializeOptions); Console.WriteLine($"JSON output:\n{jsonString}\n"); var deserializeOptions = new JsonSerializerOptions(); - deserializeOptions.Converters.Add(new ImmutablePointConverter()); + deserializeOptions.Converters.Add(new ImmutablePointConverter(deserializeOptions)); points = JsonSerializer.Deserialize>(jsonString, deserializeOptions); Console.WriteLine("Deserialized object values"); foreach (ImmutablePoint point in points) @@ -31,5 +28,4 @@ public static void Run() } } } - } diff --git a/snippets/core/system-text-json/csharp/RoundtripLongToString.cs b/snippets/core/system-text-json/csharp/RoundtripLongToString.cs index 20c1c21d41c..0c9eb4e0e65 100644 --- a/snippets/core/system-text-json/csharp/RoundtripLongToString.cs +++ b/snippets/core/system-text-json/csharp/RoundtripLongToString.cs @@ -7,15 +7,13 @@ public class RoundtripLongToString { public static void Run() { - string jsonString; - // Serialize to create input JSON - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithLong(); + WeatherForecastWithLong weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithLong(); var serializeOptions = new JsonSerializerOptions { WriteIndented = true }; - jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions); + string jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions); Console.WriteLine($"JSON output:\n{jsonString}\n"); weatherForecast = JsonSerializer.Deserialize(jsonString); diff --git a/snippets/core/system-text-json/csharp/RoundtripPolymorphic.cs b/snippets/core/system-text-json/csharp/RoundtripPolymorphic.cs index a771f16132e..3063805fd9d 100644 --- a/snippets/core/system-text-json/csharp/RoundtripPolymorphic.cs +++ b/snippets/core/system-text-json/csharp/RoundtripPolymorphic.cs @@ -29,5 +29,4 @@ public static void Run() people.ForEach(p => p.DisplayPropertyValues()); } } - } diff --git a/snippets/core/system-text-json/csharp/RoundtripPropertyNamesByAttribute.cs b/snippets/core/system-text-json/csharp/RoundtripPropertyNamesByAttribute.cs index 6de8984a90c..bdd65dc07a0 100644 --- a/snippets/core/system-text-json/csharp/RoundtripPropertyNamesByAttribute.cs +++ b/snippets/core/system-text-json/csharp/RoundtripPropertyNamesByAttribute.cs @@ -3,12 +3,12 @@ namespace SystemTextJsonSamples { - class RoundtripPropertyNamesByAttribute + public class RoundtripPropertyNamesByAttribute { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPropertyNameAttribute(); + WeatherForecastWithPropertyNameAttribute weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPropertyNameAttribute(); weatherForecast.DisplayPropertyValues(); // @@ -26,4 +26,4 @@ public static void Run() // } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/RoundtripPropertyNamingPolicy.cs b/snippets/core/system-text-json/csharp/RoundtripPropertyNamingPolicy.cs index ff94247b1bc..abece5d31e0 100644 --- a/snippets/core/system-text-json/csharp/RoundtripPropertyNamingPolicy.cs +++ b/snippets/core/system-text-json/csharp/RoundtripPropertyNamingPolicy.cs @@ -5,12 +5,12 @@ namespace SystemTextJsonSamples { - class RoundtripPropertyNamingPolicy + public class RoundtripPropertyNamingPolicy { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/RoundtripToFile.cs b/snippets/core/system-text-json/csharp/RoundtripToFile.cs index 6a3be88fbfa..eed94af668e 100644 --- a/snippets/core/system-text-json/csharp/RoundtripToFile.cs +++ b/snippets/core/system-text-json/csharp/RoundtripToFile.cs @@ -6,13 +6,13 @@ namespace SystemTextJsonSamples { - class RoundtripToFile + public class RoundtripToFile { public static void Run() { string jsonString; string fileName = "WeatherForecast.json"; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/RoundtripToFileAsync.cs b/snippets/core/system-text-json/csharp/RoundtripToFileAsync.cs index f46e5d2dde2..401f90ca0cd 100644 --- a/snippets/core/system-text-json/csharp/RoundtripToFileAsync.cs +++ b/snippets/core/system-text-json/csharp/RoundtripToFileAsync.cs @@ -6,13 +6,12 @@ namespace SystemTextJsonSamples { - class RoundtripToFileAsync + public class RoundtripToFileAsync { public static async Task RunAsync() { string fileName = "WeatherForecastAsync.json"; - string fileNameUtf8 = "WeatherForecastAsyncUtf8"; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.DisplayPropertyValues(); // @@ -30,24 +29,6 @@ public static async Task RunAsync() } // weatherForecast.DisplayPropertyValues(); - - using (FileStream fs = File.Create(fileNameUtf8)) - { - using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8)) - { - await JsonSerializer.SerializeAsync(fs, weatherForecast); - } - } - Console.WriteLine($"The result is in {fileNameUtf8}\n"); - using (FileStream fs = File.OpenRead(fileNameUtf8)) - { - //System.Text.Json.JsonException: '0xEF' is invalid after a single JSON value. Expected end of data. - //weatherForecast = await JsonSerializer.DeserializeAsync(fs); - } - // - weatherForecast.DisplayPropertyValues(); - - } } } diff --git a/snippets/core/system-text-json/csharp/RoundtripToString.cs b/snippets/core/system-text-json/csharp/RoundtripToString.cs index 8c06fa36514..0d346a574a6 100644 --- a/snippets/core/system-text-json/csharp/RoundtripToString.cs +++ b/snippets/core/system-text-json/csharp/RoundtripToString.cs @@ -3,11 +3,11 @@ namespace SystemTextJsonSamples { - class RoundtripToString + public class RoundtripToString { public static void Run() { - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPOCOs(); + WeatherForecastWithPOCOs weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithPOCOs(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/RoundtripToUtf8.cs b/snippets/core/system-text-json/csharp/RoundtripToUtf8.cs index 38a30611377..156bfba4cec 100644 --- a/snippets/core/system-text-json/csharp/RoundtripToUtf8.cs +++ b/snippets/core/system-text-json/csharp/RoundtripToUtf8.cs @@ -5,11 +5,11 @@ namespace SystemTextJsonSamples { - class RoundtripToUtf8 + public class RoundtripToUtf8 { public static void Run() { - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/SerializeCamelCaseDictionaryKeys.cs b/snippets/core/system-text-json/csharp/SerializeCamelCaseDictionaryKeys.cs index e07604cd63f..886291e2b4b 100644 --- a/snippets/core/system-text-json/csharp/SerializeCamelCaseDictionaryKeys.cs +++ b/snippets/core/system-text-json/csharp/SerializeCamelCaseDictionaryKeys.cs @@ -9,7 +9,7 @@ public class SerializeCamelCaseDictionaryKeys public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithDictionary(); + WeatherForecastWithDictionary weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithDictionary(); weatherForecast.DisplayPropertyValues(); // diff --git a/snippets/core/system-text-json/csharp/SerializeCustomEncoding.cs b/snippets/core/system-text-json/csharp/SerializeCustomEncoding.cs index b719255adbf..73feb68e618 100644 --- a/snippets/core/system-text-json/csharp/SerializeCustomEncoding.cs +++ b/snippets/core/system-text-json/csharp/SerializeCustomEncoding.cs @@ -7,12 +7,12 @@ namespace SystemTextJsonSamples { - class SerializeCustomEncoding + public class SerializeCustomEncoding { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastCyrillic(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecastCyrillic(); weatherForecast.DisplayPropertyValues(); Console.WriteLine("Default serialization - non-ASCII escaped"); @@ -28,7 +28,7 @@ public static void Run() // options = new JsonSerializerOptions { - Encoder = JavaScriptEncoder.Create(UnicodeRanges.Cyrillic, UnicodeRanges.GreekExtended), + Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic), WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, options); @@ -40,6 +40,7 @@ public static void Run() // var encoderSettings = new TextEncoderSettings(); encoderSettings.AllowCharacters('\u0436', '\u0430'); + encoderSettings.AllowRange(UnicodeRanges.BasicLatin); options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(encoderSettings), @@ -62,4 +63,4 @@ public static void Run() Console.WriteLine(jsonString); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/SerializeExcludeNullValueProperties.cs b/snippets/core/system-text-json/csharp/SerializeExcludeNullValueProperties.cs index 96adae306ce..475ed9fa143 100644 --- a/snippets/core/system-text-json/csharp/SerializeExcludeNullValueProperties.cs +++ b/snippets/core/system-text-json/csharp/SerializeExcludeNullValueProperties.cs @@ -5,12 +5,12 @@ namespace SystemTextJsonSamples { - class SerializeExcludeNullValueProperties + public class SerializeExcludeNullValueProperties { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); weatherForecast.Summary = null; weatherForecast.DisplayPropertyValues(); @@ -26,4 +26,4 @@ public static void Run() Console.WriteLine(); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/SerializeExcludePropertiesyByAttribute.cs b/snippets/core/system-text-json/csharp/SerializeExcludePropertiesyByAttribute.cs index 60021c2fcf2..acfd216fa90 100644 --- a/snippets/core/system-text-json/csharp/SerializeExcludePropertiesyByAttribute.cs +++ b/snippets/core/system-text-json/csharp/SerializeExcludePropertiesyByAttribute.cs @@ -5,12 +5,12 @@ namespace SystemTextJsonSamples { - class SerializeExcludePropertiesByAttribute + public class SerializeExcludePropertiesByAttribute { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithIgnoreAttribute(); + WeatherForecastWithIgnoreAttribute weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithIgnoreAttribute(); weatherForecast.DisplayPropertyValues(); // @@ -24,4 +24,4 @@ public static void Run() Console.WriteLine(); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/SerializeExcludeReadOnlyProperties.cs b/snippets/core/system-text-json/csharp/SerializeExcludeReadOnlyProperties.cs index b1f6550f53e..997b3b16039 100644 --- a/snippets/core/system-text-json/csharp/SerializeExcludeReadOnlyProperties.cs +++ b/snippets/core/system-text-json/csharp/SerializeExcludeReadOnlyProperties.cs @@ -5,12 +5,12 @@ namespace SystemTextJsonSamples { - class SerializeExcludeReadOnlyProperties + public class SerializeExcludeReadOnlyProperties { public static void Run() { string jsonString; - var weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithROProperty(); + WeatherForecastWithROProperty weatherForecast = WeatherForecastFactories.CreateWeatherForecastWithROProperty(); weatherForecast.DisplayPropertyValues(); // @@ -25,4 +25,4 @@ public static void Run() Console.WriteLine(); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/SerializePolymorphic.cs b/snippets/core/system-text-json/csharp/SerializePolymorphic.cs index 4943b888c0d..9c536681430 100644 --- a/snippets/core/system-text-json/csharp/SerializePolymorphic.cs +++ b/snippets/core/system-text-json/csharp/SerializePolymorphic.cs @@ -3,7 +3,7 @@ namespace SystemTextJsonSamples { - class SerializePolymorphic + public class SerializePolymorphic { public static void Run() { @@ -45,4 +45,4 @@ public static void Run() Console.WriteLine($"JSON output:\n{jsonString}\n"); } } -} \ No newline at end of file +} diff --git a/snippets/core/system-text-json/csharp/SerializeRuntimePropertyExclusion.cs b/snippets/core/system-text-json/csharp/SerializeRuntimePropertyExclusion.cs index e56a036aa88..26b43db2f63 100644 --- a/snippets/core/system-text-json/csharp/SerializeRuntimePropertyExclusion.cs +++ b/snippets/core/system-text-json/csharp/SerializeRuntimePropertyExclusion.cs @@ -33,5 +33,4 @@ public static void Run() wf.DisplayPropertyValues(); } } - } diff --git a/snippets/core/system-text-json/csharp/SystemTextJsonSamples.csproj b/snippets/core/system-text-json/csharp/SystemTextJsonSamples.csproj index 99961bed6b3..fa98313dccd 100644 --- a/snippets/core/system-text-json/csharp/SystemTextJsonSamples.csproj +++ b/snippets/core/system-text-json/csharp/SystemTextJsonSamples.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 diff --git a/snippets/core/system-text-json/csharp/TemperatureConverter.cs b/snippets/core/system-text-json/csharp/TemperatureConverter.cs index 6eca4470834..15d2a49e9c0 100644 --- a/snippets/core/system-text-json/csharp/TemperatureConverter.cs +++ b/snippets/core/system-text-json/csharp/TemperatureConverter.cs @@ -14,8 +14,8 @@ public override Temperature Read( public override void Write( Utf8JsonWriter writer, - Temperature value, + Temperature temperature, JsonSerializerOptions options) => - writer.WriteStringValue(value.ToString()); + writer.WriteStringValue(temperature.ToString()); } } diff --git a/snippets/core/system-text-json/csharp/Universities.json b/snippets/core/system-text-json/csharp/Universities.json index f8c9339e62a..5adae482061 100644 Binary files a/snippets/core/system-text-json/csharp/Universities.json and b/snippets/core/system-text-json/csharp/Universities.json differ diff --git a/snippets/core/system-text-json/csharp/UpperCaseNamingPolicy.cs b/snippets/core/system-text-json/csharp/UpperCaseNamingPolicy.cs index f15faac20d2..7ca43ad464a 100644 --- a/snippets/core/system-text-json/csharp/UpperCaseNamingPolicy.cs +++ b/snippets/core/system-text-json/csharp/UpperCaseNamingPolicy.cs @@ -2,7 +2,7 @@ namespace SystemTextJsonSamples { - class UpperCaseNamingPolicy : JsonNamingPolicy + public class UpperCaseNamingPolicy : JsonNamingPolicy { public override string ConvertName(string name) => name.ToUpper(); diff --git a/snippets/core/system-text-json/csharp/Utf8ReaderFromBytes.cs b/snippets/core/system-text-json/csharp/Utf8ReaderFromBytes.cs index d0d15c65cb1..c7d4ac970fc 100644 --- a/snippets/core/system-text-json/csharp/Utf8ReaderFromBytes.cs +++ b/snippets/core/system-text-json/csharp/Utf8ReaderFromBytes.cs @@ -3,11 +3,11 @@ namespace SystemTextJsonSamples { - class Utf8ReaderFromBytes + public class Utf8ReaderFromBytes { public static void Run() { - var weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); + WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast(); byte[] jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes( weatherForecast, new JsonSerializerOptions { WriteIndented = true }); @@ -37,9 +37,9 @@ public static void Run() case JsonTokenType.Number: { - int value = reader.GetInt32(); + int intValue = reader.GetInt32(); Console.Write(" "); - Console.Write(value); + Console.Write(intValue); break; } diff --git a/snippets/core/system-text-json/csharp/Utf8ReaderFromFile.cs b/snippets/core/system-text-json/csharp/Utf8ReaderFromFile.cs index 1eea31b8757..ad5b3b898fd 100644 --- a/snippets/core/system-text-json/csharp/Utf8ReaderFromFile.cs +++ b/snippets/core/system-text-json/csharp/Utf8ReaderFromFile.cs @@ -5,7 +5,7 @@ namespace SystemTextJsonSamples { - class Utf8ReaderFromFile + public class Utf8ReaderFromFile { private static readonly byte[] s_nameUtf8 = Encoding.UTF8.GetBytes("name"); public static void Run() diff --git a/snippets/core/system-text-json/csharp/Utf8WriterToStream.cs b/snippets/core/system-text-json/csharp/Utf8WriterToStream.cs index 3f18c4869e8..3e67b43a54e 100644 --- a/snippets/core/system-text-json/csharp/Utf8WriterToStream.cs +++ b/snippets/core/system-text-json/csharp/Utf8WriterToStream.cs @@ -5,7 +5,7 @@ namespace SystemTextJsonSamples { - class Utf8WriterToStream + public class Utf8WriterToStream { public static void Run() { diff --git a/snippets/core/system-text-json/csharp/Utilities.cs b/snippets/core/system-text-json/csharp/Utilities.cs index 9f01ce27f34..f848708a940 100644 --- a/snippets/core/system-text-json/csharp/Utilities.cs +++ b/snippets/core/system-text-json/csharp/Utilities.cs @@ -5,7 +5,7 @@ namespace SystemTextJsonSamples { - class Utilities + public class Utilities { public static void DisplayPropertyValues(object o) { diff --git a/snippets/core/system-text-json/csharp/WeatherForecast.cs b/snippets/core/system-text-json/csharp/WeatherForecast.cs index 7b9a6771393..335a399fce3 100644 --- a/snippets/core/system-text-json/csharp/WeatherForecast.cs +++ b/snippets/core/system-text-json/csharp/WeatherForecast.cs @@ -28,6 +28,7 @@ public class WeatherForecastWithDefault { public WeatherForecastWithDefault() { + Date = DateTimeOffset.Parse("2001-01-01"); Summary = "No summary"; } public DateTimeOffset Date { get; set; } diff --git a/snippets/core/system-text-json/csharp/WeatherForecastCallbacksConverter.cs b/snippets/core/system-text-json/csharp/WeatherForecastCallbacksConverter.cs index c5b364eab82..29e38e2a766 100644 --- a/snippets/core/system-text-json/csharp/WeatherForecastCallbacksConverter.cs +++ b/snippets/core/system-text-json/csharp/WeatherForecastCallbacksConverter.cs @@ -11,27 +11,28 @@ public override WeatherForecast Read( Type type, JsonSerializerOptions options) { - // Place "before" code here (OnDeserializing), but note that there is no access here to the POCO instance. + // Place "before" code here (OnDeserializing), + // but note that there is no access here to the POCO instance. Console.WriteLine("OnDeserializing"); // Don't pass in options when recursively calling Deserialize. - WeatherForecast value = JsonSerializer.Deserialize(ref reader); + WeatherForecast forecast = JsonSerializer.Deserialize(ref reader); // Place "after" code here (OnDeserialized) Console.WriteLine("OnDeserialized"); - return value; + return forecast; } public override void Write( Utf8JsonWriter writer, - WeatherForecast value, JsonSerializerOptions options) + WeatherForecast forecast, JsonSerializerOptions options) { // Place "before" code here (OnSerializing) Console.WriteLine("OnSerializing"); // Don't pass in options when recursively calling Serialize. - JsonSerializer.Serialize(writer, value); + JsonSerializer.Serialize(writer, forecast); // Place "after" code here (OnSerialized) Console.WriteLine("OnSerialized"); diff --git a/snippets/core/system-text-json/csharp/WeatherForecastRequiredPropertyConverter.cs b/snippets/core/system-text-json/csharp/WeatherForecastRequiredPropertyConverter.cs index ed7e8ec2225..e8713349a58 100644 --- a/snippets/core/system-text-json/csharp/WeatherForecastRequiredPropertyConverter.cs +++ b/snippets/core/system-text-json/csharp/WeatherForecastRequiredPropertyConverter.cs @@ -12,22 +12,22 @@ public override WeatherForecast Read( JsonSerializerOptions options) { // Don't pass in options when recursively calling Deserialize. - WeatherForecast value = JsonSerializer.Deserialize(ref reader); + WeatherForecast forecast = JsonSerializer.Deserialize(ref reader); // Check for required fields set by values in JSON - if (value.Date == default) + if (forecast.Date == default) { throw new JsonException("Required property not received in the JSON"); - }; - return value; + } + return forecast; } public override void Write( Utf8JsonWriter writer, - WeatherForecast value, JsonSerializerOptions options) + WeatherForecast forecast, JsonSerializerOptions options) { // Don't pass in options when recursively calling Serialize. - JsonSerializer.Serialize(writer, value); + JsonSerializer.Serialize(writer, forecast); } } } diff --git a/snippets/core/system-text-json/csharp/WeatherForecastRuntimeIgnoreConverter.cs b/snippets/core/system-text-json/csharp/WeatherForecastRuntimeIgnoreConverter.cs index 28f266ec7af..12c47a50380 100644 --- a/snippets/core/system-text-json/csharp/WeatherForecastRuntimeIgnoreConverter.cs +++ b/snippets/core/system-text-json/csharp/WeatherForecastRuntimeIgnoreConverter.cs @@ -27,7 +27,7 @@ public override WeatherForecast Read( if (reader.TokenType == JsonTokenType.PropertyName) { - var propertyName = reader.GetString(); + string propertyName = reader.GetString(); reader.Read(); switch (propertyName) { @@ -52,9 +52,6 @@ public override WeatherForecast Read( public override void Write(Utf8JsonWriter writer, WeatherForecast wf, JsonSerializerOptions options) { - // Location for OnSerializing "callback" code. - Console.WriteLine("OnSerializing"); - writer.WriteStartObject(); writer.WriteString("Date", wf.Date);