encodeList function

String encodeList(
  1. List object,
  2. JsonOptions options
)

Encodes a list as JSON.

Implementation

String encodeList(List object, JsonOptions options) {
  if (object.isEmpty) return '[]';
  List<String> encoded = object
      .asMap()
      .entries
      .map((e) => encodeObject(e.value, options.extendPath(e.key.toString())))
      .toList();
  String simple = '[${encoded.join(', ')}]';
  if (simple.length < options.maxLength && options.allowCompression) {
    return simple;
  }
  String base = options.baseIndent;
  String str = '${options.indent}[';
  final opts = options.copyWith(
    depth: options.depth + 1,
    maxLineLength: options.maxLineLength - options.indent.length,
  );
  String complex = object
      .asMap()
      .entries
      .map((e) => encodeObject(e.value, opts.extendPath(e.key.toString())))
      .join(',\n$base${options.indent}');
  str = '[\n$base${options.indent}$complex\n$base]';
  return str;
}