toJson method

String toJson(
  1. DiagnosticReport report
)

Public method or function.

Implementation

String toJson(DiagnosticReport report) {
  final map = <String, dynamic>{
    'schemaVersion': AppConstants.jsonSchemaVersion,
    'firedoctorVersion': AppConstants.version,
    'generatedAt': DateTime.now().toUtc().toIso8601String(),
    'projectName': report.projectName,
    'projectPath': report.projectPath,
    'createdAt': report.createdAt.toIso8601String(),
    'score': report.score,
    'passed': report.passed,
    'exitCode': report.exitCode,
    'mostSevereRank': report.mostSevereRank,
    'totalIssues': report.totalIssues,
    'totalErrors': report.totalErrors,
    'totalWarnings': report.totalWarnings,
    'environment': report.environment,
    if (report.firebaseVersion != null)
      'firebaseVersion': report.firebaseVersion,
    'analyzerResults': report.results
        .map(
          (r) => {
            'analyzerName': r.analyzerName,
            'status': r.status.name,
            'duration': r.duration.inMilliseconds,
            'timestamp': r.timestamp.toIso8601String(),
            'issueCount': r.issueCount,
            'errorCount': r.errorCount,
            'warningCount': r.warningCount,
            'mostSevereRank': r.mostSevereRank,
            'issues': r.issues
                .map(
                  (i) => {
                    'severity': i.severity.name,
                    'code': i.code,
                    'title': i.title,
                    'description': i.description,
                    if (i.recommendation != null)
                      'recommendation': i.recommendation,
                    if (i.filePath != null) 'filePath': i.filePath,
                    if (i.lineNumber != null) 'lineNumber': i.lineNumber,
                  },
                )
                .toList(),
          },
        )
        .toList(),
  };

  if (report.healthScore != null) {
    map['healthScore'] = report.healthScore!.toJson();
  }

  // Top-level category scores (shorthand)
  if (report.healthScore != null) {
    map['categoryScores'] = report.healthScore!.categoryScores
        .map((c) => c.toJson())
        .toList();
    map['recommendations'] = report.healthScore!.recommendations
        .map((r) => r.toJson())
        .toList();
  }

  return const JsonEncoder.withIndent('  ').convert(map);
}