Skip to content

Commit

Permalink
remove unnecessary bound check now that all primitive types are suppo…
Browse files Browse the repository at this point in the history
…rted
  • Loading branch information
Jacksondr5 committed Sep 17, 2020
1 parent b3a2b9b commit 0a6a345
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,35 +119,28 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
return Unsafe.As<long, T>(ref int64);
}
break;

// When utf8reader/writer will support all primitive types we should remove custom bound checks
// https://github.com/dotnet/runtime/issues/29000
case TypeCode.SByte:
if (reader.TryGetInt32(out int byte8) && JsonHelpers.IsInRangeInclusive(byte8, sbyte.MinValue, sbyte.MaxValue))
if (reader.TryGetSByte(out sbyte byte8))
{
sbyte byte8Value = (sbyte)byte8;
return Unsafe.As<sbyte, T>(ref byte8Value);
return Unsafe.As<sbyte, T>(ref byte8);
}
break;
case TypeCode.Byte:
if (reader.TryGetUInt32(out uint ubyte8) && JsonHelpers.IsInRangeInclusive(ubyte8, byte.MinValue, byte.MaxValue))
if (reader.TryGetByte(out byte ubyte8))
{
byte ubyte8Value = (byte)ubyte8;
return Unsafe.As<byte, T>(ref ubyte8Value);
return Unsafe.As<byte, T>(ref ubyte8);
}
break;
case TypeCode.Int16:
if (reader.TryGetInt32(out int int16) && JsonHelpers.IsInRangeInclusive(int16, short.MinValue, short.MaxValue))
if (reader.TryGetInt16(out short int16))
{
short shortValue = (short)int16;
return Unsafe.As<short, T>(ref shortValue);
return Unsafe.As<short, T>(ref int16);
}
break;
case TypeCode.UInt16:
if (reader.TryGetUInt32(out uint uint16) && JsonHelpers.IsInRangeInclusive(uint16, ushort.MinValue, ushort.MaxValue))
if (reader.TryGetUInt16(out ushort uint16))
{
ushort ushortValue = (ushort)uint16;
return Unsafe.As<ushort, T>(ref ushortValue);
return Unsafe.As<ushort, T>(ref uint16);
}
break;
}
Expand Down

0 comments on commit 0a6a345

Please sign in to comment.