Skip to content

Commit 5f0ad17

Browse files
authored
use UnsafeRelaxedJsonEscaping for Utf8JsonWriter in DefaultJsonSerializer (#561)
1 parent c9cafa1 commit 5f0ad17

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.IO;
55
using System.Text;
6+
using System.Text.Encodings.Web;
67
using System.Text.Json;
78

89
namespace 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);

test/EasyCaching.UnitTests/SerializerTests/SystemTestJsonSerializerTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void NullValueHandling_Test_Should_Succeed()
5959
var serializer = new DefaultJsonSerializer("json", new JsonSerializerOptions
6060
{
6161
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
62-
}) ;
62+
});
6363

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

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

6969
Assert.Null(joe.Manager);
7070
}
71+
72+
[Fact]
73+
public void SerializeObject_NestedClass_Test_Should_Succeed()
74+
{
75+
var serializer = new DefaultJsonSerializer("json", new JsonSerializerOptions());
76+
77+
Employee joe = new Employee { Name = "Joe User" };
78+
79+
var joe_byte = serializer.SerializeObject(joe);
80+
var joe_obj = serializer.DeserializeObject(joe_byte);
81+
82+
Assert.IsType<Employee>(joe_obj);
83+
Assert.Equal(joe.Name, ((Employee)joe_obj).Name);
84+
}
7185
}
7286
}

0 commit comments

Comments
 (0)