Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,23 @@ private async Task<IReadOnlyList<CloudEvent>> DecodeBatchModeMessageImpl(Stream
private async Task<JsonDocument> 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<CloudEventAttribute>? extensionAttributes, string paramName)
Expand Down