mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-07-22 17:35:26 +00:00
Added EmptyArrayObjectConverter, added JsonSerializerOptions parameter to SystemTextJsonMessageAccessor ctor
This commit is contained in:
parent
1ec5984fad
commit
460dd97537
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
{
|
||||
/// <summary>
|
||||
/// Converter mapping to an object but also handles when an empty array is send
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class EmptyArrayObjectConverter<T> : JsonConverter<T>
|
||||
{
|
||||
private static JsonSerializerOptions _defaultConverter = SerializerOptions.WithConverters;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T? Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.StartArray:
|
||||
_ = JsonSerializer.Deserialize<object[]>(ref reader, options);
|
||||
return default;
|
||||
case JsonTokenType.StartObject:
|
||||
return JsonSerializer.Deserialize<T>(ref reader, _defaultConverter);
|
||||
};
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
=> JsonSerializer.Serialize(writer, (object?)value, options);
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
protected JsonDocument? _document;
|
||||
|
||||
private static JsonSerializerOptions _serializerOptions = SerializerOptions.WithConverters;
|
||||
private JsonSerializerOptions? _customSerializerOptions;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsJson { get; set; }
|
||||
@ -31,6 +32,21 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
/// <inheritdoc />
|
||||
public object? Underlying => throw new NotImplementedException();
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonMessageAccessor()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonMessageAccessor(JsonSerializerOptions options)
|
||||
{
|
||||
_customSerializerOptions = options;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public CallResult<object> Deserialize(Type type, MessagePath? path = null)
|
||||
{
|
||||
@ -42,7 +58,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
|
||||
try
|
||||
{
|
||||
var result = _document.Deserialize(type, _serializerOptions);
|
||||
var result = _document.Deserialize(type, _customSerializerOptions ?? _serializerOptions);
|
||||
return new CallResult<object>(result!);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
@ -65,7 +81,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
|
||||
try
|
||||
{
|
||||
var result = _document.Deserialize<T>(_serializerOptions);
|
||||
var result = _document.Deserialize<T>(_customSerializerOptions ?? _serializerOptions);
|
||||
return new CallResult<T>(result!);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
@ -129,7 +145,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
{
|
||||
try
|
||||
{
|
||||
return value.Value.Deserialize<T>(_serializerOptions);
|
||||
return value.Value.Deserialize<T>(_customSerializerOptions ?? _serializerOptions);
|
||||
}
|
||||
catch { }
|
||||
return default;
|
||||
@ -223,6 +239,20 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
/// <inheritdoc />
|
||||
public override bool OriginalDataAvailable => _stream?.CanSeek == true;
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonStreamMessageAccessor(): base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonStreamMessageAccessor(JsonSerializerOptions options): base(options)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<CallResult> Read(Stream stream, bool bufferStream)
|
||||
{
|
||||
@ -286,6 +316,20 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||
{
|
||||
private ReadOnlyMemory<byte> _bytes;
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonByteMessageAccessor() : base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SystemTextJsonByteMessageAccessor(JsonSerializerOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public CallResult Read(ReadOnlyMemory<byte> data)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user