Skip to content

Commit

Permalink
feat: add support for customizing JsonWriterOptions in JsonCoreSerial…
Browse files Browse the repository at this point in the history
…izer
  • Loading branch information
joelfoliveira committed Sep 8, 2023
1 parent 9ccfd35 commit a30f81d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/KafkaFlow.Serializer.JsonCore/JsonCoreSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,52 @@
/// </summary>
public class JsonCoreSerializer : ISerializer
{
private readonly JsonSerializerOptions options;
private readonly JsonSerializerOptions serializerOptions;
private readonly JsonWriterOptions writerOptions;

/// <summary>
/// Initializes a new instance of the <see cref="JsonCoreSerializer"/> class.
/// </summary>
/// <param name="options">Json serializer options</param>
public JsonCoreSerializer(JsonSerializerOptions options)
{
this.options = options;
this.serializerOptions = options;
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonCoreSerializer"/> class.
/// </summary>
/// <param name="writerOptions">Json writer options</param>
public JsonCoreSerializer(JsonWriterOptions writerOptions)
{
this.writerOptions = writerOptions;
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonCoreSerializer"/> class.
/// </summary>
/// <param name="serializerOptions">Json serializer options</param>
/// <param name="writerOptions">Json writer options</param>
public JsonCoreSerializer(JsonSerializerOptions serializerOptions, JsonWriterOptions writerOptions)
{
this.serializerOptions = serializerOptions;
this.writerOptions = writerOptions;
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonCoreSerializer"/> class.
/// </summary>
public JsonCoreSerializer()
: this(new JsonSerializerOptions())
: this(new JsonSerializerOptions(), default)
{
}

/// <inheritdoc/>
public Task SerializeAsync(object message, Stream output, ISerializerContext context)
{
using var writer = new Utf8JsonWriter(output);
using var writer = new Utf8JsonWriter(output, this.writerOptions);

JsonSerializer.Serialize(writer, message, this.options);
JsonSerializer.Serialize(writer, message, this.serializerOptions);

return Task.CompletedTask;
}
Expand All @@ -43,8 +64,8 @@ public Task SerializeAsync(object message, Stream output, ISerializerContext con
public async Task<object> DeserializeAsync(Stream input, Type type, ISerializerContext context)
{
return await JsonSerializer
.DeserializeAsync(input, type, this.options)
.DeserializeAsync(input, type, this.serializerOptions)
.ConfigureAwait(false);
}
}
}
}

Check warning on line 71 in src/KafkaFlow.Serializer.JsonCore/JsonCoreSerializer.cs

View workflow job for this annotation

GitHub Actions / Deploy to GitHub Pages

File is required to end with a single newline character
1 change: 1 addition & 0 deletions src/KafkaFlow.UnitTests/KafkaFlow.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ProjectReference Include="..\KafkaFlow.BatchConsume\KafkaFlow.BatchConsume.csproj" />
<ProjectReference Include="..\KafkaFlow.Compressor\KafkaFlow.Compressor.csproj" />
<ProjectReference Include="..\KafkaFlow.LogHandler.Microsoft\KafkaFlow.LogHandler.Microsoft.csproj" />
<ProjectReference Include="..\KafkaFlow.Serializer.JsonCore\KafkaFlow.Serializer.JsonCore.csproj" />
<ProjectReference Include="..\KafkaFlow.Serializer\KafkaFlow.Serializer.csproj" />
<ProjectReference Include="..\KafkaFlow.Serializer.NewtonsoftJson\KafkaFlow.Serializer.NewtonsoftJson.csproj" />
<ProjectReference Include="..\KafkaFlow.TypedHandler\KafkaFlow.TypedHandler.csproj" />
Expand Down
48 changes: 48 additions & 0 deletions src/KafkaFlow.UnitTests/Serializers/JsonCoreSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace KafkaFlow.UnitTests.Serializers
{
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using FluentAssertions;
using KafkaFlow.Serializer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[TestClass]
public class JsonCoreSerializerTests
{
private readonly Mock<ISerializerContext> contextMock = new();

[TestMethod]
public async Task SerializeAsync_PreventEscapeOfAccentedCharacter_SerializedObjectDoesNotHaveAccentedCharacterEscaped()
{
// Arrange
var message = new TestMessage { Text = "Café Façade" };
using var output = new MemoryStream();

var writerOptions = new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };

var target = new JsonCoreSerializer(writerOptions);

// Act
await target.SerializeAsync(message, output, this.contextMock.Object);

// Assert
var result = GetStreamText(output);
result.Should().Contain("Café Façade");
}

private static string GetStreamText(MemoryStream output)
{
output.Position = 0;
var reader = new StreamReader(output);
return reader.ReadToEnd();
}

private class TestMessage
{
public string Text { get; set; }
}
}
}

0 comments on commit a30f81d

Please sign in to comment.