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

Fix wrong type name serialization due to the plus sign '+' is escaped. #561

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace EasyCaching.Serialization.SystemTextJson
Expand All @@ -17,6 +18,10 @@ public class DefaultJsonSerializer : IEasyCachingSerializer
/// </summary>
private readonly JsonSerializerOptions jsonSerializerOption;
/// <summary>
/// The option for Utf8JsonWriter.
/// </summary>
private readonly JsonWriterOptions _jsonWriterOption;
/// <summary>
/// The name.
/// </summary>
private readonly string _name;
Expand All @@ -35,6 +40,10 @@ public DefaultJsonSerializer(string name, JsonSerializerOptions serializerSettin
{
_name = name;
jsonSerializerOption = serializerSettings;

// NOTE: We must use UnsafeRelaxedJsonEscaping instead of the encoder from JsonSerializerOptions,
// because we must ensure that the plus sign '+', which is the part of a nested class, is not escaped when writing type name.
_jsonWriterOption = new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
}

/// <summary>
Expand Down Expand Up @@ -64,7 +73,7 @@ public object Deserialize(byte[] bytes, Type type)
/// <param name="value">Value.</param>
public object DeserializeObject(ArraySegment<byte> value)
{
var jr = new Utf8JsonReader(value);
var jr = new Utf8JsonReader(value); // the JsonReaderOptions will be used here, which works well.
jr.Read();
if (jr.TokenType == JsonTokenType.StartArray)
{
Expand Down Expand Up @@ -101,7 +110,7 @@ public ArraySegment<byte> SerializeObject(object obj)
var typeName = TypeHelper.BuildTypeName(obj.GetType());

using (var ms = new MemoryStream())
using (var jw = new Utf8JsonWriter(ms))
using (var jw = new Utf8JsonWriter(ms, _jsonWriterOption))
{
jw.WriteStartArray();
jw.WriteStringValue(typeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void NullValueHandling_Test_Should_Succeed()
var serializer = new DefaultJsonSerializer("json", new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}) ;
});

Employee joe = new Employee { Name = "Joe User" };

Expand All @@ -68,5 +68,19 @@ public void NullValueHandling_Test_Should_Succeed()

Assert.Null(joe.Manager);
}

[Fact]
public void SerializeObject_NestedClass_Test_Should_Succeed()
{
var serializer = new DefaultJsonSerializer("json", new JsonSerializerOptions());

Employee joe = new Employee { Name = "Joe User" };

var joe_byte = serializer.SerializeObject(joe);
var joe_obj = serializer.DeserializeObject(joe_byte);

Assert.IsType<Employee>(joe_obj);
Assert.Equal(joe.Name, ((Employee)joe_obj).Name);
}
}
}
Loading