fromJson method

  1. @override
Map<String, DynamicMap> fromJson(
  1. dynamic json
)
override

Json data List can be retrieved in NoSqlDatabase Map.

Specify the ID of the document as the key of Map and the data of the document as the value in DynamicMap.

JsonのデータListNoSqlDatabaseで取得可能なMap

MapのキーにドキュメントのIDを指定し、値にドキュメントのデータをDynamicMapで指定します。

Implementation

@override
Map<String, DynamicMap> fromJson(dynamic json) {
  final result = <String, DynamicMap>{};
  if (json is List) {
    assert(idKey.isNotEmpty, "If Json is a List, [idKey] must be specified.");
    for (final tmp in json) {
      if (tmp is! Map) {
        continue;
      }
      final converted = <String, dynamic>{};
      for (final map in tmp.entries) {
        final r = _toAny(map.value);
        final id = map.key.toString().trim();
        if (r != null) {
          if (converted.containsKey(id)) {
            if (converted[id] is List) {
              converted[id].add(r);
            } else {
              converted[id] = [converted[id], r];
            }
          } else {
            converted[id] = r;
          }
        }
      }
      final key = converted.get(idKey!, "").toString().trim();
      result[key] = _merged(converted);
    }
  } else if (json is Map) {
    for (final tmp in json.entries) {
      final value = tmp.value;
      if (value is! Map) {
        continue;
      }
      final converted = <String, dynamic>{};
      for (final map in value.entries) {
        final r = _toAny(map.value);
        final id = map.key.toString().trim();
        if (r != null) {
          if (converted.containsKey(id)) {
            if (converted[id] is List) {
              converted[id].add(r);
            } else {
              converted[id] = [converted[id], r];
            }
          } else {
            converted[id] = r;
          }
        } else if (converted.containsKey(id)) {
          if (converted[id] is! List) {
            converted[id] = [converted[id]];
          }
        }
      }
      final key = tmp.key.trim();
      result[key] = _merged(converted);
    }
  } else {
    throw Exception("Incorrect data.");
  }
  return result;
}