mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-13 01:12:59 +00:00
355d111a55
AOT support
43 lines
1.6 KiB
C#
43 lines
1.6 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace CryptoExchange.Net.Converters.SystemTextJson
|
|
{
|
|
/// <summary>
|
|
/// Serializer options
|
|
/// </summary>
|
|
public static class SerializerOptions
|
|
{
|
|
private static readonly ConcurrentDictionary<JsonSerializerContext, JsonSerializerOptions> _cache = new ConcurrentDictionary<JsonSerializerContext, JsonSerializerOptions>();
|
|
|
|
/// <summary>
|
|
/// Get Json serializer settings which includes standard converters for DateTime, bool, enum and number types
|
|
/// </summary>
|
|
public static JsonSerializerOptions WithConverters(JsonSerializerContext typeResolver)
|
|
{
|
|
if (!_cache.TryGetValue(typeResolver, out var options))
|
|
{
|
|
options = new JsonSerializerOptions
|
|
{
|
|
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals,
|
|
PropertyNameCaseInsensitive = false,
|
|
Converters =
|
|
{
|
|
new DateTimeConverter(),
|
|
new BoolConverter(),
|
|
new DecimalConverter(),
|
|
new IntConverter(),
|
|
new LongConverter(),
|
|
new NullableEnumConverterFactory(typeResolver)
|
|
},
|
|
TypeInfoResolver = typeResolver,
|
|
};
|
|
_cache.TryAdd(typeResolver, options);
|
|
}
|
|
|
|
return options;
|
|
}
|
|
}
|
|
}
|