encode method

String encode(
  1. dynamic obj, {
  2. bool beautify = false,
  3. int indent = 2,
  4. bool quoteMapKeys = false,
  5. dynamic jsonBooleans = false,
})

Insert object to encode it as GSON

Implementation

String encode(dynamic obj,
    {bool beautify = false,
    int indent = 2,
    bool quoteMapKeys = false,
    jsonBooleans = false}) {
  if (beautify) {
    return beautyEncode(obj,
            indent: indent,
            quoteMapKeys: quoteMapKeys,
            jsonBooleans: jsonBooleans)
        .join('\n');
  }
  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) {
    var cont = [];
    obj.forEach((e) {
      cont.add(encode(e,
          indent: indent,
          beautify: beautify,
          quoteMapKeys: quoteMapKeys,
          jsonBooleans: jsonBooleans));
    });
    return '[' + cont.join(',') + ']';
  }
  if (obj is Map) {
    var cont = [];
    obj.forEach((k, v) {
      if (quoteMapKeys) {
        k = json.encode(k);
      }
      cont.add(
          '${k}:${encode(v, indent: indent, beautify: beautify, quoteMapKeys: quoteMapKeys, jsonBooleans: jsonBooleans)}');
    });
    return '{' + cont.join(',') + '}';
  }
  return json.encode(obj);
}