encodeJsonl function

String encodeJsonl(
  1. Object data
)

Encodes a map, a JsonlEncodable model, or an iterable of either as JSONL.

Each top-level item becomes one line. Nested maps and lists stay inside their containing record.

Implementation

String encodeJsonl(Object data) {
  final records = data is Map || data is JsonlEncodable
      ? <Object?>[data]
      : data is Iterable
          ? data.cast<Object?>()
          : throw ArgumentError.value(
              data,
              'data',
              'Expected a Map, JsonlEncodable model, or an Iterable of them.',
            );

  final buffer = StringBuffer();
  for (final record in records) {
    final map = _recordToMap(record);
    buffer.writeln(jsonEncode(map));
  }
  return buffer.toString();
}