encodeArray function

void encodeArray(
  1. String? key,
  2. List<Object?> value,
  3. LineWriter writer,
  4. int depth,
  5. ResolvedEncodeOptions options,
)

Encodes an array

Implementation

void encodeArray(String? key, List<Object?> value, LineWriter writer, int depth,
    ResolvedEncodeOptions options) {
  if (value.isEmpty) {
    final header = formatHeader(0,
        key: key,
        delimiter: options.delimiter,
        lengthMarker: options.lengthMarker);
    writer.push(depth, header);
    return;
  }

  // Primitive array
  if (isArrayOfPrimitives(value)) {
    final formatted = encodeInlineArrayLine(
        value, options.delimiter, key, options.lengthMarker);
    writer.push(depth, formatted);
    return;
  }

  // Array of arrays (all primitives)
  if (isArrayOfArrays(value)) {
    final allPrimitiveArrays =
        value.every((arr) => isArrayOfPrimitives(arr as List<Object?>));
    if (allPrimitiveArrays) {
      encodeArrayOfArraysAsListItems(
          key, value.cast<List<Object?>>(), writer, depth, options);
      return;
    }
  }

  // Array of objects
  if (isArrayOfObjects(value)) {
    final header = extractTabularHeader(value.cast<Map<String, Object?>>());
    if (header != null) {
      encodeArrayOfObjectsAsTabular(key, value.cast<Map<String, Object?>>(),
          header, writer, depth, options);
    } else {
      encodeMixedArrayAsListItems(key, value, writer, depth, options);
    }
    return;
  }

  // Mixed array: fallback to expanded format
  encodeMixedArrayAsListItems(key, value, writer, depth, options);
}