< Summary

Information
Class: Spdx3.Serialization.Writer
Assembly: Spdx3
File(s): /home/runner/work/Spdx3/Spdx3/Spdx3/Serialization/Writer.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 71
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
get_Options()100%11100%
.ctor(...)100%11100%
WriteString()100%11100%
WriteFileStream(...)100%11100%
WriteFileName(...)100%11100%

File(s)

/home/runner/work/Spdx3/Spdx3/Spdx3/Serialization/Writer.cs

#LineLine coverage
 1using System.Text.Json;
 2using Spdx3.Utility;
 3using File = Spdx3.Model.Software.Classes.File;
 4
 5namespace Spdx3.Serialization;
 6
 7public class Writer
 8{
 9    /// <summary>
 10    ///     The catalog we're writing out
 11    /// </summary>
 12    private readonly Catalog _catalog;
 13
 14    /// <summary>
 15    ///     Serialization options
 16    /// </summary>
 1617    private JsonSerializerOptions Options { get; } = new()
 418    {
 419        WriteIndented = false,
 420        MaxDepth = 99
 421    };
 22
 23    /// <summary>
 24    ///     Constructor
 25    /// </summary>
 26    /// <param name="catalog">The catalog of objects to write</param>
 427    public Writer(Catalog catalog)
 28    {
 429        _catalog = catalog;
 430        Options.Converters.Add(new SpdxModelConverterFactory());
 431        Options.Converters.Add(new SpdxWrapperConverterFactory());
 432    }
 33
 34    /// <summary>
 35    ///     Write the catalog to a string that is the content of an SPDX 3.0.1 compliant JSON file.
 36    /// </summary>
 37    /// <returns>The catalog as a string</returns>
 38    public string WriteString()
 39    {
 40        // ReSharper disable once SuggestVarOrType_BuiltInTypes
 441        object obj = new SpdxWrapper(_catalog);
 42        // ReSharper disable once RedundantTypeArgumentsOfMethod
 443        var result = JsonSerializer.Serialize<object>(obj, Options);
 44
 445        return result;
 46    }
 47
 48    /// <summary>
 49    ///     Write the catalog to an SPDX 3.0.1 compliant JSON file
 50    /// </summary>
 51    /// <param name="fileStream">The FileStream to write to</param>
 52    /// <returns>The file we wrote to</returns>
 53    public FileStream WriteFileStream(FileStream fileStream)
 54    {
 255        using var writer = new StreamWriter(fileStream);
 256        writer.Write(WriteString());
 257        return fileStream;
 258    }
 59
 60    /// <summary>
 61    ///     Create a new file and write the catalog to it as SPDX 3.0.1 compliant JSON
 62    /// </summary>
 63    /// <param name="fileName">The name of the new JSON file to create</param>
 64    /// <returns>The file we created and wrote to</returns>
 65    public FileInfo WriteFileName(string fileName)
 66    {
 167        var fileStream = new FileStream(fileName, FileMode.Create);
 168        WriteFileStream(fileStream);
 169        return new FileInfo(fileName);
 70    }
 71}