33using System ;
44using System . IO ;
55using System . Text ;
6+ using System . Text . Encodings . Web ;
67using System . Text . Json ;
78
89namespace EasyCaching . Serialization . SystemTextJson
@@ -17,6 +18,10 @@ public class DefaultJsonSerializer : IEasyCachingSerializer
1718 /// </summary>
1819 private readonly JsonSerializerOptions jsonSerializerOption ;
1920 /// <summary>
21+ /// The option for Utf8JsonWriter.
22+ /// </summary>
23+ private readonly JsonWriterOptions _jsonWriterOption ;
24+ /// <summary>
2025 /// The name.
2126 /// </summary>
2227 private readonly string _name ;
@@ -35,6 +40,10 @@ public DefaultJsonSerializer(string name, JsonSerializerOptions serializerSettin
3540 {
3641 _name = name ;
3742 jsonSerializerOption = serializerSettings ;
43+
44+ // NOTE: We must use UnsafeRelaxedJsonEscaping instead of the encoder from JsonSerializerOptions,
45+ // because we must ensure that the plus sign '+', which is the part of a nested class, is not escaped when writing type name.
46+ _jsonWriterOption = new JsonWriterOptions { Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping } ;
3847 }
3948
4049 /// <summary>
@@ -64,7 +73,7 @@ public object Deserialize(byte[] bytes, Type type)
6473 /// <param name="value">Value.</param>
6574 public object DeserializeObject ( ArraySegment < byte > value )
6675 {
67- var jr = new Utf8JsonReader ( value ) ;
76+ var jr = new Utf8JsonReader ( value ) ; // the JsonReaderOptions will be used here, which works well.
6877 jr . Read ( ) ;
6978 if ( jr . TokenType == JsonTokenType . StartArray )
7079 {
@@ -101,7 +110,7 @@ public ArraySegment<byte> SerializeObject(object obj)
101110 var typeName = TypeHelper . BuildTypeName ( obj . GetType ( ) ) ;
102111
103112 using ( var ms = new MemoryStream ( ) )
104- using ( var jw = new Utf8JsonWriter ( ms ) )
113+ using ( var jw = new Utf8JsonWriter ( ms , _jsonWriterOption ) )
105114 {
106115 jw . WriteStartArray ( ) ;
107116 jw . WriteStringValue ( typeName ) ;
0 commit comments