LootDumpProcessor/Serializers/Json/JsonSerializerFactory.cs

36 lines
1.1 KiB
C#
Raw Normal View History

namespace LootDumpProcessor.Serializers.Json;
2023-08-12 19:08:38 +01:00
public static class JsonSerializerFactory
{
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))
{
serializer = type switch
2023-08-12 19:08:38 +01: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;
}
}