4
4
using System . Collections . Generic ;
5
5
using System . Diagnostics ;
6
6
using System . Diagnostics . CodeAnalysis ;
7
+ using System . Runtime . CompilerServices ;
7
8
8
9
namespace System . Text . Json
9
10
{
@@ -333,7 +334,12 @@ public bool GetBoolean()
333
334
return
334
335
type == JsonTokenType . True ? true :
335
336
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
+ }
337
343
}
338
344
339
345
/// <summary>
@@ -400,12 +406,12 @@ public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
400
406
/// <seealso cref="ToString"/>
401
407
public byte [ ] GetBytesFromBase64 ( )
402
408
{
403
- if ( TryGetBytesFromBase64 ( out byte [ ] ? value ) )
409
+ if ( ! TryGetBytesFromBase64 ( out byte [ ] ? value ) )
404
410
{
405
- return value ;
411
+ ThrowHelper . ThrowFormatException ( ) ;
406
412
}
407
413
408
- throw ThrowHelper . GetFormatException ( ) ;
414
+ return value ;
409
415
}
410
416
411
417
/// <summary>
@@ -645,12 +651,12 @@ public bool TryGetInt32(out int value)
645
651
/// </exception>
646
652
public int GetInt32 ( )
647
653
{
648
- if ( TryGetInt32 ( out int value ) )
654
+ if ( ! TryGetInt32 ( out int value ) )
649
655
{
650
- return value ;
656
+ ThrowHelper . ThrowFormatException ( ) ;
651
657
}
652
658
653
- throw ThrowHelper . GetFormatException ( ) ;
659
+ return value ;
654
660
}
655
661
656
662
/// <summary>
@@ -697,12 +703,12 @@ public bool TryGetUInt32(out uint value)
697
703
[ CLSCompliant ( false ) ]
698
704
public uint GetUInt32 ( )
699
705
{
700
- if ( TryGetUInt32 ( out uint value ) )
706
+ if ( ! TryGetUInt32 ( out uint value ) )
701
707
{
702
- return value ;
708
+ ThrowHelper . ThrowFormatException ( ) ;
703
709
}
704
710
705
- throw ThrowHelper . GetFormatException ( ) ;
711
+ return value ;
706
712
}
707
713
708
714
/// <summary>
@@ -747,12 +753,12 @@ public bool TryGetInt64(out long value)
747
753
/// </exception>
748
754
public long GetInt64 ( )
749
755
{
750
- if ( TryGetInt64 ( out long value ) )
756
+ if ( ! TryGetInt64 ( out long value ) )
751
757
{
752
- return value ;
758
+ ThrowHelper . ThrowFormatException ( ) ;
753
759
}
754
760
755
- throw ThrowHelper . GetFormatException ( ) ;
761
+ return value ;
756
762
}
757
763
758
764
/// <summary>
@@ -799,12 +805,12 @@ public bool TryGetUInt64(out ulong value)
799
805
[ CLSCompliant ( false ) ]
800
806
public ulong GetUInt64 ( )
801
807
{
802
- if ( TryGetUInt64 ( out ulong value ) )
808
+ if ( ! TryGetUInt64 ( out ulong value ) )
803
809
{
804
- return value ;
810
+ ThrowHelper . ThrowFormatException ( ) ;
805
811
}
806
812
807
- throw ThrowHelper . GetFormatException ( ) ;
813
+ return value ;
808
814
}
809
815
810
816
/// <summary>
@@ -866,12 +872,12 @@ public bool TryGetDouble(out double value)
866
872
/// </exception>
867
873
public double GetDouble ( )
868
874
{
869
- if ( TryGetDouble ( out double value ) )
875
+ if ( ! TryGetDouble ( out double value ) )
870
876
{
871
- return value ;
877
+ ThrowHelper . ThrowFormatException ( ) ;
872
878
}
873
879
874
- throw ThrowHelper . GetFormatException ( ) ;
880
+ return value ;
875
881
}
876
882
877
883
/// <summary>
@@ -933,12 +939,12 @@ public bool TryGetSingle(out float value)
933
939
/// </exception>
934
940
public float GetSingle ( )
935
941
{
936
- if ( TryGetSingle ( out float value ) )
942
+ if ( ! TryGetSingle ( out float value ) )
937
943
{
938
- return value ;
944
+ ThrowHelper . ThrowFormatException ( ) ;
939
945
}
940
946
941
- throw ThrowHelper . GetFormatException ( ) ;
947
+ return value ;
942
948
}
943
949
944
950
/// <summary>
@@ -985,12 +991,12 @@ public bool TryGetDecimal(out decimal value)
985
991
/// <seealso cref="GetRawText"/>
986
992
public decimal GetDecimal ( )
987
993
{
988
- if ( TryGetDecimal ( out decimal value ) )
994
+ if ( ! TryGetDecimal ( out decimal value ) )
989
995
{
990
- return value ;
996
+ ThrowHelper . ThrowFormatException ( ) ;
991
997
}
992
998
993
- throw ThrowHelper . GetFormatException ( ) ;
999
+ return value ;
994
1000
}
995
1001
996
1002
/// <summary>
@@ -1036,12 +1042,12 @@ public bool TryGetDateTime(out DateTime value)
1036
1042
/// <seealso cref="ToString"/>
1037
1043
public DateTime GetDateTime ( )
1038
1044
{
1039
- if ( TryGetDateTime ( out DateTime value ) )
1045
+ if ( ! TryGetDateTime ( out DateTime value ) )
1040
1046
{
1041
- return value ;
1047
+ ThrowHelper . ThrowFormatException ( ) ;
1042
1048
}
1043
1049
1044
- throw ThrowHelper . GetFormatException ( ) ;
1050
+ return value ;
1045
1051
}
1046
1052
1047
1053
/// <summary>
@@ -1087,12 +1093,12 @@ public bool TryGetDateTimeOffset(out DateTimeOffset value)
1087
1093
/// <seealso cref="ToString"/>
1088
1094
public DateTimeOffset GetDateTimeOffset ( )
1089
1095
{
1090
- if ( TryGetDateTimeOffset ( out DateTimeOffset value ) )
1096
+ if ( ! TryGetDateTimeOffset ( out DateTimeOffset value ) )
1091
1097
{
1092
- return value ;
1098
+ ThrowHelper . ThrowFormatException ( ) ;
1093
1099
}
1094
1100
1095
- throw ThrowHelper . GetFormatException ( ) ;
1101
+ return value ;
1096
1102
}
1097
1103
1098
1104
/// <summary>
@@ -1138,12 +1144,12 @@ public bool TryGetGuid(out Guid value)
1138
1144
/// <seealso cref="ToString"/>
1139
1145
public Guid GetGuid ( )
1140
1146
{
1141
- if ( TryGetGuid ( out Guid value ) )
1147
+ if ( ! TryGetGuid ( out Guid value ) )
1142
1148
{
1143
- return value ;
1149
+ ThrowHelper . ThrowFormatException ( ) ;
1144
1150
}
1145
1151
1146
- throw ThrowHelper . GetFormatException ( ) ;
1152
+ return value ;
1147
1153
}
1148
1154
1149
1155
internal string GetPropertyName ( )
@@ -1326,7 +1332,7 @@ public ArrayEnumerator EnumerateArray()
1326
1332
1327
1333
if ( tokenType != JsonTokenType . StartArray )
1328
1334
{
1329
- throw ThrowHelper . GetJsonElementWrongTypeException ( JsonTokenType . StartArray , tokenType ) ;
1335
+ ThrowHelper . ThrowJsonElementWrongTypeException ( JsonTokenType . StartArray , tokenType ) ;
1330
1336
}
1331
1337
1332
1338
return new ArrayEnumerator ( this ) ;
@@ -1352,7 +1358,7 @@ public ObjectEnumerator EnumerateObject()
1352
1358
1353
1359
if ( tokenType != JsonTokenType . StartObject )
1354
1360
{
1355
- throw ThrowHelper . GetJsonElementWrongTypeException ( JsonTokenType . StartObject , tokenType ) ;
1361
+ ThrowHelper . ThrowJsonElementWrongTypeException ( JsonTokenType . StartObject , tokenType ) ;
1356
1362
}
1357
1363
1358
1364
return new ObjectEnumerator ( this ) ;
0 commit comments