3
3
using System ;
4
4
using System . IO ;
5
5
using System . Text ;
6
+ using System . Text . Encodings . Web ;
6
7
using System . Text . Json ;
7
8
8
9
namespace EasyCaching . Serialization . SystemTextJson
@@ -17,6 +18,10 @@ public class DefaultJsonSerializer : IEasyCachingSerializer
17
18
/// </summary>
18
19
private readonly JsonSerializerOptions jsonSerializerOption ;
19
20
/// <summary>
21
+ /// The option for Utf8JsonWriter.
22
+ /// </summary>
23
+ private readonly JsonWriterOptions _jsonWriterOption ;
24
+ /// <summary>
20
25
/// The name.
21
26
/// </summary>
22
27
private readonly string _name ;
@@ -35,6 +40,10 @@ public DefaultJsonSerializer(string name, JsonSerializerOptions serializerSettin
35
40
{
36
41
_name = name ;
37
42
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 } ;
38
47
}
39
48
40
49
/// <summary>
@@ -64,7 +73,7 @@ public object Deserialize(byte[] bytes, Type type)
64
73
/// <param name="value">Value.</param>
65
74
public object DeserializeObject ( ArraySegment < byte > value )
66
75
{
67
- var jr = new Utf8JsonReader ( value ) ;
76
+ var jr = new Utf8JsonReader ( value ) ; // the JsonReaderOptions will be used here, which works well.
68
77
jr . Read ( ) ;
69
78
if ( jr . TokenType == JsonTokenType . StartArray )
70
79
{
@@ -101,7 +110,7 @@ public ArraySegment<byte> SerializeObject(object obj)
101
110
var typeName = TypeHelper . BuildTypeName ( obj . GetType ( ) ) ;
102
111
103
112
using ( var ms = new MemoryStream ( ) )
104
- using ( var jw = new Utf8JsonWriter ( ms ) )
113
+ using ( var jw = new Utf8JsonWriter ( ms , _jsonWriterOption ) )
105
114
{
106
115
jw . WriteStartArray ( ) ;
107
116
jw . WriteStringValue ( typeName ) ;
0 commit comments