mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 16:06:15 +00:00
Updated single endpoint limit configuration, added LongConverter, updated SystemTextJsonComparer logic
This commit is contained in:
parent
0a0c66541e
commit
9ec4f2276f
@ -309,14 +309,14 @@ namespace CryptoExchange.Net.Clients
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Endpoint specific rate limiting
|
// Endpoint specific rate limiting
|
||||||
if (definition.EndpointLimitCount != null && definition.EndpointLimitPeriod != null)
|
if (definition.LimitGuard != null && ClientOptions.RateLimiterEnabled)
|
||||||
{
|
{
|
||||||
if (definition.RateLimitGate == null)
|
if (definition.RateLimitGate == null)
|
||||||
throw new Exception("Ratelimit gate not set when endpoint limit is specified");
|
throw new Exception("Ratelimit gate not set when endpoint limit is specified");
|
||||||
|
|
||||||
if (ClientOptions.RateLimiterEnabled)
|
if (ClientOptions.RateLimiterEnabled)
|
||||||
{
|
{
|
||||||
var limitResult = await definition.RateLimitGate.ProcessSingleAsync(_logger, requestId, RateLimitItemType.Request, definition, baseAddress, AuthenticationProvider?._credentials.Key, requestWeight, ClientOptions.RateLimitingBehaviour, cancellationToken).ConfigureAwait(false);
|
var limitResult = await definition.RateLimitGate.ProcessSingleAsync(_logger, requestId, definition.LimitGuard, RateLimitItemType.Request, definition, baseAddress, AuthenticationProvider?._credentials.Key, ClientOptions.RateLimitingBehaviour, cancellationToken).ConfigureAwait(false);
|
||||||
if (!limitResult)
|
if (!limitResult)
|
||||||
return new CallResult(limitResult.Error!);
|
return new CallResult(limitResult.Error!);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net.Converters.SystemTextJson
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Int converter
|
||||||
|
/// </summary>
|
||||||
|
public class LongConverter : JsonConverter<long?>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.Null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (reader.TokenType == JsonTokenType.String)
|
||||||
|
{
|
||||||
|
var value = reader.GetString();
|
||||||
|
if (string.IsNullOrEmpty(value))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
return reader.GetInt64();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
writer.WriteNullValue();
|
||||||
|
else
|
||||||
|
writer.WriteNumberValue(value.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,8 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
|
|||||||
new EnumConverter(),
|
new EnumConverter(),
|
||||||
new BoolConverter(),
|
new BoolConverter(),
|
||||||
new DecimalConverter(),
|
new DecimalConverter(),
|
||||||
new IntConverter()
|
new IntConverter(),
|
||||||
|
new LongConverter()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -48,18 +48,16 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// Request weight
|
/// Request weight
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Weight { get; set; } = 1;
|
public int Weight { get; set; } = 1;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rate limit gate to use
|
/// Rate limit gate to use
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IRateLimitGate? RateLimitGate { get; set; }
|
public IRateLimitGate? RateLimitGate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rate limit for this specific endpoint
|
/// Individual endpoint rate limit guard to use
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? EndpointLimitCount { get; set; }
|
public IRateLimitGuard? LimitGuard { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// Rate limit period for this specific endpoint
|
|
||||||
/// </summary>
|
|
||||||
public TimeSpan? EndpointLimitPeriod { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -41,8 +41,7 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// <param name="method">The HttpMethod</param>
|
/// <param name="method">The HttpMethod</param>
|
||||||
/// <param name="path">Endpoint path</param>
|
/// <param name="path">Endpoint path</param>
|
||||||
/// <param name="rateLimitGate">The rate limit gate</param>
|
/// <param name="rateLimitGate">The rate limit gate</param>
|
||||||
/// <param name="endpointLimitCount">The limit count for this specific endpoint</param>
|
/// <param name="limitGuard">The rate limit guard for this specific endpoint</param>
|
||||||
/// <param name="endpointLimitPeriod">The period for the limit for this specific endpoint</param>
|
|
||||||
/// <param name="weight">Request weight</param>
|
/// <param name="weight">Request weight</param>
|
||||||
/// <param name="authenticated">Endpoint is authenticated</param>
|
/// <param name="authenticated">Endpoint is authenticated</param>
|
||||||
/// <param name="requestBodyFormat">Request body format</param>
|
/// <param name="requestBodyFormat">Request body format</param>
|
||||||
@ -56,8 +55,7 @@ namespace CryptoExchange.Net.Objects
|
|||||||
IRateLimitGate? rateLimitGate,
|
IRateLimitGate? rateLimitGate,
|
||||||
int weight,
|
int weight,
|
||||||
bool authenticated,
|
bool authenticated,
|
||||||
int? endpointLimitCount = null,
|
IRateLimitGuard? limitGuard = null,
|
||||||
TimeSpan? endpointLimitPeriod = null,
|
|
||||||
RequestBodyFormat? requestBodyFormat = null,
|
RequestBodyFormat? requestBodyFormat = null,
|
||||||
HttpMethodParameterPosition? parameterPosition = null,
|
HttpMethodParameterPosition? parameterPosition = null,
|
||||||
ArrayParametersSerialization? arraySerialization = null,
|
ArrayParametersSerialization? arraySerialization = null,
|
||||||
@ -69,8 +67,7 @@ namespace CryptoExchange.Net.Objects
|
|||||||
def = new RequestDefinition(path, method)
|
def = new RequestDefinition(path, method)
|
||||||
{
|
{
|
||||||
Authenticated = authenticated,
|
Authenticated = authenticated,
|
||||||
EndpointLimitCount = endpointLimitCount,
|
LimitGuard = limitGuard,
|
||||||
EndpointLimitPeriod = endpointLimitPeriod,
|
|
||||||
RateLimitGate = rateLimitGate,
|
RateLimitGate = rateLimitGate,
|
||||||
Weight = weight,
|
Weight = weight,
|
||||||
ArraySerialization = arraySerialization,
|
ArraySerialization = arraySerialization,
|
||||||
|
@ -12,9 +12,22 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SingleLimitGuard : IRateLimitGuard
|
public class SingleLimitGuard : IRateLimitGuard
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Default endpoint limit
|
||||||
|
/// </summary>
|
||||||
|
public static Func<RequestDefinition, string, SecureString?, string> Default { get; } = new Func<RequestDefinition, string, SecureString?, string>((def, host, key) => def.Path + def.Method);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Endpoint limit per API key
|
||||||
|
/// </summary>
|
||||||
|
public static Func<RequestDefinition, string, SecureString?, string> PerApiKey { get; } = new Func<RequestDefinition, string, SecureString?, string>((def, host, key) => def.Path + def.Method);
|
||||||
|
|
||||||
private readonly Dictionary<string, IWindowTracker> _trackers;
|
private readonly Dictionary<string, IWindowTracker> _trackers;
|
||||||
private readonly RateLimitWindowType _windowType;
|
private readonly RateLimitWindowType _windowType;
|
||||||
private readonly double? _decayRate;
|
private readonly double? _decayRate;
|
||||||
|
private readonly int _limit;
|
||||||
|
private readonly TimeSpan _period;
|
||||||
|
private readonly Func<RequestDefinition, string, SecureString?, string> _keySelector;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Name => "EndpointLimitGuard";
|
public string Name => "EndpointLimitGuard";
|
||||||
@ -25,20 +38,28 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SingleLimitGuard(RateLimitWindowType windowType, double? decayRate = null)
|
public SingleLimitGuard(
|
||||||
|
int limit,
|
||||||
|
TimeSpan period,
|
||||||
|
RateLimitWindowType windowType,
|
||||||
|
double? decayRate = null,
|
||||||
|
Func<RequestDefinition, string, SecureString?, string>? keySelector = null)
|
||||||
{
|
{
|
||||||
|
_limit = limit;
|
||||||
|
_period = period;
|
||||||
_windowType = windowType;
|
_windowType = windowType;
|
||||||
_decayRate = decayRate;
|
_decayRate = decayRate;
|
||||||
|
_keySelector = keySelector ?? Default;
|
||||||
_trackers = new Dictionary<string, IWindowTracker>();
|
_trackers = new Dictionary<string, IWindowTracker>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public LimitCheck Check(RateLimitItemType type, RequestDefinition definition, string host, SecureString? apiKey, int requestWeight)
|
public LimitCheck Check(RateLimitItemType type, RequestDefinition definition, string host, SecureString? apiKey, int requestWeight)
|
||||||
{
|
{
|
||||||
var key = definition.Path + definition.Method;
|
var key = _keySelector(definition, host, apiKey);
|
||||||
if (!_trackers.TryGetValue(key, out var tracker))
|
if (!_trackers.TryGetValue(key, out var tracker))
|
||||||
{
|
{
|
||||||
tracker = CreateTracker(definition.EndpointLimitCount!.Value, definition.EndpointLimitPeriod!.Value);
|
tracker = CreateTracker();
|
||||||
_trackers.Add(key, tracker);
|
_trackers.Add(key, tracker);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,27 +67,27 @@ namespace CryptoExchange.Net.RateLimiting.Guards
|
|||||||
if (delay == default)
|
if (delay == default)
|
||||||
return LimitCheck.NotNeeded;
|
return LimitCheck.NotNeeded;
|
||||||
|
|
||||||
return LimitCheck.Needed(delay, definition.EndpointLimitCount!.Value, definition.EndpointLimitPeriod!.Value, tracker.Current);
|
return LimitCheck.Needed(delay, _limit, _period, tracker.Current);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, SecureString? apiKey, int requestWeight)
|
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, SecureString? apiKey, int requestWeight)
|
||||||
{
|
{
|
||||||
var key = definition.Path + definition.Method;
|
var key = _keySelector(definition, host, apiKey);
|
||||||
var tracker = _trackers[key];
|
var tracker = _trackers[key];
|
||||||
tracker.ApplyWeight(requestWeight);
|
tracker.ApplyWeight(requestWeight);
|
||||||
return RateLimitState.Applied(definition.EndpointLimitCount!.Value, definition.EndpointLimitPeriod!.Value, tracker.Current);
|
return RateLimitState.Applied(_limit, _period, tracker.Current);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new WindowTracker
|
/// Create a new WindowTracker
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected IWindowTracker CreateTracker(int limit, TimeSpan timeSpan)
|
protected IWindowTracker CreateTracker()
|
||||||
{
|
{
|
||||||
return _windowType == RateLimitWindowType.Sliding ? new SlidingWindowTracker(limit, timeSpan)
|
return _windowType == RateLimitWindowType.Sliding ? new SlidingWindowTracker(_limit, _period)
|
||||||
: _windowType == RateLimitWindowType.Fixed ? new FixedWindowTracker(limit, timeSpan) :
|
: _windowType == RateLimitWindowType.Fixed ? new FixedWindowTracker(_limit, _period) :
|
||||||
new DecayWindowTracker(limit, timeSpan, _decayRate ?? throw new InvalidOperationException("Decay rate not provided"));
|
new DecayWindowTracker(_limit, _period, _decayRate ?? throw new InvalidOperationException("Decay rate not provided"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,13 +32,6 @@ namespace CryptoExchange.Net.RateLimiting.Interfaces
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task SetRetryAfterGuardAsync(DateTime retryAfter);
|
Task SetRetryAfterGuardAsync(DateTime retryAfter);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set the SingleLimitGuard for handling individual endpoint rate limits
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guard"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
IRateLimitGate SetSingleLimitGuard(SingleLimitGuard guard);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the 'retry after' timestamp if set
|
/// Returns the 'retry after' timestamp if set
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -65,14 +58,14 @@ namespace CryptoExchange.Net.RateLimiting.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">Logger</param>
|
/// <param name="logger">Logger</param>
|
||||||
/// <param name="itemId">Id of the item to check</param>
|
/// <param name="itemId">Id of the item to check</param>
|
||||||
|
/// <param name="guard">The guard</param>
|
||||||
/// <param name="type">The rate limit item type</param>
|
/// <param name="type">The rate limit item type</param>
|
||||||
/// <param name="definition">The request definition</param>
|
/// <param name="definition">The request definition</param>
|
||||||
/// <param name="baseAddress">The host address</param>
|
/// <param name="baseAddress">The host address</param>
|
||||||
/// <param name="apiKey">The API key</param>
|
/// <param name="apiKey">The API key</param>
|
||||||
/// <param name="requestWeight">Request weight</param>
|
|
||||||
/// <param name="behaviour">Behaviour when rate limit is hit</param>
|
/// <param name="behaviour">Behaviour when rate limit is hit</param>
|
||||||
/// <param name="ct">Cancelation token</param>
|
/// <param name="ct">Cancelation token</param>
|
||||||
/// <returns>Error if RateLimitingBehaviour is Fail and rate limit is hit</returns>
|
/// <returns>Error if RateLimitingBehaviour is Fail and rate limit is hit</returns>
|
||||||
Task<CallResult> ProcessSingleAsync(ILogger logger, int itemId, RateLimitItemType type, RequestDefinition definition, string baseAddress, SecureString? apiKey, int requestWeight, RateLimitingBehaviour behaviour, CancellationToken ct);
|
Task<CallResult> ProcessSingleAsync(ILogger logger, int itemId, IRateLimitGuard guard, RateLimitItemType type, RequestDefinition definition, string baseAddress, SecureString? apiKey, RateLimitingBehaviour behaviour, CancellationToken ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ namespace CryptoExchange.Net.RateLimiting
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class RateLimitGate : IRateLimitGate
|
public class RateLimitGate : IRateLimitGate
|
||||||
{
|
{
|
||||||
private IRateLimitGuard _singleLimitGuard = new SingleLimitGuard(RateLimitWindowType.Sliding);
|
|
||||||
private readonly ConcurrentBag<IRateLimitGuard> _guards;
|
private readonly ConcurrentBag<IRateLimitGuard> _guards;
|
||||||
private readonly SemaphoreSlim _semaphore;
|
private readonly SemaphoreSlim _semaphore;
|
||||||
private readonly string _name;
|
private readonly string _name;
|
||||||
@ -53,16 +52,23 @@ namespace CryptoExchange.Net.RateLimiting
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<CallResult> ProcessSingleAsync(ILogger logger, int itemId, RateLimitItemType type, RequestDefinition definition, string host, SecureString? apiKey, int requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
|
public async Task<CallResult> ProcessSingleAsync(
|
||||||
|
ILogger logger,
|
||||||
|
int itemId,
|
||||||
|
IRateLimitGuard guard,
|
||||||
|
RateLimitItemType type,
|
||||||
|
RequestDefinition definition,
|
||||||
|
string host,
|
||||||
|
SecureString? apiKey,
|
||||||
|
RateLimitingBehaviour rateLimitingBehaviour,
|
||||||
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
await _semaphore.WaitAsync(ct).ConfigureAwait(false);
|
await _semaphore.WaitAsync(ct).ConfigureAwait(false);
|
||||||
if (requestWeight == 0)
|
|
||||||
requestWeight = 1;
|
|
||||||
|
|
||||||
_waitingCount++;
|
_waitingCount++;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await CheckGuardsAsync(new IRateLimitGuard[] { _singleLimitGuard }, logger, itemId, type, definition, host, apiKey, requestWeight, rateLimitingBehaviour, ct).ConfigureAwait(false);
|
return await CheckGuardsAsync(new IRateLimitGuard[] { guard }, logger, itemId, type, definition, host, apiKey, 1, rateLimitingBehaviour, ct).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -130,13 +136,6 @@ namespace CryptoExchange.Net.RateLimiting
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IRateLimitGate SetSingleLimitGuard(SingleLimitGuard guard)
|
|
||||||
{
|
|
||||||
_singleLimitGuard = guard;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task SetRetryAfterGuardAsync(DateTime retryAfter)
|
public async Task SetRetryAfterGuardAsync(DateTime retryAfter)
|
||||||
{
|
{
|
||||||
|
@ -70,48 +70,62 @@ namespace CryptoExchange.Net.Testing.Comparers
|
|||||||
else if (jsonObject!.Type == JTokenType.Array)
|
else if (jsonObject!.Type == JTokenType.Array)
|
||||||
{
|
{
|
||||||
var jObjs = (JArray)jsonObject;
|
var jObjs = (JArray)jsonObject;
|
||||||
var list = (IEnumerable)resultData;
|
if (resultData is IEnumerable list)
|
||||||
var enumerator = list.GetEnumerator();
|
|
||||||
foreach (var jObj in jObjs)
|
|
||||||
{
|
{
|
||||||
enumerator.MoveNext();
|
var enumerator = list.GetEnumerator();
|
||||||
if (jObj.Type == JTokenType.Object)
|
foreach (var jObj in jObjs)
|
||||||
{
|
{
|
||||||
foreach (var subProp in ((JObject)jObj).Properties())
|
enumerator.MoveNext();
|
||||||
|
if (jObj.Type == JTokenType.Object)
|
||||||
{
|
{
|
||||||
if (ignoreProperties?.Contains(subProp.Name) == true)
|
foreach (var subProp in ((JObject)jObj).Properties())
|
||||||
|
{
|
||||||
|
if (ignoreProperties?.Contains(subProp.Name) == true)
|
||||||
|
continue;
|
||||||
|
CheckObject(method, subProp, enumerator.Current, ignoreProperties!);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (jObj.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
var resultObj = enumerator.Current;
|
||||||
|
if (resultObj is string)
|
||||||
|
// string list
|
||||||
continue;
|
continue;
|
||||||
CheckObject(method, subProp, enumerator.Current, ignoreProperties!);
|
|
||||||
|
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
|
||||||
|
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
|
||||||
|
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
|
||||||
|
if (jsonConverter != typeof(ArrayConverter))
|
||||||
|
// Not array converter?
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in jObj.Children())
|
||||||
|
{
|
||||||
|
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
|
||||||
|
if (arrayProp != null)
|
||||||
|
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
else if (jObj.Type == JTokenType.Array)
|
|
||||||
{
|
|
||||||
var resultObj = enumerator.Current;
|
|
||||||
if (resultObj is string)
|
|
||||||
// string list
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
|
|
||||||
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
|
|
||||||
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
|
|
||||||
if (jsonConverter != typeof(ArrayConverter))
|
|
||||||
// Not array converter?
|
|
||||||
continue;
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
foreach (var item in jObj.Children())
|
|
||||||
{
|
{
|
||||||
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
|
var value = enumerator.Current;
|
||||||
if (arrayProp != null)
|
if (value == default && ((JValue)jObj).Type != JTokenType.Null)
|
||||||
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
|
throw new Exception($"{method}: Array has no value while input json array has value {jObj}");
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var resultProps = resultData.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in jObjs.Children())
|
||||||
{
|
{
|
||||||
var value = enumerator.Current;
|
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
|
||||||
if (value == default && ((JValue)jObj).Type != JTokenType.Null)
|
if (arrayProp != null)
|
||||||
throw new Exception($"{method}: Array has no value while input json array has value {jObj}");
|
CheckPropertyValue(method, item, arrayProp.GetValue(resultData), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user