From 17ac32194b0608d1fc8d944c68c401b2723220d0 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 10 Apr 2026 10:37:35 +0200 Subject: [PATCH] ArrayBufferWriter Signed-off-by: Erwin --- Directory.Packages.props | 1 + ...udNative.CloudEvents.SystemTextJson.csproj | 2 +- .../JsonEventFormatter.cs | 34 +++++++++++++------ .../CloudNative.CloudEvents.csproj | 1 + 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 42b8c4b..78e9043 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,6 +4,7 @@ + diff --git a/src/CloudNative.CloudEvents.SystemTextJson/CloudNative.CloudEvents.SystemTextJson.csproj b/src/CloudNative.CloudEvents.SystemTextJson/CloudNative.CloudEvents.SystemTextJson.csproj index dde18c8..077c8d5 100644 --- a/src/CloudNative.CloudEvents.SystemTextJson/CloudNative.CloudEvents.SystemTextJson.csproj +++ b/src/CloudNative.CloudEvents.SystemTextJson/CloudNative.CloudEvents.SystemTextJson.csproj @@ -1,7 +1,7 @@ - netstandard2.0;netstandard2.1;net8.0 + netstandard2.1;net8.0 JSON support for the CNCF CloudEvents SDK, based on System.Text.Json. cncf;cloudnative;cloudevents;events;json;systemtextjson enable diff --git a/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs b/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs index e170bd2..853802e 100644 --- a/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs +++ b/src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs @@ -4,6 +4,7 @@ using CloudNative.CloudEvents.Core; using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Net.Mime; @@ -410,8 +411,8 @@ public override ReadOnlyMemory EncodeBatchModeMessage(IEnumerable(); + var writer = CreateUtf8JsonWriter(bufferWriter); writer.WriteStartArray(); foreach (var cloudEvent in cloudEvents) { @@ -419,24 +420,24 @@ public override ReadOnlyMemory EncodeBatchModeMessage(IEnumerable public override ReadOnlyMemory EncodeStructuredModeMessage(CloudEvent cloudEvent, out ContentType contentType) { - contentType = new ContentType(StructuredMediaType) - { - CharSet = Encoding.UTF8.WebName - }; + contentType = new ContentType(StructuredMediaType) { CharSet = Encoding.UTF8.WebName }; + + var bufferWriter = new ArrayBufferWriter(); + using var writer = CreateUtf8JsonWriter(bufferWriter); - var stream = new MemoryStream(); - var writer = CreateUtf8JsonWriter(stream); WriteCloudEventForBatchOrStructuredMode(writer, cloudEvent); writer.Flush(); - return stream.ToArray(); + + return bufferWriter.WrittenMemory; } + /// /// Converts the given to a containing the structured mode JSON format representation /// of the event. @@ -470,6 +471,19 @@ private Utf8JsonWriter CreateUtf8JsonWriter(Stream stream) return new Utf8JsonWriter(stream, options); } + private Utf8JsonWriter CreateUtf8JsonWriter(IBufferWriter bufferWriter) + { + var options = new JsonWriterOptions + { + Encoder = SerializerOptions?.Encoder, + Indented = SerializerOptions?.WriteIndented ?? false, + // TODO: Consider skipping validation in the future for the sake of performance. + SkipValidation = false + }; + + return new Utf8JsonWriter(bufferWriter, options); + } + private void WriteCloudEventForBatchOrStructuredMode(Utf8JsonWriter writer, CloudEvent cloudEvent) { Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent)); diff --git a/src/CloudNative.CloudEvents/CloudNative.CloudEvents.csproj b/src/CloudNative.CloudEvents/CloudNative.CloudEvents.csproj index aa85f9b..0c08995 100644 --- a/src/CloudNative.CloudEvents/CloudNative.CloudEvents.csproj +++ b/src/CloudNative.CloudEvents/CloudNative.CloudEvents.csproj @@ -8,6 +8,7 @@ +