未验证 提交 7ab70f90 编写于 作者: S Stephen Toub 提交者: GitHub

Use Utf8.IsValid in System.Text.Json (#88165)

* Use Utf8.IsValid in System.Text.Json

* Fix TestingGetStringInvalidUTF8 test

It's expecting there to always be a DecoderFallbackException as the inner exception.
上级 41a624dc
......@@ -5,7 +5,7 @@
using System.Buffers.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Unicode;
namespace System.Text.Json
{
......@@ -277,6 +277,12 @@ public static int TranscodeHelper(ReadOnlySpan<byte> utf8Unescaped, Span<char> d
public static void ValidateUtf8(ReadOnlySpan<byte> utf8Buffer)
{
#if NET8_0_OR_GREATER
if (!Utf8.IsValid(utf8Buffer))
{
throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8();
}
#else
try
{
#if NETCOREAPP
......@@ -304,6 +310,7 @@ public static void ValidateUtf8(ReadOnlySpan<byte> utf8Buffer)
// Therefore, wrapping the DecoderFallbackException around an InvalidOperationException.
throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex);
}
#endif
}
internal static int GetUtf8ByteCount(ReadOnlySpan<char> text)
......
......@@ -515,7 +515,7 @@ public static void ThrowInvalidOperationException_ReadIncompleteUTF16()
throw GetInvalidOperationException(SR.CannotReadIncompleteUTF16);
}
public static InvalidOperationException GetInvalidOperationException_ReadInvalidUTF8(DecoderFallbackException innerException)
public static InvalidOperationException GetInvalidOperationException_ReadInvalidUTF8(DecoderFallbackException? innerException = null)
{
return GetInvalidOperationException(SR.CannotTranscodeInvalidUtf8, innerException);
}
......@@ -525,7 +525,7 @@ public static ArgumentException GetArgumentException_ReadInvalidUTF16(EncoderFal
return new ArgumentException(SR.CannotTranscodeInvalidUtf16, innerException);
}
public static InvalidOperationException GetInvalidOperationException(string message, Exception innerException)
public static InvalidOperationException GetInvalidOperationException(string message, Exception? innerException)
{
InvalidOperationException ex = new InvalidOperationException(message, innerException);
ex.Source = ExceptionSourceValueToRethrowAsJsonException;
......
......@@ -225,10 +225,15 @@ internal static void ValidateNumber(ReadOnlySpan<byte> utf8FormattedNumber)
}
}
#if !NET8_0_OR_GREATER
private static readonly UTF8Encoding s_utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
#endif
public static unsafe bool IsValidUtf8String(ReadOnlySpan<byte> bytes)
{
#if NET8_0_OR_GREATER
return Utf8.IsValid(bytes);
#else
try
{
#if NETCOREAPP
......@@ -248,6 +253,7 @@ public static unsafe bool IsValidUtf8String(ReadOnlySpan<byte> bytes)
{
return false;
}
#endif
}
internal static unsafe OperationStatus ToUtf8(ReadOnlySpan<char> source, Span<byte> destination, out int written)
......
......@@ -1125,13 +1125,22 @@ public static void TestingGetStringInvalidUTF8(byte[] dataUtf8)
int length = json.HasValueSequence ? (int)json.ValueSequence.Length : json.ValueSpan.Length;
InvalidOperationException ex = JsonTestHelper.AssertThrows<InvalidOperationException>(ref json, (ref Utf8JsonReader json) => json.GetString());
Assert.IsType<DecoderFallbackException>(ex.InnerException);
if (ex.InnerException is not null)
{
Assert.IsType<DecoderFallbackException>(ex.InnerException);
}
ex = JsonTestHelper.AssertThrows<InvalidOperationException>(ref json, (ref Utf8JsonReader json) => json.CopyString(new byte[length]));
Assert.IsType<DecoderFallbackException>(ex.InnerException);
if (ex.InnerException is not null)
{
Assert.IsType<DecoderFallbackException>(ex.InnerException);
}
ex = JsonTestHelper.AssertThrows<InvalidOperationException>(ref json, (ref Utf8JsonReader json) => json.CopyString(new char[length]));
Assert.IsType<DecoderFallbackException>(ex.InnerException);
if (ex.InnerException is not null)
{
Assert.IsType<DecoderFallbackException>(ex.InnerException);
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册