encodeObjectAsListItem function
void
encodeObjectAsListItem(
- Map<
String, Object?> obj, - LineWriter writer,
- int depth,
- ResolvedEncodeOptions options,
Encodes an object as a list item
Implementation
void encodeObjectAsListItem(Map<String, Object?> obj, LineWriter writer,
int depth, ResolvedEncodeOptions options) {
final keys = obj.keys.toList();
if (keys.isEmpty) {
writer.push(depth, listItemMarker);
return;
}
// First key-value on the same line as "- "
final firstKey = keys[0];
final encodedKey = encodeKey(firstKey);
final firstValue = obj[firstKey];
if (isJsonPrimitive(firstValue)) {
writer.pushListItem(depth,
'$encodedKey: ${encodePrimitive(firstValue, options.delimiter)}');
} else if (isJsonArray(firstValue)) {
final firstValueList = firstValue as List<Object?>;
if (isArrayOfPrimitives(firstValueList)) {
// Inline format for primitive arrays
final formatted = encodeInlineArrayLine(
firstValueList, options.delimiter, firstKey, options.lengthMarker);
writer.pushListItem(depth, formatted);
} else if (isArrayOfObjects(firstValueList)) {
// Check if array of objects can use tabular format
final header =
extractTabularHeader(firstValueList.cast<Map<String, Object?>>());
if (header != null) {
// Tabular format for uniform arrays of objects
final formattedHeader = formatHeader(firstValueList.length,
key: firstKey,
fields: header,
delimiter: options.delimiter,
lengthMarker: options.lengthMarker);
writer.pushListItem(depth, formattedHeader);
writeTabularRows(firstValueList.cast<Map<String, Object?>>(), header,
writer, depth + 1, options);
} else {
// Fall back to list format for non-uniform arrays of objects
writer.pushListItem(depth, '$encodedKey[${firstValueList.length}]:');
for (final item in firstValueList) {
encodeObjectAsListItem(
item as Map<String, Object?>, writer, depth + 1, options);
}
}
} else {
// Complex arrays on separate lines (array of arrays, etc.)
writer.pushListItem(depth, '$encodedKey[${firstValueList.length}]:');
// Encode array contents at depth + 1
for (final item in firstValueList) {
encodeListItemValue(item, writer, depth + 1, options);
}
}
} else if (isJsonObject(firstValue)) {
final nestedKeys = (firstValue as Map<String, Object?>).keys;
if (nestedKeys.isEmpty) {
writer.pushListItem(depth, '$encodedKey:');
} else {
writer.pushListItem(depth, '$encodedKey:');
encodeObject(firstValue, writer, depth + 2, options);
}
}
// Remaining keys on indented lines
for (int i = 1; i < keys.length; i++) {
final key = keys[i];
encodeKeyValuePair(key, obj[key], writer, depth + 1, options);
}
}