writeJsonConst function

void writeJsonConst(
  1. IndentingWriter out,
  2. Object? val
)

Writes JSON data as a Dart constant expression. Accepts null, bool, num, String, and maps and lists.

Implementation

void writeJsonConst(IndentingWriter out, Object? val) {
  if (val is Map) {
    if (val.values.any(_nonEmptyListOrMap)) {
      out.addBlock('{', '}', () => _writeMapItems(out, val, vertical: true),
          endWithNewline: false);
    } else {
      out.print('{');
      _writeMapItems(out, val);
      out.print('}');
    }
  } else if (val is List) {
    if (val.any(_nonEmptyListOrMap)) {
      out.addBlock('[', ']', () => _writeListItems(out, val, vertical: true),
          endWithNewline: false);
    } else {
      out.print('[');
      _writeListItems(out, val);
      out.print(']');
    }
  } else if (val is String) {
    _writeString(out, val);
  } else if (val is num || val is bool) {
    out.print(val.toString());
  } else if (val == null) {
    out.print('null');
  } else {
    throw 'not JSON: $val';
  }
}