Skip to content

Commit

Permalink
Fix ordinal case and add support for decimal and nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Mar 14, 2024
1 parent bfa0780 commit 31cacce
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public static T Deserialize<T>(object data)
Deserialize(hashtable, typeof(T), ref instance!);
return (T)instance;
}
else if (data is byte[] byteArray)
{
var jsonString = new string(System.Text.Encoding.UTF8.GetChars(byteArray));
return Deserialize<T>(jsonString);
}
else
{
throw new ArgumentException("Unsupported data type for deserialization.");
Expand Down Expand Up @@ -210,7 +215,7 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)

foreach (string v in values.Keys)
{
var prop = props.FirstOrDefault(p => string.Compare(p.Name, v, true) == 0);
var prop = props.FirstOrDefault(p => string.Compare(p.Name, v, StringComparison.OrdinalIgnoreCase) == 0);

if (prop != null && prop.CanWrite)
{
Expand Down Expand Up @@ -242,7 +247,10 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)
prop.SetValue(instance, Convert.ToByte(values[v]));
break;
case bool _ when prop.PropertyType == typeof(sbyte):
prop.SetValue(instance, Convert.ToBoolean(values[v]));
prop.SetValue(instance, Convert.ToSByte(values[v]));
break;
case bool _ when prop.PropertyType == typeof(decimal):
prop.SetValue(instance, Convert.ToDecimal(values[v]));
break;
case bool _ when prop.PropertyType == typeof(double):
prop.SetValue(instance, Convert.ToDouble(values[v]));
Expand Down Expand Up @@ -273,13 +281,23 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)

foreach (var item in (ArrayList)values[v])
{
var listItem = Activator.CreateInstance(listType);
object listItem = Activator.CreateInstance(listType);
Deserialize(item as Hashtable, listType, ref listItem);
addMethod.Invoke(list, new[] { listItem });
}

prop.SetValue(instance, list);
}
else if (IsComplexType(prop.PropertyType))
{
var hashtableValue = values[v] as Hashtable;
if (hashtableValue != null)
{
object complexInstance = Activator.CreateInstance(prop.PropertyType);
Deserialize(hashtableValue, prop.PropertyType, ref complexInstance);
prop.SetValue(instance, complexInstance);
}
}
else
{
throw new NotSupportedException($"Type '{prop.PropertyType}' not supported");
Expand All @@ -289,4 +307,26 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)
}
}
}


private static bool IsComplexType(Type type)
{
if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal) || type == typeof(DateTime) || type == typeof(Guid))
{
return false;
}

if (Nullable.GetUnderlyingType(type) != null)
{
return IsComplexType(Nullable.GetUnderlyingType(type));
}

if (type.IsEnum)
{
return false;
}

// If none of the above, it's a complex type
return true;
}
}

0 comments on commit 31cacce

Please sign in to comment.