decodeImportAny function

Object decodeImportAny(
  1. Object srcData
)

Decode the exported data to be imported.

Returns a list of data or a map.

Implementation

Object decodeImportAny(Object srcData) {
  Map mapImport(Map map) {
    return map;
  }

  Object linesImport(List lines) {
    return lines;
  }

  try {
    if (srcData is Map) {
      return mapImport(srcData);
    } else if (srcData is Iterable) {
      if (srcData.isNotEmpty) {
        // First is meta
        if (srcData.first is Map) {
          return linesImport(srcData.toList());
        } else if (srcData.first is String) {
          // list of json string?
          var srcLines = srcData.map((e) => jsonDecode(e.toString())).toList();
          return linesImport(srcLines);
        }
      }
    } else if (srcData is String) {
      // handle multiple json encoding
      Object? srcDecoded;
      try {
        // json ?
        srcDecoded = jsonDecode(srcData.trim()) as Object?;
      } catch (_) {}
      if (srcDecoded is Map || srcDecoded is List) {
        return srcDecoded!;
      }

      var lines = LineSplitter.split(srcData.trim());
      if (lines.isNotEmpty) {
        var srcLines = lines.map((e) => jsonDecode(e)).toList();
        return linesImport(srcLines);
      }
    }
  } catch (e) {
    if (isDebug) {
      // ignore: avoid_print
      print('import error $e');
    }
    throw FormatException('decode invalid export format (error: $e)');
  }
  throw FormatException(
      'decode invalid export format (${srcData.runtimeType})');
}