beautyEncode method

List<String> beautyEncode(
  1. dynamic obj, {
  2. int indent = 2,
  3. bool quoteMapKeys = false,
  4. dynamic jsonBooleans = false,
})

encode beautified gson

Implementation

List<String> beautyEncode(dynamic obj,
    {int indent = 2, bool quoteMapKeys = false, jsonBooleans = false}) {
  if (obj is bool) {
    return [jsonBooleans ? (obj ? 'true' : 'false') : (obj ? '1b' : '0b')];
  }
  if (obj is double) {
    return [obj.toString() + 'd'];
  }
  if (obj is GsonValue) {
    return [obj.toString()];
  }
  if (obj is List) {
    if (obj.isEmpty) return ['[]'];
    var cont = ['['];
    for (var c = 0; c < obj.length; c++) {
      var e = beautyEncode(obj[c],
          indent: indent,
          quoteMapKeys: quoteMapKeys,
          jsonBooleans: jsonBooleans);
      for (var i = 0; i < e.length; i++) {
        cont.add(_repeatString(' ', indent) +
            e[i] +
            (i == e.length - 1 && c < obj.length - 1 ? ',' : ''));
      }
    }
    cont.add(']');
    return cont;
  }
  if (obj is Map) {
    if (obj.isEmpty) return ['{}'];
    var cont = ['{'];
    var c = 0;
    obj.forEach((k, v) {
      if (quoteMapKeys) {
        k = json.encode(k);
      }
      var e = beautyEncode(v,
          indent: indent,
          quoteMapKeys: quoteMapKeys,
          jsonBooleans: jsonBooleans);
      for (var i = 0; i < e.length; i++) {
        cont.add(_repeatString(' ', indent) +
            (i == 0 ? k + ': ' : '') +
            e[i] +
            (i == e.length - 1 && c < obj.length - 1 ? ',' : ''));
      }
      c++;
    });
    cont.add('}');
    return cont;
  }
  return [json.encode(obj)];
}