Skip to content

Commit

Permalink
#58 replace of Newtonsoft with System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
totpero committed Nov 26, 2024
1 parent 80ace9c commit f8b0b14
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
17 changes: 7 additions & 10 deletions DeviceDetector.NET/Cache/ParseCache.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
using LiteDB;
using Newtonsoft.Json;
using System;
using System.IO;
using Microsoft.Extensions.Logging;
using DeviceDetectorNET.JsonSerializer;
namespace DeviceDetectorNET.Cache
{
internal class ParseCache : IParseCache
{
private ParseCache()
{
}

private static readonly Lazy<ParseCache> LazyCache = new Lazy<ParseCache>(InitializeCache);
private LiteDatabase _cacheDatabase;
private IJsonSerializerProvider _jsonSerializer;

private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
private ParseCache()
{
ContractResolver = null,
TypeNameHandling = TypeNameHandling.Auto
};
_jsonSerializer = new SystemTextJsonSerializerProvider();
}

private static ParseCache InitializeCache()
{
Expand Down Expand Up @@ -67,7 +64,7 @@ public DeviceDetectorCachedData FindById(string key)

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

var data = JsonConvert.DeserializeObject<DeviceDetectorCachedData>(cachedData.Json, JsonSettings);
var data = _jsonSerializer.Deserialize<DeviceDetectorCachedData>(cachedData.Json);
return data;
}

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

Expand Down
2 changes: 1 addition & 1 deletion DeviceDetector.NET/DeviceDetector.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.21" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="6.0.5" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>

Expand Down
14 changes: 14 additions & 0 deletions DeviceDetector.NET/JsonSerializer/IJsonSerializerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DeviceDetectorNET.JsonSerializer
{
public interface IJsonSerializerProvider
{
bool CanHandle(Type type);
string Serialize(object obj, bool camelCase = true, bool indented = false);
T Deserialize<T>(string jsonString, bool camelCase = true);
object Deserialize(Type type, string jsonString, bool camelCase = true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Text.Json;

namespace DeviceDetectorNET.JsonSerializer
{
public class SystemTextJsonSerializerProvider : IJsonSerializerProvider
{
public bool CanHandle(Type type)
{
return true;
}

public T Deserialize<T>(string jsonString, bool camelCase = true)
{
return System.Text.Json.JsonSerializer.Deserialize<T>(jsonString, CreateJsonSerializerOptions(camelCase));
}

public object Deserialize(Type type, string jsonString, bool camelCase = true)
{
return System.Text.Json.JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase));
}

public string Serialize(object obj, bool camelCase = true, bool indented = false)
{
return System.Text.Json.JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented));
}

protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
{
var settings = new JsonSerializerOptions();

if (camelCase)
{
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}

if (indented)
{
settings.WriteIndented = true;
}

return settings;
}
}
}

0 comments on commit f8b0b14

Please sign in to comment.