From 9326f9ed4ca4106b2838aabf9867118806cbd27a Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 10 Apr 2026 11:15:08 +0200 Subject: [PATCH] Introduce transcodingSteam for .NET 5 or greater Signed-off-by: Erwin --- .../JsonEventFormatter.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs b/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs index e170bd2..2f26035 100644 --- a/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs +++ b/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs @@ -190,20 +190,23 @@ private async Task> DecodeBatchModeMessageImpl(Stream private async Task ReadDocumentAsync(Stream data, ContentType? contentType, bool async) { var encoding = MimeUtilities.GetEncoding(contentType); - if (encoding is UTF8Encoding) - { - return async - ? await JsonDocument.ParseAsync(data, DocumentOptions).ConfigureAwait(false) - : JsonDocument.Parse(data, DocumentOptions); - } - else + if (encoding is not UTF8Encoding) { +#if NET5_0_OR_GREATER + data = Encoding.CreateTranscodingStream(data, encoding, Encoding.UTF8); +#else using var reader = new StreamReader(data, encoding); var json = async ? await reader.ReadToEndAsync().ConfigureAwait(false) : reader.ReadToEnd(); + return JsonDocument.Parse(json, DocumentOptions); +#endif } + + return async + ? await JsonDocument.ParseAsync(data, DocumentOptions).ConfigureAwait(false) + : JsonDocument.Parse(data, DocumentOptions); } private CloudEvent DecodeJsonElement(JsonElement element, IEnumerable? extensionAttributes, string paramName)