using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; using System.Text.Json.Serialization; namespace CryptoExchange.Net.Converters { /// /// Caching for JsonSerializerContext instances /// public static class JsonSerializerContextCache { private static ConcurrentDictionary _cache = new ConcurrentDictionary(); /// /// Get the instance of the provided type T. It will be created if it doesn't exist yet. /// /// Implementation type of the JsonSerializerContext public static JsonSerializerContext GetOrCreate() where T: JsonSerializerContext, new() { var contextType = typeof(T); if (_cache.TryGetValue(contextType, out var context)) return context; var instance = new T(); _cache[contextType] = instance; return instance; } } }