Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add complex json support to MicroJson #922

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static List<T> DeserializeList<T>(ArrayList array)
/// </summary>
/// <typeparam name="T">The type of objects in the list.</typeparam>
/// <param name="array">The JSON array to deserialize.</param>
/// <param name="type">The type of objects in the list as a <see cref="System.Type"/>.</param>
/// <param name="type">The type of objects in the list as a <see cref="Type"/>.</param>
/// <param name="instance"></param>
/// <returns>A list of objects of type T.</returns>
private static void DeserializeList<T>(ArrayList array, Type type, ref List<T> instance)
Expand Down 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 @@ -280,6 +288,15 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)

prop.SetValue(instance, list);
}
else if (IsComplexType(prop.PropertyType))
{
if (values[v] is Hashtable hashtableValue)
{
var 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 +306,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
<ItemGroup>
<None Include=".\Readme.md" Pack="true" PackagePath="" />
<None Include="..\..\..\icon.png" Pack="true" PackagePath="" />
<ProjectReference Include="..\..\Graphics.MicroGraphics\Driver\Graphics.MicroGraphics.csproj" />
</ItemGroup>
</Project>
Loading