Skip to content

Commit 56b5df4

Browse files
[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
1 parent c03a89f commit 56b5df4

17 files changed

+200
-272
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ private void CheckExpectedType(JsonTokenType expected, JsonTokenType actual)
11071107
{
11081108
if (expected != actual)
11091109
{
1110-
throw ThrowHelper.GetJsonElementWrongTypeException(expected, actual);
1110+
ThrowHelper.ThrowJsonElementWrongTypeException(expected, actual);
11111111
}
11121112
}
11131113

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public int MaxDepth
5252
set
5353
{
5454
if (value < 0)
55-
throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
55+
{
56+
ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
57+
}
5658

5759
_maxDepth = value;
5860
}

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs

+42-36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Runtime.CompilerServices;
78

89
namespace System.Text.Json
910
{
@@ -333,7 +334,12 @@ public bool GetBoolean()
333334
return
334335
type == JsonTokenType.True ? true :
335336
type == JsonTokenType.False ? false :
336-
throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), type);
337+
ThrowJsonElementWrongTypeException(type);
338+
339+
static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType)
340+
{
341+
throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind());
342+
}
337343
}
338344

339345
/// <summary>
@@ -400,12 +406,12 @@ public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
400406
/// <seealso cref="ToString"/>
401407
public byte[] GetBytesFromBase64()
402408
{
403-
if (TryGetBytesFromBase64(out byte[]? value))
409+
if (!TryGetBytesFromBase64(out byte[]? value))
404410
{
405-
return value;
411+
ThrowHelper.ThrowFormatException();
406412
}
407413

408-
throw ThrowHelper.GetFormatException();
414+
return value;
409415
}
410416

411417
/// <summary>
@@ -645,12 +651,12 @@ public bool TryGetInt32(out int value)
645651
/// </exception>
646652
public int GetInt32()
647653
{
648-
if (TryGetInt32(out int value))
654+
if (!TryGetInt32(out int value))
649655
{
650-
return value;
656+
ThrowHelper.ThrowFormatException();
651657
}
652658

653-
throw ThrowHelper.GetFormatException();
659+
return value;
654660
}
655661

656662
/// <summary>
@@ -697,12 +703,12 @@ public bool TryGetUInt32(out uint value)
697703
[CLSCompliant(false)]
698704
public uint GetUInt32()
699705
{
700-
if (TryGetUInt32(out uint value))
706+
if (!TryGetUInt32(out uint value))
701707
{
702-
return value;
708+
ThrowHelper.ThrowFormatException();
703709
}
704710

705-
throw ThrowHelper.GetFormatException();
711+
return value;
706712
}
707713

708714
/// <summary>
@@ -747,12 +753,12 @@ public bool TryGetInt64(out long value)
747753
/// </exception>
748754
public long GetInt64()
749755
{
750-
if (TryGetInt64(out long value))
756+
if (!TryGetInt64(out long value))
751757
{
752-
return value;
758+
ThrowHelper.ThrowFormatException();
753759
}
754760

755-
throw ThrowHelper.GetFormatException();
761+
return value;
756762
}
757763

758764
/// <summary>
@@ -799,12 +805,12 @@ public bool TryGetUInt64(out ulong value)
799805
[CLSCompliant(false)]
800806
public ulong GetUInt64()
801807
{
802-
if (TryGetUInt64(out ulong value))
808+
if (!TryGetUInt64(out ulong value))
803809
{
804-
return value;
810+
ThrowHelper.ThrowFormatException();
805811
}
806812

807-
throw ThrowHelper.GetFormatException();
813+
return value;
808814
}
809815

810816
/// <summary>
@@ -866,12 +872,12 @@ public bool TryGetDouble(out double value)
866872
/// </exception>
867873
public double GetDouble()
868874
{
869-
if (TryGetDouble(out double value))
875+
if (!TryGetDouble(out double value))
870876
{
871-
return value;
877+
ThrowHelper.ThrowFormatException();
872878
}
873879

874-
throw ThrowHelper.GetFormatException();
880+
return value;
875881
}
876882

877883
/// <summary>
@@ -933,12 +939,12 @@ public bool TryGetSingle(out float value)
933939
/// </exception>
934940
public float GetSingle()
935941
{
936-
if (TryGetSingle(out float value))
942+
if (!TryGetSingle(out float value))
937943
{
938-
return value;
944+
ThrowHelper.ThrowFormatException();
939945
}
940946

941-
throw ThrowHelper.GetFormatException();
947+
return value;
942948
}
943949

