diff --git a/CryptoExchange.Net/Converters/ArrayConverter.cs b/CryptoExchange.Net/Converters/ArrayConverter.cs index d2c856f..3132e02 100644 --- a/CryptoExchange.Net/Converters/ArrayConverter.cs +++ b/CryptoExchange.Net/Converters/ArrayConverter.cs @@ -65,16 +65,46 @@ namespace CryptoExchange.Net.Converters var props = value.GetType().GetProperties(); var ordered = props.OrderBy(p => p.GetCustomAttribute()?.Index); + int last = -1; foreach (var prop in ordered) { + var arrayProp = prop.GetCustomAttribute(); + if (arrayProp == null) + continue; + + if (arrayProp.Index == last) + continue; + + while (arrayProp.Index != last + 1) + { + writer.WriteValue((string)null); + last += 1; + } + + last = arrayProp.Index; var converterAttribute = (JsonConverterAttribute)prop.GetCustomAttribute(typeof(JsonConverterAttribute)); if(converterAttribute != null) - writer.WriteValue(JsonConvert.SerializeObject(prop.GetValue(value), (JsonConverter)Activator.CreateInstance(converterAttribute.ConverterType))); - else + writer.WriteRawValue(JsonConvert.SerializeObject(prop.GetValue(value), (JsonConverter)Activator.CreateInstance(converterAttribute.ConverterType))); + else if(!IsSimple(prop.PropertyType)) writer.WriteValue(JsonConvert.SerializeObject(prop.GetValue(value))); + else + writer.WriteValue(prop.GetValue(value)); } writer.WriteEndArray(); } + + private bool IsSimple(Type type) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + // nullable type, check if the nested type is simple. + return IsSimple(type.GetGenericArguments()[0]); + } + return type.IsPrimitive + || type.IsEnum + || type.Equals(typeof(string)) + || type.Equals(typeof(decimal)); + } } public class ArrayPropertyAttribute: Attribute diff --git a/CryptoExchange.Net/Converters/TimestampConverter.cs b/CryptoExchange.Net/Converters/TimestampConverter.cs index 17e7c5f..3004ef7 100644 --- a/CryptoExchange.Net/Converters/TimestampConverter.cs +++ b/CryptoExchange.Net/Converters/TimestampConverter.cs @@ -21,7 +21,7 @@ namespace CryptoExchange.Net.Converters public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - writer.WriteValue(Math.Round((((DateTime)value) - new DateTime(1970, 1, 1)).TotalMilliseconds)); + writer.WriteValue((long)Math.Round((((DateTime)value) - new DateTime(1970, 1, 1)).TotalMilliseconds)); } } } diff --git a/CryptoExchange.Net/Converters/TimestampSecondsConverter.cs b/CryptoExchange.Net/Converters/TimestampSecondsConverter.cs index eff94f5..8ad79cc 100644 --- a/CryptoExchange.Net/Converters/TimestampSecondsConverter.cs +++ b/CryptoExchange.Net/Converters/TimestampSecondsConverter.cs @@ -22,7 +22,7 @@ namespace CryptoExchange.Net.Converters public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - writer.WriteValue(Math.Round((((DateTime)value) - new DateTime(1970, 1, 1)).TotalSeconds)); + writer.WriteValue((long)Math.Round((((DateTime)value) - new DateTime(1970, 1, 1)).TotalSeconds)); } } }