Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize encoding #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
35 changes: 26 additions & 9 deletions src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace CloudNative.CloudEvents.NewtonsoftJson
/// </remarks>
public class JsonEventFormatter : CloudEventFormatter
{
private static readonly Encoding noBomUtf8 = new UTF8Encoding(false);

private static readonly IReadOnlyDictionary<CloudEventAttributeType, JTokenType> expectedTokenTypesForReservedAttributes =
new Dictionary<CloudEventAttributeType, JTokenType>
{
Expand Down Expand Up @@ -395,7 +397,7 @@ public override ReadOnlyMemory<byte> EncodeStructuredModeMessage(CloudEvent clou
CharSet = Encoding.UTF8.WebName
};

var stream = new MemoryStream();
var stream = this.GetMemoryStream();
var writer = CreateJsonTextWriter(stream);
WriteCloudEventForBatchOrStructuredMode(writer, cloudEvent);
writer.Flush();
Expand Down Expand Up @@ -425,7 +427,7 @@ public override ReadOnlyMemory<byte> EncodeBatchModeMessage(IEnumerable<CloudEve
CharSet = Encoding.UTF8.WebName
};

var stream = new MemoryStream();
var stream = this.GetMemoryStream();
var writer = CreateJsonTextWriter(stream);
writer.WriteStartArray();
foreach (var cloudEvent in cloudEvents)
Expand Down Expand Up @@ -568,13 +570,21 @@ public override ReadOnlyMemory<byte> EncodeBinaryModeEventData(CloudEvent cloudE
ContentType contentType = new ContentType(cloudEvent.DataContentType ?? JsonMediaType);
if (IsJsonMediaType(contentType.MediaType))
{
// TODO: Make this more efficient. We could write to a StreamWriter with a MemoryStream,
// but then we end up with a BOM in most cases, which I suspect we don't want.
// An alternative is to make sure that contentType.GetEncoding() always returns an encoding
// without a preamble (or rewrite StreamWriter...)
var stringWriter = new StringWriter();
Serializer.Serialize(stringWriter, cloudEvent.Data);
return MimeUtilities.GetEncoding(contentType).GetBytes(stringWriter.ToString());
var encoding = MimeUtilities.GetEncoding(contentType);
if (ReferenceEquals(encoding, Encoding.UTF8))
{
using var stream = this.GetMemoryStream();
using var writer = new StreamWriter(stream, noBomUtf8);
Serializer.Serialize(writer, cloudEvent.Data);
writer.Flush();
return stream.ToArray();
}
else
{
var stringWriter = new StringWriter();
Serializer.Serialize(stringWriter, cloudEvent.Data);
return encoding.GetBytes(stringWriter.ToString());
}
}
if (contentType.MediaType.StartsWith("text/") && cloudEvent.Data is string text)
{
Expand Down Expand Up @@ -614,6 +624,13 @@ public override void DecodeBinaryModeEventData(ReadOnlyMemory<byte> body, CloudE
}
}

/// <summary>
/// Creates a stream that can be written to to provide buffered array data.
/// </summary>
/// <returns>A memory stream.</returns>
protected virtual MemoryStream GetMemoryStream()
=> new MemoryStream();

/// <summary>
/// Creates a <see cref="JsonReader"/> for the given stream. This may be overridden in derived classes to
/// customize the JSON parsing process, subject to the constraints listed in the remarks.
Expand Down