diff --git a/CryptoExchange.Net/Testing/Comparers/JsonNetComparer.cs b/CryptoExchange.Net/Testing/Comparers/JsonNetComparer.cs index 2c7943b..4e72951 100644 --- a/CryptoExchange.Net/Testing/Comparers/JsonNetComparer.cs +++ b/CryptoExchange.Net/Testing/Comparers/JsonNetComparer.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Globalization; using System.Linq; using System.Reflection; +using System.Text.Json.Nodes; using CryptoExchange.Net.Converters; using CryptoExchange.Net.Converters.JsonNet; using Newtonsoft.Json; @@ -257,6 +258,64 @@ namespace CryptoExchange.Net.Testing.Comparers } } } + else if(propValue.Type == JTokenType.Array) + { + var jObjs = (JArray)propValue; + if (propertyValue is IEnumerable list) + { + var enumerator = list.GetEnumerator(); + foreach (var jObj in jObjs) + { + enumerator.MoveNext(); + if (jObj.Type == JTokenType.Object) + { + 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; + var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast().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.Values()) + { + var arrayProp = resultProps.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 + { + var value = enumerator.Current; + if (value == default && ((JValue)jObj).Type != JTokenType.Null) + throw new Exception($"{method}: Array has no value while input json array has value {jObj}"); + } + } + } + else + { + var resultProps = propertyValue.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast().SingleOrDefault())); + int i = 0; + foreach (var item in jObjs.Values()) + { + var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p; + if (arrayProp != null) + CheckPropertyValue(method, item, arrayProp.GetValue(propertyValue), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!); + i++; + } + } + } else { CheckValues(method, propertyName!, propertyType, (JValue)propValue, propertyValue); @@ -278,7 +337,7 @@ namespace CryptoExchange.Net.Testing.Comparers if (time != DateTimeConverter.ParseFromString(jsonValue.Value()!)) throw new Exception($"{method}: {property} not equal: {jsonValue.Value()} vs {time}"); } - else if (propertyType.IsEnum) + else if (propertyType.IsEnum || Nullable.GetUnderlyingType(propertyType)?.IsEnum == true) { // TODO enum comparing } @@ -305,7 +364,10 @@ namespace CryptoExchange.Net.Testing.Comparers } else if (jsonValue.Type == JTokenType.Boolean) { - if (jsonValue.Value() != (bool)objectValue) + if (objectValue is bool boolVal && jsonValue.Value() != boolVal) + throw new Exception($"{method}: {property} not equal: {jsonValue.Value()} vs {(bool)objectValue}"); + + if (jsonValue.Value() != bool.Parse(objectValue.ToString())) throw new Exception($"{method}: {property} not equal: {jsonValue.Value()} vs {(bool)objectValue}"); } }