Skip to content

Commit

Permalink
fix: Observe JsonSerializer options for JsonEventFormatter using Newt…
Browse files Browse the repository at this point in the history
…onsoft.Json

This is the Json.NET equivalent of #225.

Note that not every option in JsonTextWriter is supported; in the future we could potentially introduce some alternative approach to provide more flexibility.

Signed-off-by: Jon Skeet <jonskeet@google.com>
  • Loading branch information
jskeet committed Aug 5, 2022
1 parent b67ac2b commit aa7429d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public override ReadOnlyMemory<byte> EncodeStructuredModeMessage(CloudEvent clou
};

var stream = new MemoryStream();
var writer = new JsonTextWriter(new StreamWriter(stream));
var writer = CreateJsonTextWriter(stream);
WriteCloudEventForBatchOrStructuredMode(writer, cloudEvent);
writer.Flush();
return stream.ToArray();
Expand All @@ -406,7 +406,7 @@ public override ReadOnlyMemory<byte> EncodeBatchModeMessage(IEnumerable<CloudEve
};

var stream = new MemoryStream();
var writer = new JsonTextWriter(new StreamWriter(stream));
var writer = CreateJsonTextWriter(stream);
writer.WriteStartArray();
foreach (var cloudEvent in cloudEvents)
{
Expand All @@ -417,6 +417,20 @@ public override ReadOnlyMemory<byte> EncodeBatchModeMessage(IEnumerable<CloudEve
return stream.ToArray();
}

private JsonTextWriter CreateJsonTextWriter(Stream stream) =>
// TODO: Allow settings to be specified separately?
// JsonSerializer doesn't allow us to set the indentation or indentation character, for example.
new JsonTextWriter(new StreamWriter(stream))
{
Formatting = Serializer.Formatting,
DateFormatHandling = Serializer.DateFormatHandling,
DateFormatString = Serializer.DateFormatString,
DateTimeZoneHandling = Serializer.DateTimeZoneHandling,
FloatFormatHandling = Serializer.FloatFormatHandling,
Culture = Serializer.Culture,
StringEscapeHandling = Serializer.StringEscapeHandling,
};

private void WriteCloudEventForBatchOrStructuredMode(JsonWriter writer, CloudEvent cloudEvent)
{
Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,18 @@ public void DecodeStructured_DefaultContentTypeToApplicationJson()
Assert.Equal("some text", jsonData.Value);
}

[Fact]
public void EncodeStructured_IndentationSettings()
{
var cloudEvent = new CloudEvent().PopulateRequiredAttributes();
var formatter = new JsonEventFormatter(new JsonSerializer { Formatting = Formatting.Indented });
var encoded = formatter.EncodeStructuredModeMessage(cloudEvent, out _);
// Normalize the result to LF line endings.
var json = BinaryDataUtilities.GetString(encoded, Encoding.UTF8).Replace("\r\n", "\n").Replace("\r", "\n");
var expected = "{\n \"specversion\": \"1.0\",\n \"id\": \"test-id\",\n \"source\": \"//test\",\n \"type\": \"test-type\"\n}";
Assert.Equal(expected, json);
}

// Utility methods
private static object? DecodeBinaryModeEventData(byte[] bytes, string contentType)
{
Expand Down

0 comments on commit aa7429d

Please sign in to comment.