From de43acd4cf6fb1597468b910d9736d4cbf1bce93 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Tue, 21 Jul 2020 09:11:11 -0700 Subject: [PATCH 1/2] Clean up code following JSON number handling and field support PRs --- src/libraries/System.Text.Json/src/Resources/Strings.resx | 4 ++-- .../src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs | 2 +- .../Serialization/Attributes/JsonConverterAttribute.cs | 2 +- .../Serialization/Attributes/JsonExtensionDataAttribute.cs | 2 +- .../Json/Serialization/Attributes/JsonIncludeAttribute.cs | 7 +++++-- .../Attributes/JsonNumberHandlingAttribute.cs | 4 ++-- .../Serialization/Converters/Collection/IListConverter.cs | 1 - .../System/Text/Json/Serialization/JsonNumberHandling.cs | 6 +++--- .../Text/Json/Serialization/JsonSerializer.Read.Helpers.cs | 3 +-- .../tests/Serialization/ExtensionDataTests.cs | 2 +- 10 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Text.Json/src/Resources/Strings.resx b/src/libraries/System.Text.Json/src/Resources/Strings.resx index d5db3cfc6fb1b0..07655cbccea7a5 100644 --- a/src/libraries/System.Text.Json/src/Resources/Strings.resx +++ b/src/libraries/System.Text.Json/src/Resources/Strings.resx @@ -486,7 +486,7 @@ Members '{0}' and '{1}' on type '{2}' cannot both bind with parameter '{3}' in constructor '{4}' on deserialization. - Each parameter in constructor '{0}' on type '{1}' must bind to an object member on deserialization. Each parameter name must be the camel case equivalent of an object member named with the pascal case naming convention. + Each parameter in constructor '{0}' on type '{1}' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive. The constructor '{0}' on type '{1}' may not have more than 64 parameters for deserialization. @@ -542,4 +542,4 @@ When 'JsonNumberHandlingAttribute' is placed on a property or field, the property or field must be a number or a collection. See member '{0}' on type '{1}'. - \ No newline at end of file + diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs index d08cbcbc28dd06..b888bf3c10f3e4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs @@ -977,7 +977,7 @@ public bool TryGetSingle(out float value) throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } - ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan;; + ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (Utf8Parser.TryParse(span, out float tmp, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonConverterAttribute.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonConverterAttribute.cs index e4248e726d5d54..098b1e175e01c4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonConverterAttribute.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonConverterAttribute.cs @@ -12,7 +12,7 @@ namespace System.Text.Json.Serialization /// The specified converter type must derive from . /// When placed on a property or field, the specified converter will always be used. /// When placed on a type, the specified converter will be used unless a compatible converter is added to - /// or there is another on a member + /// or there is another on a property or field /// of the same type. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonExtensionDataAttribute.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonExtensionDataAttribute.cs index 242d50d11e8284..194143cfaf3237 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonExtensionDataAttribute.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonExtensionDataAttribute.cs @@ -16,7 +16,7 @@ namespace System.Text.Json.Serialization /// During serializing, the name of the extension data member is not included in the JSON; /// the data contained within the extension data is serialized as properties of the JSON object. /// - /// If there is more than one extension member on a type, or it the member is not of the correct type, + /// If there is more than one extension member on a type, or the member is not of the correct type, /// an is thrown during the first serialization or deserialization of that type. /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonIncludeAttribute.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonIncludeAttribute.cs index cf5597a574be99..40a6132a20f08d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonIncludeAttribute.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonIncludeAttribute.cs @@ -4,10 +4,13 @@ namespace System.Text.Json.Serialization { /// - /// Indicates that the member should be included for serialization and deserialization. + /// Indicates that the property or field should be included for serialization and deserialization. /// /// - /// When applied to a property, indicates that non-public getters and setters can be used for serialization and deserialization. + /// When applied to a public property, indicates that non-public getters and setters should be used for serialization and deserialization. + /// + /// Non-public properties and fields are not allowed when serializing and deserializing. If the attribute is used on a non-public property or field, + /// an is thrown during the first serialization or deserialization of the declaring type. /// [AttributeUsage(AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple = false)] public sealed class JsonIncludeAttribute : JsonAttribute diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNumberHandlingAttribute.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNumberHandlingAttribute.cs index 581d4aacd766e4..ac7661b1d9eb2b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNumberHandlingAttribute.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNumberHandlingAttribute.cs @@ -5,13 +5,13 @@ namespace System.Text.Json.Serialization { /// /// When placed on a type, property, or field, indicates what - /// settings should be used when serializing or deserialing numbers. + /// settings should be used when serializing or deserializing numbers. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public sealed class JsonNumberHandlingAttribute : JsonAttribute { /// - /// Indicates what settings should be used when serializing or deserialing numbers. + /// Indicates what settings should be used when serializing or deserializing numbers. /// public JsonNumberHandling Handling { get; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IListConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IListConverter.cs index 606ddfa7ca3d78..f4e9f29b86120f 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IListConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IListConverter.cs @@ -82,7 +82,6 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, } } - return true; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberHandling.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberHandling.cs index 09be0bfc94c81e..0e791730f84f65 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberHandling.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberHandling.cs @@ -23,9 +23,9 @@ public enum JsonNumberHandling /// WriteAsString = 0x2, /// - /// The "NaN", "Infinity", and "-Infinity" tokens can be read as floating-point constants, - /// and the , , and - /// values will be written as their corresponding JSON string representations. + /// The "NaN", "Infinity", and "-Infinity" tokens can be read as + /// floating-point constants, and the and values for these + /// constants will be written as their corresponding JSON string representations. /// AllowNamedFloatingPointLiterals = 0x4 } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs index 8b01c199e6e470..f41e1090b270f4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs @@ -39,8 +39,7 @@ private static TValue ReadCore(JsonConverter jsonConverter, ref Utf8Json internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) { - int handlingValue = (int)handling; - return handlingValue >= 0 && handlingValue <= 7; + return JsonHelpers.IsInRangeInclusive((int)handling, 0, 7); } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/ExtensionDataTests.cs b/src/libraries/System.Text.Json/tests/Serialization/ExtensionDataTests.cs index 8899ef5e9c18b9..a005ec38e6ef04 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/ExtensionDataTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/ExtensionDataTests.cs @@ -55,7 +55,7 @@ void Verify() } [Fact] - public static void ExtensioFieldNotUsed() + public static void ExtensionFieldNotUsed() { string json = @"{""MyNestedClass"":" + SimpleTestClass.s_json + "}"; ClassWithExtensionField obj = JsonSerializer.Deserialize(json); From 6127df5a01f01032940b81d03d05c1d0cffc41d2 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Tue, 21 Jul 2020 10:52:01 -0700 Subject: [PATCH 2/2] Move IsValidNumberHandlingValue to better location --- .../System.Text.Json/src/System.Text.Json.csproj | 1 + .../Serialization/JsonSerializer.Read.Helpers.cs | 6 ------ .../Text/Json/Serialization/JsonSerializer.cs | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.cs diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index e787defd06be02..64d2b0562b0e1a 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -142,6 +142,7 @@ + diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs index f41e1090b270f4..4e7946872454c4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json.Serialization; namespace System.Text.Json @@ -36,10 +35,5 @@ private static TValue ReadCore(JsonConverter jsonConverter, ref Utf8Json Debug.Assert(value == null || value is TValue); return (TValue)value!; } - - internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) - { - return JsonHelpers.IsInRangeInclusive((int)handling, 0, 7); - } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.cs new file mode 100644 index 00000000000000..8ddd013597fe12 --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Text.Json.Serialization; + +namespace System.Text.Json +{ + public static partial class JsonSerializer + { + internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) + { + return JsonHelpers.IsInRangeInclusive((int)handling, 0, 7); + } + } +}