AstBundle.fromJson constructor

AstBundle.fromJson(
  1. Map<String, dynamic> json
)

Deserializes a bundle from a JSON map.

Throws ArgumentD4rtException if required fields are missing or module data is invalid.

Implementation

factory AstBundle.fromJson(Map<String, dynamic> json) {
  _requireString(json, AstBundleFormat.keyVersion, 'bundle JSON');
  final entryPointUri = _requireString(
    json,
    AstBundleFormat.keyEntryPoint,
    'bundle JSON',
  );

  final modulesJson = json[AstBundleFormat.keyModules];
  if (modulesJson is! Map<String, dynamic>) {
    throw ArgumentD4rtException(
      'Invalid bundle JSON: '
      '"${AstBundleFormat.keyModules}" must be a JSON object',
    );
  }

  final modules = <String, SCompilationUnit>{};
  for (final entry in modulesJson.entries) {
    if (entry.value is! Map<String, dynamic>) {
      throw ArgumentD4rtException(
        'Invalid bundle JSON: '
        'module "${entry.key}" is not a valid AST object',
      );
    }
    modules[entry.key] = SCompilationUnit.fromJson(
      entry.value as Map<String, dynamic>,
    );
  }

  // Parse optional sources
  Map<String, String>? sources;
  final sourcesJson = json[AstBundleFormat.keySources];
  if (sourcesJson is Map<String, dynamic>) {
    sources = sourcesJson.map((k, v) => MapEntry(k, v.toString()));
  }

  return AstBundle(
    entryPointUri: entryPointUri,
    modules: modules,
    sources: sources,
  );
}