944950
/// <summary>
@@ -985,12 +991,12 @@ public bool TryGetDecimal(out decimal value)
985991
/// <seealso cref="GetRawText"/>
986992
public decimal GetDecimal()
987993
{
988-
if (TryGetDecimal(out decimal value))
994+
if (!TryGetDecimal(out decimal value))
989995
{
990-
return value;
996+
ThrowHelper.ThrowFormatException();
991997
}
992998

993-
throw ThrowHelper.GetFormatException();
999+
return value;
9941000
}
9951001

9961002
/// <summary>
@@ -1036,12 +1042,12 @@ public bool TryGetDateTime(out DateTime value)
10361042
/// <seealso cref="ToString"/>
10371043
public DateTime GetDateTime()
10381044
{
1039-
if (TryGetDateTime(out DateTime value))
1045+
if (!TryGetDateTime(out DateTime value))
10401046
{
1041-
return value;
1047+
ThrowHelper.ThrowFormatException();
10421048
}
10431049

1044-
throw ThrowHelper.GetFormatException();
1050+
return value;
10451051
}
10461052

10471053
/// <summary>
@@ -1087,12 +1093,12 @@ public bool TryGetDateTimeOffset(out DateTimeOffset value)
10871093
/// <seealso cref="ToString"/>
10881094
public DateTimeOffset GetDateTimeOffset()
10891095
{
1090-
if (TryGetDateTimeOffset(out DateTimeOffset value))
1096+
if (!TryGetDateTimeOffset(out DateTimeOffset value))
10911097
{
1092-
return value;
1098+
ThrowHelper.ThrowFormatException();
10931099
}
10941100

1095-
throw ThrowHelper.GetFormatException();
1101+
return value;
10961102
}
10971103

10981104
/// <summary>
@@ -1138,12 +1144,12 @@ public bool TryGetGuid(out Guid value)
11381144
/// <seealso cref="ToString"/>
11391145
public Guid GetGuid()
11401146
{
1141-
if (TryGetGuid(out Guid value))
1147+
if (!TryGetGuid(out Guid value))
11421148
{
1143-
return value;
1149+
ThrowHelper.ThrowFormatException();
11441150
}
11451151

1146-
throw ThrowHelper.GetFormatException();
1152+
return value;
11471153
}
11481154

11491155
internal string GetPropertyName()
@@ -1326,7 +1332,7 @@ public ArrayEnumerator EnumerateArray()
13261332

13271333
if (tokenType != JsonTokenType.StartArray)
13281334
{
1329-
throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
1335+
ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
13301336
}
13311337

13321338
return new ArrayEnumerator(this);
@@ -1352,7 +1358,7 @@ public ObjectEnumerator EnumerateObject()
13521358

13531359
if (tokenType != JsonTokenType.StartObject)
13541360
{
1355-
throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
1361+
ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
13561362
}
13571363

13581364
return new ObjectEnumerator(this);

src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.KeyCollection.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator()
3636
}
3737
}
3838

39-
public void Add(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
39+
public void Add(string propertyName) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();
4040

41-
public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
41+
public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();
4242

4343
public bool Contains(string propertyName) => _parent.ContainsProperty(propertyName);
4444

@@ -68,7 +68,7 @@ public IEnumerator<string> GetEnumerator()
6868
}
6969
}
7070

71-
bool ICollection<string>.Remove(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
71+
bool ICollection<string>.Remove(string propertyName) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly();
7272
}
7373
}
7474
}

src/libraries/System.Text.Json/src/System/Text/Json/JsonPropertyDictionary.ValueCollection.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator()
3636
}
3737
}
3838

39-
public void Add(T? jsonNode) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
39+
public void Add(T? jsonNode) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();
4040

41-
public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
41+
public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();
4242

4343
public bool Contains(T? jsonNode) => _parent.ContainsValue(jsonNode);
4444

@@ -68,7 +68,7 @@ public void CopyTo(T?[] nodeArray, int index)
6868
}
6969
}
7070

71-
bool ICollection<T?>.Remove(T? node) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
71+
bool ICollection<T?>.Remove(T? node) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly();
7272
}
7373
}
7474
}

src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderOptions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public JsonCommentHandling CommentHandling
3131
{
3232
Debug.Assert(value >= 0);
3333
if (value > JsonCommentHandling.Allow)
34-
throw ThrowHelper.GetArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value));
34+
{
35+
ThrowHelper.ThrowArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value));
36+
}
3537

3638
_commentHandling = value;
3739
}
@@ -52,7 +54,9 @@ public int MaxDepth
5254
set
5355
{
5456
if (value < 0)
55-
throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
57+
{
58+
ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
59+
}
5660

5761
_maxDepth = value;
5862
}

0 commit comments

Comments
 (0)