diff --git a/serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs b/serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs
index 5a314471..4b353ba9 100644
--- a/serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs
+++ b/serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs
@@ -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
@@ -17,6 +18,10 @@ public class DefaultJsonSerializer : IEasyCachingSerializer
///
private readonly JsonSerializerOptions jsonSerializerOption;
///
+ /// The option for Utf8JsonWriter.
+ ///
+ private readonly JsonWriterOptions _jsonWriterOption;
+ ///
/// The name.
///
private readonly string _name;
@@ -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 };
}
///
@@ -64,7 +73,7 @@ public object Deserialize(byte[] bytes, Type type)
/// Value.
public object DeserializeObject(ArraySegment 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)
{
@@ -101,7 +110,7 @@ public ArraySegment 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);
diff --git a/test/EasyCaching.UnitTests/SerializerTests/SystemTestJsonSerializerTest.cs b/test/EasyCaching.UnitTests/SerializerTests/SystemTestJsonSerializerTest.cs
index 5066c176..28dc0ca5 100644
--- a/test/EasyCaching.UnitTests/SerializerTests/SystemTestJsonSerializerTest.cs
+++ b/test/EasyCaching.UnitTests/SerializerTests/SystemTestJsonSerializerTest.cs
@@ -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" };
@@ -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(joe_obj);
+ Assert.Equal(joe.Name, ((Employee)joe_obj).Name);
+ }
}
}
\ No newline at end of file