mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-12 02:16:23 +00:00
ArrayConverter array conversion, can now deserialize single objects and arrays into an array
This commit is contained in:
parent
850a0a91b6
commit
94a7c1422c
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@ -18,6 +19,11 @@ namespace CryptoExchange.Net.Converters
|
|||||||
{
|
{
|
||||||
var result = Activator.CreateInstance(objectType);
|
var result = Activator.CreateInstance(objectType);
|
||||||
var arr = JArray.Load(reader);
|
var arr = JArray.Load(reader);
|
||||||
|
return ParseObject(arr, result, objectType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ParseObject(JArray arr, object result, Type objectType)
|
||||||
|
{
|
||||||
foreach (var property in objectType.GetProperties())
|
foreach (var property in objectType.GetProperties())
|
||||||
{
|
{
|
||||||
var attribute =
|
var attribute =
|
||||||
@ -28,8 +34,42 @@ namespace CryptoExchange.Net.Converters
|
|||||||
if (attribute.Index >= arr.Count)
|
if (attribute.Index >= arr.Count)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if(property.PropertyType.BaseType == typeof(Array))
|
||||||
|
{
|
||||||
|
var objType = property.PropertyType.GetElementType();
|
||||||
|
var innerArray = (JArray)arr[attribute.Index];
|
||||||
|
var count = 0;
|
||||||
|
if (innerArray.Count == 0)
|
||||||
|
{
|
||||||
|
var arrayResult = (IList)Activator.CreateInstance(property.PropertyType, new object[] { 0 });
|
||||||
|
property.SetValue(result, arrayResult);
|
||||||
|
}
|
||||||
|
else if (innerArray[0].Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
var arrayResult = (IList)Activator.CreateInstance(property.PropertyType, new object[] { innerArray.Count() });
|
||||||
|
foreach (var obj in innerArray)
|
||||||
|
{
|
||||||
|
var innerObj = Activator.CreateInstance(objType);
|
||||||
|
arrayResult[count] = ParseObject((JArray)obj, innerObj, objType);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
property.SetValue(result, arrayResult);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var arrayResult = (IList)Activator.CreateInstance(property.PropertyType, new object[] { 1 });
|
||||||
|
var innerObj = Activator.CreateInstance(objType);
|
||||||
|
arrayResult[0] = ParseObject(innerArray, innerObj, objType);
|
||||||
|
property.SetValue(result, arrayResult);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
object value;
|
object value;
|
||||||
var converterAttribute = (JsonConverterAttribute)property.GetCustomAttribute(typeof(JsonConverterAttribute));
|
var converterAttribute = (JsonConverterAttribute)property.GetCustomAttribute(typeof(JsonConverterAttribute));
|
||||||
|
if (converterAttribute == null)
|
||||||
|
converterAttribute = (JsonConverterAttribute)property.PropertyType.GetCustomAttribute(typeof(JsonConverterAttribute));
|
||||||
|
|
||||||
if (converterAttribute != null)
|
if (converterAttribute != null)
|
||||||
value = arr[attribute.Index].ToObject(property.PropertyType, new JsonSerializer() { Converters = { (JsonConverter)Activator.CreateInstance(converterAttribute.ConverterType) } });
|
value = arr[attribute.Index].ToObject(property.PropertyType, new JsonSerializer() { Converters = { (JsonConverter)Activator.CreateInstance(converterAttribute.ConverterType) } });
|
||||||
else
|
else
|
||||||
@ -43,7 +83,7 @@ namespace CryptoExchange.Net.Converters
|
|||||||
if (((JToken)value).Type == JTokenType.Null)
|
if (((JToken)value).Type == JTokenType.Null)
|
||||||
value = null;
|
value = null;
|
||||||
|
|
||||||
if ((property.PropertyType == typeof(decimal)
|
if ((property.PropertyType == typeof(decimal)
|
||||||
|| property.PropertyType == typeof(decimal?))
|
|| property.PropertyType == typeof(decimal?))
|
||||||
&& (value != null && value.ToString().Contains("e")))
|
&& (value != null && value.ToString().Contains("e")))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user