< Summary

Information
Class: DotNetApiDiff.Reporting.JsonFormatter
Assembly: DotNetApiDiff
File(s): /home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/JsonFormatter.cs
Line coverage
100%
Covered lines: 84
Uncovered lines: 0
Coverable lines: 84
Total lines: 169
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Format(...)100%11100%
CreateJsonModel(...)100%11100%
ConvertToJsonDifference(...)100%11100%
get_OldAssemblyPath()100%11100%
get_NewAssemblyPath()100%11100%
get_ComparisonTimestamp()100%11100%
get_HasBreakingChanges()100%11100%
get_TotalDifferences()100%11100%
get_Summary()100%11100%
get_Added()100%11100%
get_Removed()100%11100%
get_Modified()100%11100%
get_Excluded()100%11100%
get_Moved()100%11100%
get_BreakingChanges()100%11100%
get_AddedCount()100%11100%
get_RemovedCount()100%11100%
get_ModifiedCount()100%11100%
get_BreakingChangesCount()100%11100%
get_TotalChanges()100%11100%
get_ChangeType()100%11100%
get_ElementType()100%11100%
get_ElementName()100%11100%
get_Description()100%11100%
get_IsBreakingChange()100%11100%
get_Severity()100%11100%
get_OldSignature()100%11100%
get_NewSignature()100%11100%

File(s)

/home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/JsonFormatter.cs

