Skip to content

Commit f8b0b14

Browse files
committed
#58 replace of Newtonsoft with System.Text.Json
1 parent 80ace9c commit f8b0b14

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

DeviceDetector.NET/Cache/ParseCache.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
using LiteDB;
2-
using Newtonsoft.Json;
32
using System;
43
using System.IO;
54
using Microsoft.Extensions.Logging;
5+
using DeviceDetectorNET.JsonSerializer;
66
namespace DeviceDetectorNET.Cache
77
{
88
internal class ParseCache : IParseCache
99
{
10-
private ParseCache()
11-
{
12-
}
1310

1411
private static readonly Lazy<ParseCache> LazyCache = new Lazy<ParseCache>(InitializeCache);
1512
private LiteDatabase _cacheDatabase;
13+
private IJsonSerializerProvider _jsonSerializer;
1614

17-
private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
15+
private ParseCache()
1816
{
19-
ContractResolver = null,
20-
TypeNameHandling = TypeNameHandling.Auto
21-
};
17+
_jsonSerializer = new SystemTextJsonSerializerProvider();
18+
}
2219

2320
private static ParseCache InitializeCache()
2421
{
@@ -67,7 +64,7 @@ public DeviceDetectorCachedData FindById(string key)
6764

6865
if (string.IsNullOrEmpty(cachedData?.Json)) return null;
6966

70-
var data = JsonConvert.DeserializeObject<DeviceDetectorCachedData>(cachedData.Json, JsonSettings);
67+
var data = _jsonSerializer.Deserialize<DeviceDetectorCachedData>(cachedData.Json);
7168
return data;
7269
}
7370

@@ -76,7 +73,7 @@ public void Upsert(string key, DeviceDetectorCachedData data)
7673
var cachedData = new CachedDataHolder()
7774
{
7875
Id = key,
79-
Json = JsonConvert.SerializeObject(data, JsonSettings),
76+
Json = _jsonSerializer.Serialize(data),
8077
ExpirationDate = DateTime.UtcNow.Add(DeviceDetectorSettings.ParseCacheDBExpiration)
8178
};
8279

DeviceDetector.NET/DeviceDetector.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<ItemGroup>
3939
<PackageReference Include="LiteDB" Version="5.0.21" />
4040
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
41-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
41+
<PackageReference Include="System.Text.Json" Version="6.0.5" />
4242
<PackageReference Include="YamlDotNet" Version="16.0.0" />
4343
</ItemGroup>
4444

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DeviceDetectorNET.JsonSerializer
6+
{
7+
public interface IJsonSerializerProvider
8+
{
9+
bool CanHandle(Type type);
10+
string Serialize(object obj, bool camelCase = true, bool indented = false);
11+
T Deserialize<T>(string jsonString, bool camelCase = true);
12+
object Deserialize(Type type, string jsonString, bool camelCase = true);
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Text.Json;
3+
4+
namespace DeviceDetectorNET.JsonSerializer
5+
{
6+
public class SystemTextJsonSerializerProvider : IJsonSerializerProvider
7+
{
8+
public bool CanHandle(Type type)
9+
{
10+
return true;
11+
}
12+
13+
public T Deserialize<T>(string jsonString, bool camelCase = true)
14+
{
15+
return System.Text.Json.JsonSerializer.Deserialize<T>(jsonString, CreateJsonSerializerOptions(camelCase));
16+
}
17+
18+
public object Deserialize(Type type, string jsonString, bool camelCase = true)
19+
{
20+
return System.Text.Json.JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase));
21+
}
22+
23+
public string Serialize(object obj, bool camelCase = true, bool indented = false)
24+
{
25+
return System.Text.Json.JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented));
26+
}
27+
28+
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
29+
{
30+
var settings = new JsonSerializerOptions();
31+
32+
if (camelCase)
33+
{
34+
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
35+
}
36+
37+
if (indented)
38+
{
39+
settings.WriteIndented = true;
40+
}
41+
42+
return settings;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)