mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-10-04 02:11:11 +00:00
4e2dc564dd
Updated WebSocket message routing improving performance for scenarios with multiple different subscriptions and topics Added AddCommaSeparated helper for Enum value arrays to ParameterCollection Improved EnumConverter performance and removed string allocation for happy path Fixed CreateParamString extension method for ArrayParametersSerialization.Json Fixed Shared GetOrderBookOptions and GetRecentTradeOptions base validations not being called
44 lines
1.8 KiB
C#
44 lines
1.8 KiB
C#
using System;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.Json;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace CryptoExchange.Net.Converters.SystemTextJson
|
|
{
|
|
/// <summary>
|
|
/// Converter for values which contain a nested json value
|
|
/// </summary>
|
|
public class ObjectStringConverter<T> : JsonConverter<T>
|
|
{
|
|
/// <inheritdoc />
|
|
#if NET5_0_OR_GREATER
|
|
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")]
|
|
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")]
|
|
#endif
|
|
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
return default;
|
|
|
|
var value = reader.GetString();
|
|
if (string.IsNullOrEmpty(value))
|
|
return default;
|
|
|
|
return JsonDocument.Parse(value!).Deserialize<T>(options);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
#if NET5_0_OR_GREATER
|
|
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")]
|
|
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")]
|
|
#endif
|
|
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
|
{
|
|
if (value is null)
|
|
writer.WriteStringValue("");
|
|
|
|
writer.WriteStringValue(JsonSerializer.Serialize(value, options));
|
|
}
|
|
}
|
|
}
|