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

Support for customizing JsonWriterOptions in JsonCoreSerializer #425

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
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: 28 additions & 7 deletions src/KafkaFlow.Serializer.JsonCore/JsonCoreSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace KafkaFlow.Serializer

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

View workflow job for this annotation

GitHub Actions / build

File is required to end with a single newline character [/home/runner/work/kafkaflow/kafkaflow/src/KafkaFlow.Serializer.JsonCore/KafkaFlow.Serializer.JsonCore.csproj]
{
using System;
using System.IO;
Expand All @@ -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 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 / Test deployment

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; }
}
}
}
Loading