Skip to content

Commit

Permalink
Fixes for nested array handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Oct 18, 2024
1 parent a312834 commit 9ed987c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
76 changes: 59 additions & 17 deletions JsonFlatFileDataStore/ExpandoObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,66 @@ private static void AddPropertyToExpando(IDictionary<string, object> expando, st
var nestedArray = new List<object>();
foreach (var nestedArrayElement in arrayElement.EnumerateArray())
{
if (nestedArrayElement.ValueKind == JsonValueKind.Object)
switch (nestedArrayElement.ValueKind)
{
var nestedExpandoInNestedArray = new ExpandoObject();
var nestedDictionaryInNestedArray = (IDictionary<string, object>)nestedExpandoInNestedArray;
foreach (var nestedPropertyInNestedArray in nestedArrayElement.EnumerateObject())
{
AddPropertyToExpando(nestedDictionaryInNestedArray, nestedPropertyInNestedArray.Name, nestedPropertyInNestedArray.Value);
}
nestedArray.Add(nestedExpandoInNestedArray);
}
else if (nestedArrayElement.ValueKind == JsonValueKind.Array)
{
// Recursively handle deeper nested arrays
nestedArray.Add(ProcessNestedArray(nestedArrayElement));
}
else
{
nestedArray.Add(nestedArrayElement.ToString());
case JsonValueKind.Undefined:
case JsonValueKind.Null:
nestedArray.Add(null);
break;

case JsonValueKind.False:
nestedArray.Add(false);
break;

case JsonValueKind.True:
nestedArray.Add(true);
break;

case JsonValueKind.Number:
if (nestedArrayElement.TryGetInt32(out int nestedArrayElementIntValue))
{
nestedArray.Add(nestedArrayElementIntValue);
}
else if (nestedArrayElement.TryGetInt64(out long longValue))
{
nestedArray.Add(longValue);
}
else if (nestedArrayElement.TryGetDouble(out double doubleValue))
{
nestedArray.Add(doubleValue);
}
else if (nestedArrayElement.TryGetDecimal(out decimal decimalValue))
{
nestedArray.Add(decimalValue);
}
else
{
throw new JsonException("Unsupported numeric type");
}
break;

case JsonValueKind.String:
nestedArray.Add(nestedArrayElement.GetString());
break;

case JsonValueKind.Object:

var nestedExpandoInNestedArray = new ExpandoObject();
var nestedDictionaryInNestedArray = (IDictionary<string, object>)nestedExpandoInNestedArray;
foreach (var nestedPropertyInNestedArray in nestedArrayElement.EnumerateObject())
{
AddPropertyToExpando(nestedDictionaryInNestedArray,
nestedPropertyInNestedArray.Name,
nestedPropertyInNestedArray.Value);
}
nestedArray.Add(nestedExpandoInNestedArray);
break;

case JsonValueKind.Array:
// Recursively handle deeper nested arrays
nestedArray.Add(ProcessNestedArray(nestedArrayElement));
break;

}
}
arrayValues.Add(nestedArray);
Expand Down
15 changes: 14 additions & 1 deletion JsonFlatFileDataStore/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,20 @@ private static void HandleTypedEnumerable(object source, object destination, dyn

if (targetType.GetTypeInfo().IsValueType || targetType == typeof(string) || IsDictionary(targetType) || IsEnumerable(targetType))
targetArray[i] = sourceValue;
else if (IsDictionary(type))

Check failure on line 243 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The name 'type' does not exist in the current context

Check failure on line 243 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The name 'type' does not exist in the current context

Check failure on line 243 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build-windows

The name 'type' does not exist in the current context

Check failure on line 243 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build-windows

The name 'type' does not exist in the current context
targetArray[i] = sourceValue; // TODO: Should we copy child items instead of just replacing?
else if (IsEnumerable(type))

Check failure on line 245 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The name 'type' does not exist in the current context

Check failure on line 245 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The name 'type' does not exist in the current context

Check failure on line 245 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build-windows

The name 'type' does not exist in the current context

Check failure on line 245 in JsonFlatFileDataStore/ObjectExtensions.cs

View workflow job for this annotation

GitHub Actions / build-windows

The name 'type' does not exist in the current context
targetArray[i] = sourceValue; // TODO: Should we copy child items instead of just replacing?
else
CopyProperties(sourceValue, targetArray[i]);
}

// TODO: What about this? This is not mentioned in the documentation?
// If list are not equal length, erase items from the end of the source
// while (targetArray.Count > sourceArray.Count)
// {
// targetArray.RemoveAt(targetArray.Count - 1);
// }
}

private static void HandleTypedDictionary(object source, object destination, PropertyInfo targetProperty, dynamic srcProp)
Expand Down Expand Up @@ -409,7 +420,9 @@ private static IEnumerable<dynamic> GetProperties(object source)
return expandoObject
.Select(i => new
{
Name = i.Key, Value = i.Value, PropertyType = i.Value?.GetType()
Name = i.Key,
Value = i.Value,
PropertyType = i.Value?.GetType()
})
.ToList();
}
Expand Down

0 comments on commit 9ed987c

Please sign in to comment.