Skip to content

Commit b67ac2b

Browse files
committed
fix: Observe serialization options in JsonEventFormatter for System.Text.Json
Fixes #225 Signed-off-by: Jon Skeet <[email protected]>
1 parent 0005fc7 commit b67ac2b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/CloudNative.CloudEvents.SystemTextJson/JsonEventFormatter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public override ReadOnlyMemory<byte> EncodeBatchModeMessage(IEnumerable<CloudEve
402402
};
403403

404404
var stream = new MemoryStream();
405-
var writer = new Utf8JsonWriter(stream);
405+
var writer = CreateUtf8JsonWriter(stream);
406406
writer.WriteStartArray();
407407
foreach (var cloudEvent in cloudEvents)
408408
{
@@ -422,12 +422,24 @@ public override ReadOnlyMemory<byte> EncodeStructuredModeMessage(CloudEvent clou
422422
};
423423

424424
var stream = new MemoryStream();
425-
var writer = new Utf8JsonWriter(stream);
425+
var writer = CreateUtf8JsonWriter(stream);
426426
WriteCloudEventForBatchOrStructuredMode(writer, cloudEvent);
427427
writer.Flush();
428428
return stream.ToArray();
429429
}
430430

431+
private Utf8JsonWriter CreateUtf8JsonWriter(Stream stream)
432+
{
433+
var options = new JsonWriterOptions
434+
{
435+
Encoder = SerializerOptions?.Encoder,
436+
Indented = SerializerOptions?.WriteIndented ?? false,
437+
// TODO: Consider skipping validation in the future for the sake of performance.
438+
SkipValidation = false
439+
};
440+
return new Utf8JsonWriter(stream, options);
441+
}
442+
431443
private void WriteCloudEventForBatchOrStructuredMode(Utf8JsonWriter writer, CloudEvent cloudEvent)
432444
{
433445
Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));

test/CloudNative.CloudEvents.UnitTests/SystemTextJson/JsonEventFormatterTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,21 @@ public void DecodeStructured_DefaultContentTypeToApplicationJson()
10871087
Assert.Equal("some text", jsonData.GetString());
10881088
}
10891089

1090+
[Fact]
1091+
public void EncodeStructured_IndentationSettings()
1092+
{
1093+
var cloudEvent = new CloudEvent().PopulateRequiredAttributes();
1094+
var formatter = new JsonEventFormatter(new JsonSerializerOptions
1095+
{
1096+
WriteIndented = true
1097+
}, new JsonDocumentOptions());
1098+
var encoded = formatter.EncodeStructuredModeMessage(cloudEvent, out _);
1099+
// Normalize the result to LF line endings.
1100+
var json = BinaryDataUtilities.GetString(encoded, Encoding.UTF8).Replace("\r\n", "\n").Replace("\r", "\n");
1101+
var expected = "{\n \"specversion\": \"1.0\",\n \"id\": \"test-id\",\n \"source\": \"//test\",\n \"type\": \"test-type\"\n}";
1102+
Assert.Equal(expected, json);
1103+
}
1104+
10901105
// Utility methods
10911106
private static object DecodeBinaryModeEventData(byte[] bytes, string contentType)
10921107
{

0 commit comments

Comments
 (0)