8000 refactor: use MongoDB.Bson instead of Newtonsoft.Json.Bson · cnblogs/EnyimMemcachedCore@2c494ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c494ba

Browse files
committed
refactor: use MongoDB.Bson instead of Newtonsoft.Json.Bson
1 parent a780f42 commit 2c494ba

File tree

4 files changed

+15
-76
lines changed

4 files changed

+15
-76
lines changed

src/Enyim.Caching/Enyim.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
<ItemGroup>
2222
<FrameworkReference Include="Microsoft.AspNetCore.App" />
23-
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
2423
</ItemGroup>
2524

2625
<ItemGroup>
2726
<PackageReference Include="MessagePack" Version="2.5.140" />
27+
<PackageReference Include="MongoDB.Bson" Version="2.24.0" />
2828
</ItemGroup>
2929

3030
<ItemGroup>

src/Enyim.Caching/Memcached/Transcoders/DataContractTranscoder.cs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
1-
using System;
2-
using System.IO;
3-
using System.Runtime.Serialization;
4-
using System.Text;
5-
using Newtonsoft.Json;
6-
using Newtonsoft.Json.Bson;
7-
81
namespace Enyim.Caching.Memcached
92
{
103
/// <summary>
114
/// Default <see cref="T:Enyim.Caching.Memcached.ITranscoder"/> implementation. Primitive types are manually serialized, the rest is serialized using <see cref="T:System.Runtime.Serialization.NetDataContractSerializer"/>.
125
/// </summary>
136
public class DataContractTranscoder : DefaultTranscoder
14-
{
15-
protected override object DeserializeObject(ArraySegment<byte> value)
16-
{
17-
using (var ms = new MemoryStream(value.Array, value.Offset, value.Count))
18-
{
19-
using (var reader = new BsonDataReader(ms))
20-
{
21-
JsonSerializer serializer = new JsonSerializer();
22-
return serializer.Deserialize(reader);
23-
}
24-
}
25-
}
26-
27-
protected override ArraySegment<byte> SerializeObject(object value)
28-
{
29-
using (var ms = new MemoryStream())
30-
{
31-
using (var writer = new BsonDataWriter(ms))
32-
{
33-
JsonSerializer serializer = new JsonSerializer();
34-
serializer.Serialize(writer, value);
35-
}
36-
37-
return new ArraySegment<byte>(ms.ToArray(), 0, (int)ms.Length);
38-
}
39-
}
40-
}
7+
{ }
418
}
429

4310
#region [ License information ]

src/Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using Newtonsoft.Json;
2-
using Newtonsoft.Json.Bson;
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization;
3+
using MongoDB.Bson.Serialization.Serializers;
34
using System;
4-
using System.Collections;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Reflection;
85
using System.Text;
96

107
namespace Enyim.Caching.Memcached
@@ -17,6 +14,12 @@ public class DefaultTranscoder : ITranscoder
1714
public const uint RawDataFlag = 0xfa52;
1815
private static readonly ArraySegment<byte> NullArray = new([]);
1916

17+
static DefaultTranscoder()
18+
{
19+
var objectSerializer = new ObjectSerializer(ObjectSerializer.AllAllowedTypes);
20+
BsonSerializer.RegisterSerializer(objectSerializer);
21+
}
22+
2023
CacheItem ITranscoder.Serialize(object value)
2124
{
2225
return Serialize(value);
@@ -49,14 +52,7 @@ public virtual T Deserialize<T>(CacheItem item)
4952
}
5053
}
5154

52-
using var ms = new MemoryStream(item.Data.ToArray());
53-
using var reader = new BsonDataReader(ms);
54-
if (typeof(T).GetTypeInfo().ImplementedInterfaces.Contains(typeof(IEnumerable)))
55-
{
56-
reader.ReadRootValueAsArray = true;
57-
}
58-
var serializer = new JsonSerializer();
59-
return serializer.Deserialize<T>(reader);
55+
return BsonSerializer.Deserialize<T>(item.Data.ToArray());
6056
}
6157

6258
protected virtual CacheItem Serialize(object value)
@@ -261,15 +257,8 @@ protected virtual ArraySegment<byte> SerializeSingle(Single value)
261257

262258
protected virtual ArraySegment<byte> SerializeObject(object value)
263259
{
264-
using (var ms = new MemoryStream())
265-
{
266-
using (var writer = new BsonDataWriter(ms))
267-
{
268-
var serializer = new JsonSerializer();
269-
serializer.Serialize(writer, value);
270-
return new ArraySegment<byte>(ms.ToArray(), 0, (int)ms.Length);
271-
}
272-
}
260+
var bson = value.ToBson();
261+
return new ArraySegment<byte>(bson, 0, bson.Length);
273262
}
274263

275264
#endregion
@@ -347,14 +336,7 @@ protected virtual sbyte DeserializeSByte(ArraySegment<byte> data)
347336

348337
protected virtual object DeserializeObject(ArraySegment<byte> value)
349338
{
350-
using (var ms = new MemoryStream(value.Array, value.Offset, value.Count))
351-
{
352-
using (var reader = new BsonDataReader(ms))
353-
{
354-
JsonSerializer serializer = new JsonSerializer();
355-
return serializer.Deserialize(reader);
356-
}
357-
}
339+
return BsonSerializer.Deserialize<object>(value.Array);
358340
}
359341

360342
#endregion

src/Enyim.Caching/Memcached/Transcoders/MessagePackTranscoder.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
using MessagePack;
22
using MessagePack.Resolvers;
3-
using Microsoft.Extensions.Options;
4-
using Newtonsoft.Json.Bson;
5-
using Newtonsoft.Json.Linq;
63
using System;
7-
using System.Collections;
8-
using System.Collections.Generic;
9-
using System.Collections.Immutable;
10-
using System.IO;
11-
using System.Reflection;
12-
using System.Runtime.CompilerServices;
13-
using System.Text;
144

155
namespace Enyim.Caching.Memcached.Transcoders
166
{

0 commit comments

Comments
 (0)
0