Skip to content
Open
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
14 changes: 13 additions & 1 deletion Interfaces/Cosmos.DataTransfer.Common/DataItemJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ internal static void WriteFieldValue(Utf8JsonWriter writer, string fieldName, ob
}
else if (fieldValue is not string && fieldValue is IEnumerable children)
{
writer.WriteStartArray(propertyName);
if (!string.IsNullOrWhiteSpace(propertyName.Value))
{
writer.WriteStartArray(propertyName);
}
else
{
writer.WriteStartArray();
}

foreach (object? arrayItem in children)
{
if (arrayItem is IDataItem arrayChild)
Expand All @@ -125,6 +133,10 @@ internal static void WriteFieldValue(Utf8JsonWriter writer, string fieldName, ob
var arrayDictItem = new DictionaryDataItem(arrayDict);
WriteDataItem(writer, arrayDictItem, includeNullFields);
}
else if (arrayItem is not string && arrayItem is IEnumerable childs)
{
WriteFieldValue(writer, "", childs, includeNullFields);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR — adding support for nested (multi-dimensional) arrays makes sense.

That said, I’m a bit concerned about the current approach of handling nested arrays by calling:

WriteFieldValue(writer, "", childs, includeNullFields);

relies on:

if (!string.IsNullOrWhiteSpace(propertyName.Value)) { writer.WriteStartArray(propertyName); }
else { writer.WriteStartArray(); }

Could we refactor nested-array writing so it doesn’t depend on passing fieldName == "" and the propertyName.Value check?

For example:

introduce a dedicated helper that writes an array value (no property name), e.g. WriteArrayValue(Utf8JsonWriter writer, IEnumerable items, ...)
use that helper for nested arrays so it always calls writer.WriteStartArray() / writer.WriteEndArray()

(Optional)
The nested-array detection (arrayItem is not string && arrayItem is IEnumerable) is very broad and may accidentally treat things like byte[] as arrays of numbers. It might be worth tightening this to the specific collection types we expect to see here.

}
else if (TryGetLong(arrayItem, out var longValue))
{
writer.WriteNumberValue(longValue);
Expand Down
Loading