#LineLine coverage
 1// Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT
 2using DotNetApiDiff.Interfaces;
 3using DotNetApiDiff.Models;
 4using System.Text.Json;
 5using System.Text.Json.Serialization;
 6
 7namespace DotNetApiDiff.Reporting;
 8
 9/// <summary>
 10/// Formatter for JSON output with complete comparison details
 11/// </summary>
 12public class JsonFormatter : IReportFormatter
 13{
 14    private readonly JsonSerializerOptions _jsonOptions;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="JsonFormatter"/> class.
 18    /// </summary>
 19    /// <param name="indented">Whether to format the JSON with indentation</param>
 920    public JsonFormatter(bool indented = true)
 921    {
 922        _jsonOptions = new JsonSerializerOptions
 923        {
 924            WriteIndented = indented,
 925            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
 926            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
 927        };
 928    }
 29
 30    /// <summary>
 31    /// Formats a comparison result as JSON
 32    /// </summary>
 33    /// <param name="result">The comparison result to format</param>
 34    /// <returns>Formatted JSON output as a string</returns>
 35    public string Format(ComparisonResult result)
 736    {
 37        // Create a JSON-friendly representation of the comparison result
 738        var jsonModel = CreateJsonModel(result);
 39
 40        // Serialize to JSON
 741        return JsonSerializer.Serialize(jsonModel, _jsonOptions);
 742    }
 43
 44    private static JsonComparisonResult CreateJsonModel(ComparisonResult result)
 745    {
 46        // Create a JSON-specific model that includes all necessary information
 747        var jsonResult = new JsonComparisonResult
 748        {
 749            OldAssemblyPath = result.OldAssemblyPath,
 750            NewAssemblyPath = result.NewAssemblyPath,
 751            ComparisonTimestamp = result.ComparisonTimestamp,
 752            HasBreakingChanges = result.HasBreakingChanges,
 753            TotalDifferences = result.TotalDifferences,
 754            Summary = new JsonComparisonSummary
 755            {
 756                AddedCount = result.Summary.AddedCount,
 757                RemovedCount = result.Summary.RemovedCount,
 758                ModifiedCount = result.Summary.ModifiedCount,
 759                BreakingChangesCount = result.Summary.BreakingChangesCount,
 760                TotalChanges = result.Summary.TotalChanges
 761            }
 762        };
 63
 64        // Group differences by change type for better organization
 1465        var addedItems = result.Differences.Where(d => d.ChangeType == ChangeType.Added).ToList();
 1466        var removedItems = result.Differences.Where(d => d.ChangeType == ChangeType.Removed).ToList();
 1467        var modifiedItems = result.Differences.Where(d => d.ChangeType == ChangeType.Modified).ToList();
 1468        var excludedItems = result.Differences.Where(d => d.ChangeType == ChangeType.Excluded).ToList();
 1469        var movedItems = result.Differences.Where(d => d.ChangeType == ChangeType.Moved).ToList();
 70
 71        // Convert differences to JSON model
 772        jsonResult.Added = addedItems.Select(ConvertToJsonDifference).ToList();
 773        jsonResult.Removed = removedItems.Select(ConvertToJsonDifference).ToList();
 774        jsonResult.Modified = modifiedItems.Select(ConvertToJsonDifference).ToList();
 775        jsonResult.Excluded = excludedItems.Select(ConvertToJsonDifference).ToList();
 776        jsonResult.Moved = movedItems.Select(ConvertToJsonDifference).ToList();
 77
 78        // Add breaking changes separately for easy access
 779        jsonResult.BreakingChanges = result.Differences
 780            .Where(d => d.IsBreakingChange)
 781            .Select(ConvertToJsonDifference)
 782            .ToList();
 83
 784        return jsonResult;
 785    }
 86
 87    private static JsonApiDifference ConvertToJsonDifference(ApiDifference difference)
 1188    {
 1189        return new JsonApiDifference
 1190        {
 1191            ChangeType = difference.ChangeType.ToString(),
 1192            ElementType = difference.ElementType.ToString(),
 1193            ElementName = difference.ElementName,
 1194            Description = difference.Description,
 1195            IsBreakingChange = difference.IsBreakingChange,
 1196            Severity = difference.Severity.ToString(),
 1197            OldSignature = difference.OldSignature,
 1198            NewSignature = difference.NewSignature
 1199        };
 11100    }
 101
 102    /// <summary>
 103    /// JSON-specific representation of a comparison result
 104    /// </summary>
 105    private class JsonComparisonResult
 106    {
 21107        public string OldAssemblyPath { get; set; } = string.Empty;
 108
 21109        public string NewAssemblyPath { get; set; } = string.Empty;
 110
 14111        public DateTime ComparisonTimestamp { get; set; }
 112
 14113        public bool HasBreakingChanges { get; set; }
 114
 14115        public int TotalDifferences { get; set; }
 116
 21117        public JsonComparisonSummary Summary { get; set; } = new();
 118
 21119        public List<JsonApiDifference> Added { get; set; } = new();
 120
 21121        public List<JsonApiDifference> Removed { get; set; } = new();
 122
 21123        public List<JsonApiDifference> Modified { get; set; } = new();
 124
 21125        public List<JsonApiDifference> Excluded { get; set; } = new();
 126
 21127        public List<JsonApiDifference> Moved { get; set; } = new();
 128
 21129        public List<JsonApiDifference> BreakingChanges { get; set; } = new();
 130    }
 131
 132    /// <summary>
 133    /// JSON-specific representation of comparison summary
 134    /// </summary>
 135    private class JsonComparisonSummary
 136    {
 14137        public int AddedCount { get; set; }
 138
 14139        public int RemovedCount { get; set; }
 140
 14141        public int ModifiedCount { get; set; }
 142
 14143        public int BreakingChangesCount { get; set; }
 144
 14145        public int TotalChanges { get; set; }
 146    }
 147
 148    /// <summary>
 149    /// JSON-specific representation of an API difference
 150    /// </summary>
 151    private class JsonApiDifference
 152    {
 33153        public string ChangeType { get; set; } = string.Empty;
 154
 33155        public string ElementType { get; set; } = string.Empty;
 156
 33157        public string ElementName { get; set; } = string.Empty;
 158
 33159        public string Description { get; set; } = string.Empty;
 160
 22161        public bool IsBreakingChange { get; set; }
 162
 33163        public string Severity { get; set; } = string.Empty;
 164
 22165        public string? OldSignature { get; set; }
 166
 22167        public string? NewSignature { get; set; }
 168    }
 169}