decompressJson function

Map<String, dynamic> decompressJson(
  1. Map<String, dynamic> json, {
  2. bool ignoreWarnings = false,
  3. bool keepUnknownKeys = true,
})

Decompress a json object or just return it if it's not compressed

Implementation

Map<String, dynamic> decompressJson(Map<String, dynamic> json,
    {bool ignoreWarnings = false, bool keepUnknownKeys = true}) {
  if (json.containsKey("_k0")) {
    String s = "";
    int l = json.length;
    for (int i = 0; i < l; i++) {
      if (json.containsKey("_k$i")) {
        dynamic m = json["_k$i"];

        if (m is String) {
          s += m;
        } else {
          if (!ignoreWarnings) {
            print(
                "Failed to decompress chunk $i. It was not a string but a ${m?.runtimeType ?? "null"}");
          }
          break;
        }
      } else {
        break;
      }
    }

    Map<String, dynamic> out = forceJsonDecode(decompress(s));

    if (keepUnknownKeys) {
      out = out.flattened();
      json = json.flattened();
      json.forEach((key, value) {
        if (!key.startsWith("_k")) {
          out[key] = value;
        }
      });
      json = json.expanded();
      out = out.expanded();
    }

    return out;
  }

  return json;
}