Skip to content

Commit

Permalink
use UnsafeRelaxedJsonEscaping for Utf8JsonWriter in DefaultJsonSerial…
Browse files Browse the repository at this point in the history
…izer (#561)
  • Loading branch information
huoshan12345 authored Dec 25, 2024
1 parent c9cafa1 commit 5f0ad17
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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);
}
}
}

0 comments on commit 5f0ad17

Please sign in to comment.