Skip to content

Commit 09fc607

Browse files
committed
fixed UTF-8 BOM of Newtonsoft.Json (#272)
1 parent ee2e0ed commit 09fc607

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

serialization/EasyCaching.Serialization.Json/DefaultJsonSerializer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class DefaultJsonSerializer : IEasyCachingSerializer
1717
/// </summary>
1818
private readonly JsonSerializer jsonSerializer;
1919

20+
/// <summary>
21+
/// default utf-8 encoding
22+
/// </summary>
23+
private static readonly UTF8Encoding s_utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
24+
2025
/// <summary>
2126
/// The name.
2227
/// </summary>
@@ -48,7 +53,7 @@ public DefaultJsonSerializer(string name, JsonSerializerSettings serializerSetti
4853
public T Deserialize<T>(byte[] bytes)
4954
{
5055
using (var ms = new MemoryStream(bytes))
51-
using (var sr = new StreamReader(ms, Encoding.UTF8))
56+
using (var sr = new StreamReader(ms, s_utf8Encoding))
5257
using (var jtr = new JsonTextReader(sr))
5358
{
5459
return jsonSerializer.Deserialize<T>(jtr);
@@ -64,7 +69,7 @@ public T Deserialize<T>(byte[] bytes)
6469
public object Deserialize(byte[] bytes, Type type)
6570
{
6671
using (var ms = new MemoryStream(bytes))
67-
using (var sr = new StreamReader(ms, Encoding.UTF8))
72+
using (var sr = new StreamReader(ms, s_utf8Encoding))
6873
using (var jtr = new JsonTextReader(sr))
6974
{
7075
return jsonSerializer.Deserialize(jtr, type);
@@ -81,7 +86,7 @@ public byte[] Serialize<T>(T value)
8186
{
8287
using (var ms = new MemoryStream())
8388
{
84-
using (var sr = new StreamWriter(ms, Encoding.UTF8))
89+
using (var sr = new StreamWriter(ms, s_utf8Encoding))
8590
using (var jtr = new JsonTextWriter(sr))
8691
{
8792
jsonSerializer.Serialize(jtr, value);

serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ public object DeserializeObject(ArraySegment<byte> value)
8080
throw new InvalidDataException("JsonTranscoder only supports [\"TypeName\", object]");
8181
}
8282
}
83+
8384
/// <summary>
84-
//; Serialize the specified value.
85+
/// Serialize the specified value.
8586
/// </summary>
8687
/// <returns>The serialize.</returns>
8788
/// <param name="value">Value.</param>

0 commit comments

Comments
 (0)