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) {
  if (json is! Map) {
    throw Exception("The data is not a Map.");
  }
  final documentId = this.documentId ?? source.hashCode.toString();
  final converted = <String, dynamic>{};
  for (final map in json.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]];
      }
    }
  }
  return {
    documentId: _merged(converted),
  };
}