Skip to content

Commit

Permalink
Use ThrowHelper in Utf8JsonReader.GetGuid so that the deserializer ca…
Browse files Browse the repository at this point in the history
…n catch the exception and re-throw JsonException. (dotnet/corefx#40938)

* Use ThrowHelper in Utf8JsonReader.GetGuid so that the deserializer can
catch the exception and re-throw JsonException.

* Fix the typo in the enum used and added more test cases.


Commit migrated from dotnet/corefx@5bec8c8
  • Loading branch information
ahsonkhan authored Sep 9, 2019
1 parent 8e061d8 commit 97d9da8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public byte[] GetBytesFromBase64()
{
if (!TryGetBytesFromBase64(out byte[] value))
{
throw ThrowHelper.GetFormatException(DateType.Base64String);
throw ThrowHelper.GetFormatException(DataType.Base64String);
}
return value;
}
Expand Down Expand Up @@ -395,7 +395,7 @@ public DateTime GetDateTime()
{
if (!TryGetDateTime(out DateTime value))
{
throw ThrowHelper.GetFormatException(DateType.DateTime);
throw ThrowHelper.GetFormatException(DataType.DateTime);
}

return value;
Expand All @@ -418,7 +418,7 @@ public DateTimeOffset GetDateTimeOffset()
{
if (!TryGetDateTimeOffset(out DateTimeOffset value))
{
throw ThrowHelper.GetFormatException(DateType.DateTimeOffset);
throw ThrowHelper.GetFormatException(DataType.DateTimeOffset);
}

return value;
Expand All @@ -441,7 +441,7 @@ public Guid GetGuid()
{
if (!TryGetGuid(out Guid value))
{
throw new FormatException(SR.FormatGuid);
throw ThrowHelper.GetFormatException(DataType.Guid);
}

return value;
Expand Down
16 changes: 10 additions & 6 deletions src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,21 +584,24 @@ public static FormatException GetFormatException(NumericType numericType)
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static FormatException GetFormatException(DateType dateType)
public static FormatException GetFormatException(DataType dateType)
{
string message = "";

switch (dateType)
{
case DateType.DateTime:
case DataType.DateTime:
message = SR.FormatDateTime;
break;
case DateType.DateTimeOffset:
case DataType.DateTimeOffset:
message = SR.FormatDateTimeOffset;
break;
case DateType.Base64String:
case DataType.Base64String:
message = SR.CannotDecodeInvalidBase64;
break;
case DataType.Guid:
message = SR.FormatGuid;
break;
default:
Debug.Fail($"The DateType enum value: {dateType} is not part of the switch. Add the appropriate case and exception message.");
break;
Expand Down Expand Up @@ -670,10 +673,11 @@ internal enum NumericType
Decimal
}

internal enum DateType
internal enum DataType
{
DateTime,
DateTimeOffset,
Base64String
Base64String,
Guid,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ public static void ReadPrimitivesFail()
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int[]>(Encoding.UTF8.GetBytes(@"[1,a]")));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int>(@"null"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int>(@""""""));

Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTime>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTimeOffset>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Guid>("\"abc\""));

Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<byte>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<byte>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<sbyte>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<sbyte>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<short>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<short>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ushort>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ushort>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<uint>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<uint>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<long>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<long>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ulong>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ulong>("1.1"));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<float>("\"abc\""));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<double>("\"abc\""));
}

[Theory]
Expand Down

0 comments on commit 97d9da8

Please sign in to comment.