1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Fixed some WriteJson calls in converters to ease unit testing

This commit is contained in:
JKorf 2018-08-15 14:15:32 +02:00
parent 24caec411f
commit 10de7f4b46
3 changed files with 34 additions and 4 deletions

View File

@ -65,16 +65,46 @@ namespace CryptoExchange.Net.Converters
var props = value.GetType().GetProperties();
var ordered = props.OrderBy(p => p.GetCustomAttribute<ArrayPropertyAttribute>()?.Index);
int last = -1;
foreach (var prop in ordered)
{
var arrayProp = prop.GetCustomAttribute<ArrayPropertyAttribute>();
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

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}