From 56b5df416f2f41aa5631d41c4a9e4eb9a37e406b Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 23 Nov 2021 12:44:34 +0000 Subject: [PATCH] [System.Text.Json] Move inline throw statements to `ThrowHelper` (#61746) * Replace occurrences of inlined throws with ThrowHelper calls * remove NoInlining from ThrowHelpers.Throw* methods * address feedback * remove NoInlining attribute from local throw method * Incorporate changes from #61608 --- .../System/Text/Json/Document/JsonDocument.cs | 2 +- .../Text/Json/Document/JsonDocumentOptions.cs | 4 +- .../System/Text/Json/Document/JsonElement.cs | 78 +++++---- .../JsonPropertyDictionary.KeyCollection.cs | 6 +- .../JsonPropertyDictionary.ValueCollection.cs | 6 +- .../Text/Json/Reader/JsonReaderOptions.cs | 8 +- .../Text/Json/Reader/Utf8JsonReader.TryGet.cs | 156 +++++++++--------- .../System/Text/Json/Reader/Utf8JsonReader.cs | 7 +- .../Converters/Value/BooleanConverter.cs | 9 +- .../Converters/Value/CharConverter.cs | 2 +- .../Converters/Value/TimeSpanConverter.cs | 14 +- .../Converters/Value/VersionConverter.cs | 10 +- .../Serialization/JsonSerializerOptions.cs | 2 +- .../src/System/Text/Json/ThrowHelper.Node.cs | 12 +- .../Text/Json/ThrowHelper.Serialization.cs | 58 ------- .../src/System/Text/Json/ThrowHelper.cs | 94 +++++------ .../Text/Json/Writer/JsonWriterOptions.cs | 4 +- 17 files changed, 200 insertions(+), 272 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index 59f944e51534a..33f4f33b46c51 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -1107,7 +1107,7 @@ private void CheckExpectedType(JsonTokenType expected, JsonTokenType actual) { if (expected != actual) { - throw ThrowHelper.GetJsonElementWrongTypeException(expected, actual); + ThrowHelper.ThrowJsonElementWrongTypeException(expected, actual); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs index b091a30012120..df1289c295d3b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs @@ -52,7 +52,9 @@ public int MaxDepth set { if (value < 0) - throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + { + ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + } _maxDepth = value; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs index 5f30dd6d3dd8b..63963db4b5fd1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace System.Text.Json { @@ -333,7 +334,12 @@ public bool GetBoolean() return type == JsonTokenType.True ? true : type == JsonTokenType.False ? false : - throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), type); + ThrowJsonElementWrongTypeException(type); + + static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType) + { + throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind()); + } } /// @@ -400,12 +406,12 @@ public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value) /// public byte[] GetBytesFromBase64() { - if (TryGetBytesFromBase64(out byte[]? value)) + if (!TryGetBytesFromBase64(out byte[]? value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -645,12 +651,12 @@ public bool TryGetInt32(out int value) /// public int GetInt32() { - if (TryGetInt32(out int value)) + if (!TryGetInt32(out int value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -697,12 +703,12 @@ public bool TryGetUInt32(out uint value) [CLSCompliant(false)] public uint GetUInt32() { - if (TryGetUInt32(out uint value)) + if (!TryGetUInt32(out uint value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -747,12 +753,12 @@ public bool TryGetInt64(out long value) /// public long GetInt64() { - if (TryGetInt64(out long value)) + if (!TryGetInt64(out long value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -799,12 +805,12 @@ public bool TryGetUInt64(out ulong value) [CLSCompliant(false)] public ulong GetUInt64() { - if (TryGetUInt64(out ulong value)) + if (!TryGetUInt64(out ulong value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -866,12 +872,12 @@ public bool TryGetDouble(out double value) /// public double GetDouble() { - if (TryGetDouble(out double value)) + if (!TryGetDouble(out double value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -933,12 +939,12 @@ public bool TryGetSingle(out float value) /// public float GetSingle() { - if (TryGetSingle(out float value)) + if (!TryGetSingle(out float value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -985,12 +991,12 @@ public bool TryGetDecimal(out decimal value) /// public decimal GetDecimal() { - if (TryGetDecimal(out decimal value)) + if (!TryGetDecimal(out decimal value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -1036,12 +1042,12 @@ public bool TryGetDateTime(out DateTime value) /// public DateTime GetDateTime() { - if (TryGetDateTime(out DateTime value)) + if (!TryGetDateTime(out DateTime value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -1087,12 +1093,12 @@ public bool TryGetDateTimeOffset(out DateTimeOffset value) /// public DateTimeOffset GetDateTimeOffset() { - if (TryGetDateTimeOffset(out DateTimeOffset value)) + if (!TryGetDateTimeOffset(out DateTimeOffset value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } /// @@ -1138,12 +1144,12 @@ public bool TryGetGuid(out Guid value) /// public Guid GetGuid() { - if (TryGetGuid(out Guid value)) + if (!TryGetGuid(out Guid value)) { - return value; + ThrowHelper.ThrowFormatException(); } - throw ThrowHelper.GetFormatException(); + return value; } internal string GetPropertyName() @@ -1326,7 +1332,7 @@ public ArrayEnumerator EnumerateArray() if (tokenType != JsonTokenType.StartArray) { - throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType); + ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType); } return new ArrayEnumerator(this); @@ -1352,7 +1358,7 @@ public ObjectEnumerator EnumerateObject() if (tokenType != JsonTokenType.StartObject) { - throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType); + ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType); } return new ObjectEnumerator(this); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.KeyCollection.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.KeyCollection.cs index 57c2bf331e8b8..a0e9edb109bfb 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.KeyCollection.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.KeyCollection.cs @@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator() } } - public void Add(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + public void Add(string propertyName) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly(); - public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly(); public bool Contains(string propertyName) => _parent.ContainsProperty(propertyName); @@ -68,7 +68,7 @@ public IEnumerator GetEnumerator() } } - bool ICollection.Remove(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + bool ICollection.Remove(string propertyName) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly(); } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.ValueCollection.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.ValueCollection.cs index a441986688c70..c907454d800e6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.ValueCollection.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.ValueCollection.cs @@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator() } } - public void Add(T? jsonNode) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + public void Add(T? jsonNode) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly(); - public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly(); public bool Contains(T? jsonNode) => _parent.ContainsValue(jsonNode); @@ -68,7 +68,7 @@ public void CopyTo(T?[] nodeArray, int index) } } - bool ICollection.Remove(T? node) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly(); + bool ICollection.Remove(T? node) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly(); } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderOptions.cs index 2d29fc328af83..5cc83c995b18c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderOptions.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderOptions.cs @@ -31,7 +31,9 @@ public JsonCommentHandling CommentHandling { Debug.Assert(value >= 0); if (value > JsonCommentHandling.Allow) - throw ThrowHelper.GetArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value)); + { + ThrowHelper.ThrowArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value)); + } _commentHandling = value; } @@ -52,7 +54,9 @@ public int MaxDepth set { if (value < 0) - throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + { + ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + } _maxDepth = value; } 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 b65653ee71527..cee0eee739859 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 @@ -33,7 +33,7 @@ public ref partial struct Utf8JsonReader if (TokenType != JsonTokenType.String && TokenType != JsonTokenType.PropertyName) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -60,7 +60,7 @@ public string GetComment() { if (TokenType != JsonTokenType.Comment) { - throw ThrowHelper.GetInvalidOperationException_ExpectedComment(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedComment(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return JsonReaderHelper.TranscodeHelper(span); @@ -76,22 +76,20 @@ public string GetComment() /// public bool GetBoolean() { - ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; - - if (TokenType == JsonTokenType.True) + JsonTokenType type = TokenType; + if (type == JsonTokenType.True) { - Debug.Assert(span.Length == 4); + Debug.Assert((HasValueSequence ? ValueSequence.ToArray() : ValueSpan).Length == 4); return true; } - else if (TokenType == JsonTokenType.False) - { - Debug.Assert(span.Length == 5); - return false; - } - else + else if (type != JsonTokenType.False) { - throw ThrowHelper.GetInvalidOperationException_ExpectedBoolean(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedBoolean(TokenType); + Debug.Fail("Throw helper should have thrown an exception."); } + + Debug.Assert((HasValueSequence ? ValueSequence.ToArray() : ValueSpan).Length == 5); + return false; } /// @@ -109,7 +107,7 @@ public byte[] GetBytesFromBase64() { if (!TryGetBytesFromBase64(out byte[]? value)) { - throw ThrowHelper.GetFormatException(DataType.Base64String); + ThrowHelper.ThrowFormatException(DataType.Base64String); } return value; } @@ -133,7 +131,7 @@ public byte GetByte() { if (!TryGetByte(out byte value)) { - throw ThrowHelper.GetFormatException(NumericType.Byte); + ThrowHelper.ThrowFormatException(NumericType.Byte); } return value; } @@ -143,7 +141,7 @@ internal byte GetByteWithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetByteCore(out byte value, span)) { - throw ThrowHelper.GetFormatException(NumericType.Byte); + ThrowHelper.ThrowFormatException(NumericType.Byte); } return value; } @@ -168,7 +166,7 @@ public sbyte GetSByte() { if (!TryGetSByte(out sbyte value)) { - throw ThrowHelper.GetFormatException(NumericType.SByte); + ThrowHelper.ThrowFormatException(NumericType.SByte); } return value; } @@ -178,7 +176,7 @@ internal sbyte GetSByteWithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetSByteCore(out sbyte value, span)) { - throw ThrowHelper.GetFormatException(NumericType.SByte); + ThrowHelper.ThrowFormatException(NumericType.SByte); } return value; } @@ -202,7 +200,7 @@ public short GetInt16() { if (!TryGetInt16(out short value)) { - throw ThrowHelper.GetFormatException(NumericType.Int16); + ThrowHelper.ThrowFormatException(NumericType.Int16); } return value; } @@ -212,7 +210,7 @@ internal short GetInt16WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetInt16Core(out short value, span)) { - throw ThrowHelper.GetFormatException(NumericType.Int16); + ThrowHelper.ThrowFormatException(NumericType.Int16); } return value; } @@ -236,7 +234,7 @@ public int GetInt32() { if (!TryGetInt32(out int value)) { - throw ThrowHelper.GetFormatException(NumericType.Int32); + ThrowHelper.ThrowFormatException(NumericType.Int32); } return value; } @@ -246,7 +244,7 @@ internal int GetInt32WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetInt32Core(out int value, span)) { - throw ThrowHelper.GetFormatException(NumericType.Int32); + ThrowHelper.ThrowFormatException(NumericType.Int32); } return value; } @@ -270,7 +268,7 @@ public long GetInt64() { if (!TryGetInt64(out long value)) { - throw ThrowHelper.GetFormatException(NumericType.Int64); + ThrowHelper.ThrowFormatException(NumericType.Int64); } return value; } @@ -280,7 +278,7 @@ internal long GetInt64WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetInt64Core(out long value, span)) { - throw ThrowHelper.GetFormatException(NumericType.Int64); + ThrowHelper.ThrowFormatException(NumericType.Int64); } return value; } @@ -305,7 +303,7 @@ public ushort GetUInt16() { if (!TryGetUInt16(out ushort value)) { - throw ThrowHelper.GetFormatException(NumericType.UInt16); + ThrowHelper.ThrowFormatException(NumericType.UInt16); } return value; } @@ -315,7 +313,7 @@ internal ushort GetUInt16WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetUInt16Core(out ushort value, span)) { - throw ThrowHelper.GetFormatException(NumericType.UInt16); + ThrowHelper.ThrowFormatException(NumericType.UInt16); } return value; } @@ -340,7 +338,7 @@ public uint GetUInt32() { if (!TryGetUInt32(out uint value)) { - throw ThrowHelper.GetFormatException(NumericType.UInt32); + ThrowHelper.ThrowFormatException(NumericType.UInt32); } return value; } @@ -350,7 +348,7 @@ internal uint GetUInt32WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetUInt32Core(out uint value, span)) { - throw ThrowHelper.GetFormatException(NumericType.UInt32); + ThrowHelper.ThrowFormatException(NumericType.UInt32); } return value; } @@ -375,7 +373,7 @@ public ulong GetUInt64() { if (!TryGetUInt64(out ulong value)) { - throw ThrowHelper.GetFormatException(NumericType.UInt64); + ThrowHelper.ThrowFormatException(NumericType.UInt64); } return value; } @@ -385,7 +383,7 @@ internal ulong GetUInt64WithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetUInt64Core(out ulong value, span)) { - throw ThrowHelper.GetFormatException(NumericType.UInt64); + ThrowHelper.ThrowFormatException(NumericType.UInt64); } return value; } @@ -408,7 +406,7 @@ public float GetSingle() { if (!TryGetSingle(out float value)) { - throw ThrowHelper.GetFormatException(NumericType.Single); + ThrowHelper.ThrowFormatException(NumericType.Single); } return value; } @@ -422,31 +420,29 @@ internal float GetSingleWithQuotes() return value; } - if (Utf8Parser.TryParse(span, out value, out int bytesConsumed) - && span.Length == bytesConsumed) + // NETCOREAPP implementation of the TryParse method above permits case-insensitive variants of the + // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation. + // The following logic reconciles the two implementations to enforce consistent behavior. + if (!(Utf8Parser.TryParse(span, out value, out int bytesConsumed) + && span.Length == bytesConsumed + && JsonHelpers.IsFinite(value))) { - // NETCOREAPP implementation of the TryParse method above permits case-insenstive variants of the - // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation. - // The following logic reconciles the two implementations to enforce consistent behavior. - if (JsonHelpers.IsFinite(value)) - { - return value; - } + ThrowHelper.ThrowFormatException(NumericType.Single); } - throw ThrowHelper.GetFormatException(NumericType.Single); + return value; } internal float GetSingleFloatingPointConstant() { ReadOnlySpan span = GetUnescapedSpan(); - if (JsonReaderHelper.TryGetFloatingPointConstant(span, out float value)) + if (!JsonReaderHelper.TryGetFloatingPointConstant(span, out float value)) { - return value; + ThrowHelper.ThrowFormatException(NumericType.Single); } - throw ThrowHelper.GetFormatException(NumericType.Single); + return value; } /// @@ -467,7 +463,7 @@ public double GetDouble() { if (!TryGetDouble(out double value)) { - throw ThrowHelper.GetFormatException(NumericType.Double); + ThrowHelper.ThrowFormatException(NumericType.Double); } return value; } @@ -481,31 +477,29 @@ internal double GetDoubleWithQuotes() return value; } - if (Utf8Parser.TryParse(span, out value, out int bytesConsumed) - && span.Length == bytesConsumed) + // NETCOREAPP implementation of the TryParse method above permits case-insensitive variants of the + // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation. + // The following logic reconciles the two implementations to enforce consistent behavior. + if (!(Utf8Parser.TryParse(span, out value, out int bytesConsumed) + && span.Length == bytesConsumed + && JsonHelpers.IsFinite(value))) { - // NETCOREAPP implementation of the TryParse method above permits case-insenstive variants of the - // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation. - // The following logic reconciles the two implementations to enforce consistent behavior. - if (JsonHelpers.IsFinite(value)) - { - return value; - } + ThrowHelper.ThrowFormatException(NumericType.Double); } - throw ThrowHelper.GetFormatException(NumericType.Double); + return value; } internal double GetDoubleFloatingPointConstant() { ReadOnlySpan span = GetUnescapedSpan(); - if (JsonReaderHelper.TryGetFloatingPointConstant(span, out double value)) + if (!JsonReaderHelper.TryGetFloatingPointConstant(span, out double value)) { - return value; + ThrowHelper.ThrowFormatException(NumericType.Double); } - throw ThrowHelper.GetFormatException(NumericType.Double); + return value; } /// @@ -526,7 +520,7 @@ public decimal GetDecimal() { if (!TryGetDecimal(out decimal value)) { - throw ThrowHelper.GetFormatException(NumericType.Decimal); + ThrowHelper.ThrowFormatException(NumericType.Decimal); } return value; } @@ -536,7 +530,7 @@ internal decimal GetDecimalWithQuotes() ReadOnlySpan span = GetUnescapedSpan(); if (!TryGetDecimalCore(out decimal value, span)) { - throw ThrowHelper.GetFormatException(NumericType.Decimal); + ThrowHelper.ThrowFormatException(NumericType.Decimal); } return value; } @@ -558,7 +552,7 @@ public DateTime GetDateTime() { if (!TryGetDateTime(out DateTime value)) { - throw ThrowHelper.GetFormatException(DataType.DateTime); + ThrowHelper.ThrowFormatException(DataType.DateTime); } return value; @@ -568,7 +562,7 @@ internal DateTime GetDateTimeNoValidation() { if (!TryGetDateTimeCore(out DateTime value)) { - throw ThrowHelper.GetFormatException(DataType.DateTime); + ThrowHelper.ThrowFormatException(DataType.DateTime); } return value; @@ -591,7 +585,7 @@ public DateTimeOffset GetDateTimeOffset() { if (!TryGetDateTimeOffset(out DateTimeOffset value)) { - throw ThrowHelper.GetFormatException(DataType.DateTimeOffset); + ThrowHelper.ThrowFormatException(DataType.DateTimeOffset); } return value; @@ -601,7 +595,7 @@ internal DateTimeOffset GetDateTimeOffsetNoValidation() { if (!TryGetDateTimeOffsetCore(out DateTimeOffset value)) { - throw ThrowHelper.GetFormatException(DataType.DateTimeOffset); + ThrowHelper.ThrowFormatException(DataType.DateTimeOffset); } return value; @@ -624,7 +618,7 @@ public Guid GetGuid() { if (!TryGetGuid(out Guid value)) { - throw ThrowHelper.GetFormatException(DataType.Guid); + ThrowHelper.ThrowFormatException(DataType.Guid); } return value; @@ -634,7 +628,7 @@ internal Guid GetGuidNoValidation() { if (!TryGetGuidCore(out Guid value)) { - throw ThrowHelper.GetFormatException(DataType.Guid); + ThrowHelper.ThrowFormatException(DataType.Guid); } return value; @@ -654,7 +648,7 @@ public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value) { if (TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -684,7 +678,7 @@ public bool TryGetByte(out byte value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -720,7 +714,7 @@ public bool TryGetSByte(out sbyte value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -755,7 +749,7 @@ public bool TryGetInt16(out short value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -790,7 +784,7 @@ public bool TryGetInt32(out int value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -825,7 +819,7 @@ public bool TryGetInt64(out long value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -861,7 +855,7 @@ public bool TryGetUInt16(out ushort value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -897,7 +891,7 @@ public bool TryGetUInt32(out uint value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -933,7 +927,7 @@ public bool TryGetUInt64(out ulong value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -968,7 +962,7 @@ public bool TryGetSingle(out float value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -998,7 +992,7 @@ public bool TryGetDouble(out double value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -1028,7 +1022,7 @@ public bool TryGetDecimal(out decimal value) { if (TokenType != JsonTokenType.Number) { - throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; @@ -1063,7 +1057,7 @@ public bool TryGetDateTime(out DateTime value) { if (TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(TokenType); } return TryGetDateTimeCore(out value); @@ -1131,7 +1125,7 @@ public bool TryGetDateTimeOffset(out DateTimeOffset value) { if (TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(TokenType); } return TryGetDateTimeOffsetCore(out value); @@ -1200,7 +1194,7 @@ public bool TryGetGuid(out Guid value) { if (TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(TokenType); } return TryGetGuidCore(out value); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index efafcf92b4ed2..42e561d89b0f4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -304,7 +304,7 @@ public void Skip() { if (!_isFinalBlock) { - throw ThrowHelper.GetInvalidOperationException_CannotSkipOnPartial(); + ThrowHelper.ThrowInvalidOperationException_CannotSkipOnPartial(); } SkipHelper(); @@ -429,8 +429,9 @@ public bool ValueTextEquals(ReadOnlySpan utf8Text) { if (!IsTokenTypeString(TokenType)) { - throw ThrowHelper.GetInvalidOperationException_ExpectedStringComparison(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedStringComparison(TokenType); } + return TextEqualsHelper(utf8Text); } @@ -499,7 +500,7 @@ public bool ValueTextEquals(ReadOnlySpan text) { if (!IsTokenTypeString(TokenType)) { - throw ThrowHelper.GetInvalidOperationException_ExpectedStringComparison(TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedStringComparison(TokenType); } if (MatchNotPossible(text.Length)) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/BooleanConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/BooleanConverter.cs index 488769d49f10f..1345659c0f64f 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/BooleanConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/BooleanConverter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Buffers.Text; +using System.Diagnostics; namespace System.Text.Json.Serialization.Converters { @@ -20,13 +21,13 @@ public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOpti internal override bool ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { ReadOnlySpan propertyName = reader.GetSpan(); - if (Utf8Parser.TryParse(propertyName, out bool value, out int bytesConsumed) - && propertyName.Length == bytesConsumed) + if (!(Utf8Parser.TryParse(propertyName, out bool value, out int bytesConsumed) + && propertyName.Length == bytesConsumed)) { - return value; + ThrowHelper.ThrowFormatException(DataType.Boolean); } - throw ThrowHelper.GetFormatException(DataType.Boolean); + return value; } internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, bool value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/CharConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/CharConverter.cs index e4242836fe091..5a33044f99dc4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/CharConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/CharConverter.cs @@ -12,7 +12,7 @@ public override char Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer string? str = reader.GetString(); if (string.IsNullOrEmpty(str) || str.Length > 1) { - throw ThrowHelper.GetInvalidOperationException_ExpectedChar(reader.TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType); } return str[0]; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs index ddb3252c9be43..168c439ecfc90 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs @@ -17,7 +17,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso { if (reader.TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(reader.TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); } bool isEscaped = reader._stringHasEscaping; @@ -32,7 +32,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso if (!JsonHelpers.IsInRangeInclusive(sequenceLength, MinimumTimeSpanFormatLength, maximumLength)) { - throw ThrowHelper.GetFormatException(DataType.TimeSpan); + ThrowHelper.ThrowFormatException(DataType.TimeSpan); } Span stackSpan = stackalloc byte[isEscaped ? MaximumEscapedTimeSpanFormatLength : MaximumTimeSpanFormatLength]; @@ -45,7 +45,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso if (!JsonHelpers.IsInRangeInclusive(source.Length, MinimumTimeSpanFormatLength, maximumLength)) { - throw ThrowHelper.GetFormatException(DataType.TimeSpan); + ThrowHelper.ThrowFormatException(DataType.TimeSpan); } } @@ -68,7 +68,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso { // Note: Utf8Parser.TryParse allows for leading whitespace so we // need to exclude that case here. - throw ThrowHelper.GetFormatException(DataType.TimeSpan); + ThrowHelper.ThrowFormatException(DataType.TimeSpan); } bool result = Utf8Parser.TryParse(source, out TimeSpan tmpValue, out int bytesConsumed, 'c'); @@ -78,12 +78,12 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso // "1$$$$$$$$$$". We need to check bytesConsumed to know if the // entire source was actually valid. - if (result && source.Length == bytesConsumed) + if (!result || source.Length != bytesConsumed) { - return tmpValue; + ThrowHelper.ThrowFormatException(DataType.TimeSpan); } - throw ThrowHelper.GetFormatException(DataType.TimeSpan); + return tmpValue; } public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs index f4bd6dff6d9ec..b1c15a6d913eb 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs @@ -21,7 +21,7 @@ public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, Json { if (reader.TokenType != JsonTokenType.String) { - throw ThrowHelper.GetInvalidOperationException_ExpectedString(reader.TokenType); + ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); } #if BUILDING_INBOX_LIBRARY @@ -33,7 +33,7 @@ public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, Json { if (!JsonHelpers.IsInRangeInclusive(reader.ValueSequence.Length, MinimumVersionLength, maxLength)) { - throw ThrowHelper.GetFormatException(DataType.Version); + ThrowHelper.ThrowFormatException(DataType.Version); } Span stackSpan = stackalloc byte[isEscaped ? MaximumEscapedVersionLength : MaximumVersionLength]; @@ -46,7 +46,7 @@ public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, Json if (!JsonHelpers.IsInRangeInclusive(source.Length, MinimumVersionLength, maxLength)) { - throw ThrowHelper.GetFormatException(DataType.Version); + ThrowHelper.ThrowFormatException(DataType.Version); } } @@ -72,7 +72,7 @@ public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, Json // we need to make sure that our input doesn't have them, // and if it has - we need to throw, to match behaviour of other converters // since Version.TryParse allows them and silently parses input to Version - throw ThrowHelper.GetFormatException(DataType.Version); + ThrowHelper.ThrowFormatException(DataType.Version); } Span charBuffer = stackalloc char[MaximumVersionLength]; @@ -89,7 +89,7 @@ public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, Json // we need to make sure that our input doesn't have them, // and if it has - we need to throw, to match behaviour of other converters // since Version.TryParse allows them and silently parses input to Version - throw ThrowHelper.GetFormatException(DataType.Version); + ThrowHelper.ThrowFormatException(DataType.Version); } if (Version.TryParse(versionString, out Version? result)) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs index 4ddf4db675d42..9b280c2904a81 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs @@ -433,7 +433,7 @@ public int MaxDepth if (value < 0) { - throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); } _maxDepth = value; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs index aac78c005c3f9..364c100282412 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs @@ -9,62 +9,54 @@ namespace System.Text.Json internal static partial class ThrowHelper { [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException_NodeValueNotAllowed(string paramName) { throw new ArgumentException(SR.NodeValueNotAllowed, paramName); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException_NodeArrayTooSmall(string paramName) { throw new ArgumentException(SR.NodeArrayTooSmall, paramName); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeException_NodeArrayIndexNegative(string paramName) { throw new ArgumentOutOfRangeException(paramName, SR.NodeArrayIndexNegative); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException_DuplicateKey(string propertyName) { throw new ArgumentException(SR.NodeDuplicateKey, propertyName); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NodeAlreadyHasParent() { throw new InvalidOperationException(SR.NodeAlreadyHasParent); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NodeCycleDetected() { throw new InvalidOperationException(SR.NodeCycleDetected); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray() { throw new InvalidOperationException(SR.NodeElementCannotBeObjectOrArray); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_NodeCollectionIsReadOnly() { - throw NotSupportedException_NodeCollectionIsReadOnly(); + throw GetNotSupportedException_NodeCollectionIsReadOnly(); } - public static NotSupportedException NotSupportedException_NodeCollectionIsReadOnly() + public static NotSupportedException GetNotSupportedException_NodeCollectionIsReadOnly() { return new NotSupportedException(SR.NodeCollectionIsReadOnly); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs index a75f5eaa48406..7036df5eb122e 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs @@ -14,91 +14,78 @@ namespace System.Text.Json internal static partial class ThrowHelper { [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException_DeserializeWrongType(Type type, object value) { throw new ArgumentException(SR.Format(SR.DeserializeWrongType, type, value.GetType())); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_SerializationNotSupported(Type propertyType) { throw new NotSupportedException(SR.Format(SR.SerializationNotSupportedType, propertyType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_TypeRequiresAsyncSerialization(Type propertyType) { throw new NotSupportedException(SR.Format(SR.TypeRequiresAsyncSerialization, propertyType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_ConstructorMaxOf64Parameters(Type type) { throw new NotSupportedException(SR.Format(SR.ConstructorMaxOf64Parameters, type)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type keyType, JsonConverter converter) { throw new NotSupportedException(SR.Format(SR.DictionaryKeyTypeNotSupported, keyType, converter.GetType())); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType) { throw new JsonException(SR.Format(SR.DeserializeUnableToConvertValue, propertyType)) { AppendPathInformation = true }; } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidCastException_DeserializeUnableToAssignValue(Type typeOfValue, Type declaredType) { throw new InvalidCastException(SR.Format(SR.DeserializeUnableToAssignValue, typeOfValue, declaredType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_DeserializeUnableToAssignNull(Type declaredType) { throw new InvalidOperationException(SR.Format(SR.DeserializeUnableToAssignNull, declaredType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_SerializationConverterRead(JsonConverter? converter) { throw new JsonException(SR.Format(SR.SerializationConverterRead, converter)) { AppendPathInformation = true }; } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_SerializationConverterWrite(JsonConverter? converter) { throw new JsonException(SR.Format(SR.SerializationConverterWrite, converter)) { AppendPathInformation = true }; } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_SerializerCycleDetected(int maxDepth) { throw new JsonException(SR.Format(SR.SerializerCycleDetected, maxDepth)) { AppendPathInformation = true }; } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException(string? message = null) { throw new JsonException(message) { AppendPathInformation = true }; } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_CannotSerializeInvalidType(Type type, Type? parentClassType, MemberInfo? memberInfo) { if (parentClassType == null) @@ -112,14 +99,12 @@ public static void ThrowInvalidOperationException_CannotSerializeInvalidType(Typ } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationConverterNotCompatible(Type converterType, Type type) { throw new InvalidOperationException(SR.Format(SR.SerializationConverterNotCompatible, converterType, type)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationConverterOnAttributeInvalid(Type classType, MemberInfo? memberInfo) { string location = classType.ToString(); @@ -132,7 +117,6 @@ public static void ThrowInvalidOperationException_SerializationConverterOnAttrib } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(Type classTypeAttributeIsOn, MemberInfo? memberInfo, Type typeToConvert) { string location = classTypeAttributeIsOn.ToString(); @@ -146,7 +130,6 @@ public static void ThrowInvalidOperationException_SerializationConverterOnAttrib } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializerOptionsImmutable(JsonSerializerContext? context) { string message = context == null @@ -157,21 +140,18 @@ public static void ThrowInvalidOperationException_SerializerOptionsImmutable(Jso } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializerPropertyNameConflict(Type type, JsonPropertyInfo jsonPropertyInfo) { throw new InvalidOperationException(SR.Format(SR.SerializerPropertyNameConflict, type, jsonPropertyInfo.ClrName)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializerPropertyNameNull(Type parentType, JsonPropertyInfo jsonPropertyInfo) { throw new InvalidOperationException(SR.Format(SR.SerializerPropertyNameNull, parentType, jsonPropertyInfo.MemberInfo?.Name)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NamingPolicyReturnNull(JsonNamingPolicy namingPolicy) { throw new InvalidOperationException(SR.Format(SR.NamingPolicyReturnNull, namingPolicy)); @@ -190,7 +170,6 @@ public static void ThrowInvalidOperationException_SerializerConverterFactoryRetu } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_MultiplePropertiesBindToConstructorParameters( Type parentType, string parameterName, @@ -207,35 +186,30 @@ public static void ThrowInvalidOperationException_MultiplePropertiesBindToConstr } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(Type parentType) { throw new InvalidOperationException(SR.Format(SR.ConstructorParamIncompleteBinding, parentType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_ExtensionDataCannotBindToCtorParam(JsonPropertyInfo jsonPropertyInfo) { throw new InvalidOperationException(SR.Format(SR.ExtensionDataCannotBindToCtorParam, jsonPropertyInfo.ClrName, jsonPropertyInfo.DeclaringType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_JsonIncludeOnNonPublicInvalid(string memberName, Type declaringType) { throw new InvalidOperationException(SR.Format(SR.JsonIncludeOnNonPublicInvalid, memberName, declaringType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_IgnoreConditionOnValueTypeInvalid(string clrPropertyName, Type propertyDeclaringType) { throw new InvalidOperationException(SR.Format(SR.IgnoreConditionOnValueTypeInvalid, clrPropertyName, propertyDeclaringType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(JsonPropertyInfo jsonPropertyInfo) { MemberInfo? memberInfo = jsonPropertyInfo.MemberInfo; @@ -246,14 +220,12 @@ public static void ThrowInvalidOperationException_NumberHandlingOnPropertyInvali } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_ConverterCanConvertMultipleTypes(Type runtimePropertyType, JsonConverter jsonConverter) { throw new InvalidOperationException(SR.Format(SR.ConverterCanConvertMultipleTypes, jsonConverter.GetType(), jsonConverter.TypeToConvert, runtimePropertyType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotHonored( ReadOnlySpan propertyName, ref Utf8JsonReader reader, @@ -267,7 +239,6 @@ public static void ThrowNotSupportedException_ObjectWithParameterizedCtorRefMeta } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ReThrowWithPath(ref ReadStack state, JsonReaderException ex) { Debug.Assert(ex.Path == null); @@ -294,7 +265,6 @@ public static void ReThrowWithPath(ref ReadStack state, JsonReaderException ex) } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ReThrowWithPath(ref ReadStack state, in Utf8JsonReader reader, Exception ex) { JsonException jsonException = new JsonException(null, ex); @@ -338,7 +308,6 @@ public static void AddJsonExceptionInformation(ref ReadStack state, in Utf8JsonR } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ReThrowWithPath(ref WriteStack state, Exception ex) { JsonException jsonException = new JsonException(null, ex); @@ -369,7 +338,6 @@ public static void AddJsonExceptionInformation(ref WriteStack state, JsonExcepti } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationDuplicateAttribute(Type attribute, Type classType, MemberInfo? memberInfo) { string location = classType.ToString(); @@ -382,21 +350,18 @@ public static void ThrowInvalidOperationException_SerializationDuplicateAttribut } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationDuplicateTypeAttribute(Type classType, Type attribute) { throw new InvalidOperationException(SR.Format(SR.SerializationDuplicateTypeAttribute, classType, attribute)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationDuplicateTypeAttribute(Type classType) { throw new InvalidOperationException(SR.Format(SR.SerializationDuplicateTypeAttribute, classType, typeof(TAttribute))); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_SerializationDataExtensionPropertyInvalid(Type type, JsonPropertyInfo jsonPropertyInfo) { throw new InvalidOperationException(SR.Format(SR.SerializationDataExtensionPropertyInvalid, type, jsonPropertyInfo.MemberInfo?.Name)); @@ -465,7 +430,6 @@ public static void ThrowNotSupportedException(ref WriteStack state, NotSupported } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_DeserializeNoConstructor(Type type, ref Utf8JsonReader reader, ref ReadStack state) { string message; @@ -483,42 +447,36 @@ public static void ThrowNotSupportedException_DeserializeNoConstructor(Type type } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_CannotPopulateCollection(Type type, ref Utf8JsonReader reader, ref ReadStack state) { ThrowNotSupportedException(ref state, reader, new NotSupportedException(SR.Format(SR.CannotPopulateCollection, type))); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataValuesInvalidToken(JsonTokenType tokenType) { ThrowJsonException(SR.Format(SR.MetadataInvalidTokenAfterValues, tokenType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataReferenceNotFound(string id) { ThrowJsonException(SR.Format(SR.MetadataReferenceNotFound, id)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataValueWasNotString(JsonTokenType tokenType) { ThrowJsonException(SR.Format(SR.MetadataValueWasNotString, tokenType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataValueWasNotString(JsonValueKind valueKind) { ThrowJsonException(SR.Format(SR.MetadataValueWasNotString, valueKind)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(ReadOnlySpan propertyName, ref ReadStack state) { state.Current.JsonPropertyName = propertyName.ToArray(); @@ -526,14 +484,12 @@ public static void ThrowJsonException_MetadataReferenceObjectCannotContainOtherP } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties() { ThrowJsonException(SR.MetadataReferenceCannotContainOtherProperties); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataIdIsNotFirstProperty(ReadOnlySpan propertyName, ref ReadStack state) { state.Current.JsonPropertyName = propertyName.ToArray(); @@ -541,7 +497,6 @@ public static void ThrowJsonException_MetadataIdIsNotFirstProperty(ReadOnlySpan< } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataMissingIdBeforeValues(ref ReadStack state, ReadOnlySpan propertyName) { state.Current.JsonPropertyName = propertyName.ToArray(); @@ -549,7 +504,6 @@ public static void ThrowJsonException_MetadataMissingIdBeforeValues(ref ReadStac } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataInvalidPropertyWithLeadingDollarSign(ReadOnlySpan propertyName, ref ReadStack state, in Utf8JsonReader reader) { // Set PropertyInfo or KeyName to write down the conflicting property name in JsonException.Path @@ -566,21 +520,18 @@ public static void ThrowJsonException_MetadataInvalidPropertyWithLeadingDollarSi } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataDuplicateIdFound(string id) { ThrowJsonException(SR.Format(SR.MetadataDuplicateIdFound, id)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataInvalidReferenceToValueType(Type propertyType) { ThrowJsonException(SR.Format(SR.MetadataInvalidReferenceToValueType, propertyType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataPreservedArrayInvalidProperty(ref ReadStack state, Type propertyType, in Utf8JsonReader reader) { state.Current.JsonPropertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray(); @@ -592,7 +543,6 @@ public static void ThrowJsonException_MetadataPreservedArrayInvalidProperty(ref } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref ReadStack state, Type propertyType) { // Missing $values, JSON path should point to the property's object. @@ -604,14 +554,12 @@ public static void ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref R } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(Type propertyType) { ThrowJsonException(SR.Format(SR.MetadataCannotParsePreservedObjectToImmutable, propertyType)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_MetadataReferenceOfTypeCannotBeAssignedToType(string referenceId, Type currentType, Type typeToConvert) { throw new InvalidOperationException(SR.Format(SR.MetadataReferenceOfTypeCannotBeAssignedToType, referenceId, currentType, typeToConvert)); @@ -644,35 +592,30 @@ internal static void ThrowUnexpectedMetadataException( } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_JsonSerializerOptionsAlreadyBoundToContext() { throw new InvalidOperationException(SR.Format(SR.OptionsAlreadyBoundToContext)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_BuiltInConvertersNotRooted(Type type) { throw new NotSupportedException(SR.Format(SR.BuiltInConvertersNotRooted, type)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotSupportedException_NoMetadataForType(Type type) { throw new NotSupportedException(SR.Format(SR.NoMetadataForType, type)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_NoMetadataForType(Type type) { throw new InvalidOperationException(SR.Format(SR.NoMetadataForType, type)); } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowInvalidOperationException_MetadatInitFuncsNull() { throw new InvalidOperationException(SR.Format(SR.MetadataInitFuncsNull)); @@ -694,7 +637,6 @@ public static void ThrowInvalidOperationException_NoDefaultOptionsForContext(Jso } [DoesNotReturn] - [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowMissingMemberException_MissingFSharpCoreMember(string missingFsharpCoreMember) { throw new MissingMemberException(SR.Format(SR.MissingFSharpCoreMember, missingFsharpCoreMember)); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs index 2721f685ae4dc..e5cd6d024ee1a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs @@ -12,23 +12,23 @@ internal static partial class ThrowHelper // If the exception source is this value, the serializer will re-throw as JsonException. public const string ExceptionSourceValueToRethrowAsJsonException = "System.Text.Json.Rethrowable"; - public static ArgumentOutOfRangeException GetArgumentOutOfRangeException_MaxDepthMustBePositive(string parameterName) + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(string parameterName) { - return GetArgumentOutOfRangeException(parameterName, SR.MaxDepthMustBePositive); + throw GetArgumentOutOfRangeException(parameterName, SR.MaxDepthMustBePositive); } - [MethodImpl(MethodImplOptions.NoInlining)] private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(string parameterName, string message) { return new ArgumentOutOfRangeException(parameterName, message); } - public static ArgumentOutOfRangeException GetArgumentOutOfRangeException_CommentEnumMustBeInRange(string parameterName) + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeException_CommentEnumMustBeInRange(string parameterName) { - return GetArgumentOutOfRangeException(parameterName, SR.CommentHandlingMustBeValid); + throw GetArgumentOutOfRangeException(parameterName, SR.CommentHandlingMustBeValid); } - [MethodImpl(MethodImplOptions.NoInlining)] private static ArgumentException GetArgumentException(string message) { return new ArgumentException(message); @@ -154,12 +154,9 @@ public static void ThrowInvalidOperationException(string message) throw GetInvalidOperationException(message); } - [MethodImpl(MethodImplOptions.NoInlining)] private static InvalidOperationException GetInvalidOperationException(string message) { - var ex = new InvalidOperationException(message); - ex.Source = ExceptionSourceValueToRethrowAsJsonException; - return ex; + return new InvalidOperationException(message) { Source = ExceptionSourceValueToRethrowAsJsonException }; } [DoesNotReturn] @@ -168,7 +165,6 @@ public static void ThrowInvalidOperationException_DepthNonZeroOrEmptyJson(int cu throw GetInvalidOperationException(currentDepth); } - [MethodImpl(MethodImplOptions.NoInlining)] private static InvalidOperationException GetInvalidOperationException(int currentDepth) { currentDepth &= JsonConstants.RemoveFlagsBitMask; @@ -207,66 +203,60 @@ public static InvalidOperationException GetInvalidOperationException_ExpectedObj return GetInvalidOperationException("object", tokenType); } - public static InvalidOperationException GetInvalidOperationException_ExpectedNumber(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedNumber(JsonTokenType tokenType) { - return GetInvalidOperationException("number", tokenType); + throw GetInvalidOperationException("number", tokenType); } - public static InvalidOperationException GetInvalidOperationException_ExpectedBoolean(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedBoolean(JsonTokenType tokenType) { - return GetInvalidOperationException("boolean", tokenType); + throw GetInvalidOperationException("boolean", tokenType); } - public static InvalidOperationException GetInvalidOperationException_ExpectedString(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType) { - return GetInvalidOperationException("string", tokenType); + throw GetInvalidOperationException("string", tokenType); } - public static InvalidOperationException GetInvalidOperationException_ExpectedStringComparison(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedStringComparison(JsonTokenType tokenType) { - return GetInvalidOperationException(tokenType); + throw GetInvalidOperationException(tokenType); } - public static InvalidOperationException GetInvalidOperationException_ExpectedComment(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedComment(JsonTokenType tokenType) { - return GetInvalidOperationException("comment", tokenType); + throw GetInvalidOperationException("comment", tokenType); } - [MethodImpl(MethodImplOptions.NoInlining)] - public static InvalidOperationException GetInvalidOperationException_CannotSkipOnPartial() + [DoesNotReturn] + public static void ThrowInvalidOperationException_CannotSkipOnPartial() { - return GetInvalidOperationException(SR.CannotSkip); + throw GetInvalidOperationException(SR.CannotSkip); } - [MethodImpl(MethodImplOptions.NoInlining)] private static InvalidOperationException GetInvalidOperationException(string message, JsonTokenType tokenType) { return GetInvalidOperationException(SR.Format(SR.InvalidCast, tokenType, message)); } - [MethodImpl(MethodImplOptions.NoInlining)] private static InvalidOperationException GetInvalidOperationException(JsonTokenType tokenType) { return GetInvalidOperationException(SR.Format(SR.InvalidComparison, tokenType)); } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static InvalidOperationException GetJsonElementWrongTypeException( + [DoesNotReturn] + internal static void ThrowJsonElementWrongTypeException( JsonTokenType expectedType, JsonTokenType actualType) { - return GetJsonElementWrongTypeException(expectedType.ToValueKind(), actualType.ToValueKind()); + throw GetJsonElementWrongTypeException(expectedType.ToValueKind(), actualType.ToValueKind()); } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static InvalidOperationException GetJsonElementWrongTypeException( - string expectedTypeName, - JsonTokenType actualType) - { - return GetJsonElementWrongTypeException(expectedTypeName, actualType.ToValueKind()); - } - - [MethodImpl(MethodImplOptions.NoInlining)] internal static InvalidOperationException GetJsonElementWrongTypeException( JsonValueKind expectedType, JsonValueKind actualType) @@ -275,7 +265,6 @@ internal static InvalidOperationException GetJsonElementWrongTypeException( SR.Format(SR.JsonElementHasWrongType, expectedType, actualType)); } - [MethodImpl(MethodImplOptions.NoInlining)] internal static InvalidOperationException GetJsonElementWrongTypeException( string expectedTypeName, JsonValueKind actualType) @@ -563,15 +552,13 @@ private static string GetResourceString(ExceptionResource resource, int currentD return message; } - public static FormatException GetFormatException() + [DoesNotReturn] + public static void ThrowFormatException() { - var ex = new FormatException(); - ex.Source = ExceptionSourceValueToRethrowAsJsonException; - return ex; + throw new FormatException { Source = ExceptionSourceValueToRethrowAsJsonException }; } - [MethodImpl(MethodImplOptions.NoInlining)] - public static FormatException GetFormatException(NumericType numericType) + public static void ThrowFormatException(NumericType numericType) { string message = ""; @@ -615,13 +602,11 @@ public static FormatException GetFormatException(NumericType numericType) break; } - var ex = new FormatException(message); - ex.Source = ExceptionSourceValueToRethrowAsJsonException; - return ex; + throw new FormatException(message) { Source = ExceptionSourceValueToRethrowAsJsonException }; } - [MethodImpl(MethodImplOptions.NoInlining)] - public static FormatException GetFormatException(DataType dateType) + [DoesNotReturn] + public static void ThrowFormatException(DataType dateType) { string message = ""; @@ -653,14 +638,13 @@ public static FormatException GetFormatException(DataType dateType) break; } - var ex = new FormatException(message); - ex.Source = ExceptionSourceValueToRethrowAsJsonException; - return ex; + throw new FormatException(message) { Source = ExceptionSourceValueToRethrowAsJsonException }; } - public static InvalidOperationException GetInvalidOperationException_ExpectedChar(JsonTokenType tokenType) + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedChar(JsonTokenType tokenType) { - return GetInvalidOperationException("char", tokenType); + throw GetInvalidOperationException("char", tokenType); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs index e71668b854323..2d89a261e3353 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs @@ -58,7 +58,9 @@ public int MaxDepth set { if (value < 0) - throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + { + ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); + } _maxDepth = value; }