jsonToDocument method

Future<void> jsonToDocument({
  1. required String path,
  2. required JsonObject json,
  3. required OnParsedCallback onParsed,
  4. String? changeRootName,
})

Implementation

Future<void> jsonToDocument({
  required String path,
  required JsonObject json,
  required OnParsedCallback onParsed,
  String? changeRootName,
}) async {
  final (id, document) = _jsonToDocument(json);

  final name = changeRootName ?? id;

  await onParsed(path, name, document);

  final rawDocumentCollections = json[metaCollections] ?? [];
  if (rawDocumentCollections is! List<dynamic>) {
    throw FormatException(
        'Wrong `$metaCollections` value=`$rawDocumentCollections` '
        'in json=`$json`');
  }

  final documentCollections = rawDocumentCollections.cast<JsonObject>();

  final documentPath = di<PathUtils>().join(path, name);

  for (final documentCollection in documentCollections) {
    final documentCollectionName =
        documentCollection[metaName] as String? ?? '';
    if (documentCollectionName.isEmpty) {
      continue;
    }

    await jsonToCollection(
      path: documentPath,
      json: documentCollection,
      onParsed: onParsed,
    );
  }
}