diff --git a/Directory.Packages.props b/Directory.Packages.props index f19f00e..b0fc0ee 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 011dd56..cda39b8 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; @@ -413,8 +414,8 @@ public override ReadOnlyMemory EncodeBatchModeMessage(IEnumerable(); + var writer = CreateUtf8JsonWriter(bufferWriter); writer.WriteStartArray(); foreach (var cloudEvent in cloudEvents) { @@ -422,24 +423,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. @@ -473,6 +474,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 @@ +