2024-04-16 18:29:40 +00:00
|
|
|
namespace LootDumpProcessor.Serializers.Json;
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
public static class JsonSerializerFactory
|
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
private static readonly Dictionary<JsonSerializerTypes, IJsonSerializer> _jsonSerializers = new();
|
|
|
|
private static object lockObject = new();
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Requires LootDumpProcessorContext to be initialized before using
|
|
|
|
*/
|
|
|
|
public static IJsonSerializer GetInstance()
|
|
|
|
{
|
|
|
|
return GetInstance(LootDumpProcessorContext.GetConfig().JsonSerializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IJsonSerializer GetInstance(JsonSerializerTypes type)
|
|
|
|
{
|
|
|
|
IJsonSerializer serializer;
|
|
|
|
lock (lockObject)
|
|
|
|
{
|
|
|
|
if (!_jsonSerializers.TryGetValue(type, out serializer))
|
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
serializer = type switch
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
JsonSerializerTypes.Newtonsoft => new NewtonsoftJsonSerializer(),
|
|
|
|
JsonSerializerTypes.DotNet => new NetJsonSerializer(),
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
|
|
};
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
_jsonSerializers.Add(type, serializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serializer;
|
|
|
|
}
|
|
|
|
}
|