@@ -113,14 +113,14 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
113
113
case JsonTokenType . Number when ( _converterOptions & EnumConverterOptions . AllowNumbers ) != 0 :
114
114
switch ( s_enumTypeCode )
115
115
{
116
- case TypeCode . Int32 when reader . TryGetInt32 ( out int int32 ) : return Unsafe . As < int , T > ( ref int32 ) ;
117
- case TypeCode . UInt32 when reader . TryGetUInt32 ( out uint uint32 ) : return Unsafe . As < uint , T > ( ref uint32 ) ;
118
- case TypeCode . Int64 when reader . TryGetInt64 ( out long int64 ) : return Unsafe . As < long , T > ( ref int64 ) ;
119
- case TypeCode . UInt64 when reader . TryGetUInt64 ( out ulong uint64 ) : return Unsafe . As < ulong , T > ( ref uint64 ) ;
120
- case TypeCode . Byte when reader . TryGetByte ( out byte ubyte8 ) : return Unsafe . As < byte , T > ( ref ubyte8 ) ;
121
- case TypeCode . SByte when reader . TryGetSByte ( out sbyte byte8 ) : return Unsafe . As < sbyte , T > ( ref byte8 ) ;
122
- case TypeCode . Int16 when reader . TryGetInt16 ( out short int16 ) : return Unsafe . As < short , T > ( ref int16 ) ;
123
- case TypeCode . UInt16 when reader . TryGetUInt16 ( out ushort uint16 ) : return Unsafe . As < ushort , T > ( ref uint16 ) ;
116
+ case TypeCode . Int32 when reader . TryGetInt32 ( out int int32 ) : return ( T ) ( object ) int32 ;
117
+ case TypeCode . UInt32 when reader . TryGetUInt32 ( out uint uint32 ) : return ( T ) ( object ) uint32 ;
118
+ case TypeCode . Int64 when reader . TryGetInt64 ( out long int64 ) : return ( T ) ( object ) int64 ;
119
+ case TypeCode . UInt64 when reader . TryGetUInt64 ( out ulong uint64 ) : return ( T ) ( object ) uint64 ;
120
+ case TypeCode . Byte when reader . TryGetByte ( out byte ubyte8 ) : return ( T ) ( object ) ubyte8 ;
121
+ case TypeCode . SByte when reader . TryGetSByte ( out sbyte byte8 ) : return ( T ) ( object ) byte8 ;
122
+ case TypeCode . Int16 when reader . TryGetInt16 ( out short int16 ) : return ( T ) ( object ) int16 ;
123
+ case TypeCode . UInt16 when reader . TryGetUInt16 ( out ushort uint16 ) : return ( T ) ( object ) uint16 ;
124
124
}
125
125
break ;
126
126
}
@@ -350,51 +350,43 @@ private bool TryParseNamedEnum(
350
350
351
351
private static ulong ConvertToUInt64 ( T value )
352
352
{
353
- switch ( s_enumTypeCode )
354
- {
355
- case TypeCode . Int32 or TypeCode . UInt32 : return Unsafe . As < T , uint > ( ref value ) ;
356
- case TypeCode . Int64 or TypeCode . UInt64 : return Unsafe . As < T , ulong > ( ref value ) ;
357
- case TypeCode . Int16 or TypeCode . UInt16 : return Unsafe . As < T , ushort > ( ref value ) ;
358
- default :
359
- Debug . Assert ( s_enumTypeCode is TypeCode . SByte or TypeCode . Byte ) ;
360
- return Unsafe . As < T , byte > ( ref value ) ;
353
+ return s_enumTypeCode switch
354
+ {
355
+ TypeCode . Int32 => ( ulong ) ( int ) ( object ) value ,
356
+ TypeCode . UInt32 => ( uint ) ( object ) value ,
357
+ TypeCode . Int64 => ( ulong ) ( long ) ( object ) value ,
358
+ TypeCode . UInt64 => ( ulong ) ( object ) value ,
359
+ TypeCode . Int16 => ( ulong ) ( short ) ( object ) value ,
360
+ TypeCode . UInt16 => ( ushort ) ( object ) value ,
361
+ TypeCode . SByte => ( ulong ) ( sbyte ) ( object ) value ,
362
+ _ => ( byte ) ( object ) value
361
363
} ;
362
364
}
363
365
364
366
private static long ConvertToInt64 ( T value )
365
367
{
366
368
Debug . Assert ( s_isSignedEnum ) ;
367
- switch ( s_enumTypeCode )
368
- {
369
- case TypeCode . Int32 : return Unsafe . As < T , int > ( ref value ) ;
370
- case TypeCode . Int64 : return Unsafe . As < T , long > ( ref value ) ;
371
- case TypeCode . Int16 : return Unsafe . As < T , short > ( ref value ) ;
372
- default :
373
- Debug . Assert ( s_enumTypeCode is TypeCode . SByte ) ;
374
- return Unsafe . As < T , sbyte > ( ref value ) ;
369
+ return s_enumTypeCode switch
370
+ {
371
+ TypeCode . Int32 => ( int ) ( object ) value ,
372
+ TypeCode . Int64 => ( long ) ( object ) value ,
373
+ TypeCode . Int16 => ( short ) ( object ) value ,
374
+ _ => ( sbyte ) ( object ) value ,
375
375
} ;
376
376
}
377
377
378
378
private static T ConvertFromUInt64 ( ulong value )
379
379
{
380
- switch ( s_enumTypeCode )
381
- {
382
- case TypeCode . Int32 or TypeCode . UInt32 :
383
- uint uintValue = ( uint ) value ;
384
- return Unsafe . As < uint , T > ( ref uintValue ) ;
385
-
386
- case TypeCode . Int64 or TypeCode . UInt64 :
387
- ulong ulongValue = value ;
388
- return Unsafe . As < ulong , T > ( ref ulongValue ) ;
389
-
390
- case TypeCode . Int16 or TypeCode . UInt16 :
391
- ushort ushortValue = ( ushort ) value ;
392
- return Unsafe . As < ushort , T > ( ref ushortValue ) ;
393
-
394
- default :
395
- Debug . Assert ( s_enumTypeCode is TypeCode . SByte or TypeCode . Byte ) ;
396
- byte byteValue = ( byte ) value ;
397
- return Unsafe . As < byte , T > ( ref byteValue ) ;
380
+ return s_enumTypeCode switch
381
+ {
382
+ TypeCode . Int32 => ( T ) ( object ) ( int ) value ,
383
+ TypeCode . UInt32 => ( T ) ( object ) ( uint ) value ,
384
+ TypeCode . Int64 => ( T ) ( object ) ( long ) value ,
385
+ TypeCode . UInt64 => ( T ) ( object ) value ,
386
+ TypeCode . Int16 => ( T ) ( object ) ( short ) value ,
387
+ TypeCode . UInt16 => ( T ) ( object ) ( ushort ) value ,
388
+ TypeCode . SByte => ( T ) ( object ) ( sbyte ) value ,
389
+ _ => ( T ) ( object ) ( byte ) value
398
390
} ;
399
391
}
400
392
0 commit comments