encodeMap function
Encodes a map as JSON. This should probably be a Map<String, dynamic>, but
this is not a constraint that is checked.
Consider using niceJson()
instead.
Implementation
String encodeMap(Map object, JsonOptions options) {
if (object.isEmpty) return '{}';
List<String> encoded =
object.entries.map((e) => encodeMapEntry(e, options)).toList();
String simple = '{${encoded.join(', ')}}';
if (simple.length < options.maxLength && options.allowCompression) {
return simple;
}
String indent = options.baseIndent;
String str = '${options.indent}[';
final opts = options.copyWith(
depth: options.depth + 1,
maxLineLength: options.maxLineLength - options.indent.length,
);
String complex = object.entries
.map((e) => encodeMapEntry(e, opts))
.join(',\n$indent${options.indent}');
str = '{\n$indent${options.indent}$complex\n$indent}';
return str;
}