Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSpdxSerializer.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmSpdxSerializer.h"
4
5
#include <utility>
6
#include <vector>
7
8
#include <cm3p/json/writer.h>
9
10
#include "cmSbomObject.h"
11
12
cmSpdxSerializer::cmSpdxSerializer()
13
0
{
14
0
  Json::StreamWriterBuilder builder = Json::StreamWriterBuilder();
15
0
  builder["indentation"] = "  ";
16
0
  Writer.reset(builder.newStreamWriter());
17
0
}
18
19
void cmSpdxSerializer::BeginObject()
20
0
{
21
0
  CurrentValue = Json::objectValue;
22
0
}
23
24
void cmSpdxSerializer::BeginArray()
25
0
{
26
0
  CurrentValue = Json::arrayValue;
27
0
}
28
29
void cmSpdxSerializer::EndObject()
30
0
{
31
0
}
32
33
void cmSpdxSerializer::EndArray()
34
0
{
35
0
}
36
37
void cmSpdxSerializer::AddReference(std::string const& id)
38
0
{
39
0
  CurrentValue = id;
40
0
}
41
42
void cmSpdxSerializer::AddString(std::string const& key,
43
                                 std::string const& value)
44
0
{
45
0
  if (!value.empty()) {
46
0
    CurrentValue[key] = value;
47
0
  }
48
0
}
49
50
void cmSpdxSerializer::AddVisitable(std::string const& key,
51
                                    cmSbomObject const& visitable)
52
0
{
53
0
  if (visitable.IsNull()) {
54
0
    return;
55
0
  }
56
57
0
  Json::Value parentValue = std::move(CurrentValue);
58
0
  visitable.Serialize(*this);
59
0
  Json::Value childValue = std::move(CurrentValue);
60
61
0
  CurrentValue = std::move(parentValue);
62
0
  CurrentValue[key] = std::move(childValue);
63
0
}
64
65
void cmSpdxSerializer::AddVectorIfPresent(std::string const& key,
66
                                          std::vector<cmSbomObject> const& vec)
67
0
{
68
0
  if (vec.empty()) {
69
0
    return;
70
0
  }
71
0
  Json::Value parentValue = std::move(CurrentValue);
72
0
  Json::Value childValue(Json::arrayValue);
73
74
0
  for (auto const& item : vec) {
75
0
    if (item.IsNull()) {
76
0
      continue;
77
0
    }
78
79
0
    item.Serialize(*this);
80
81
0
    if (!CurrentValue.isNull()) {
82
0
      childValue.append(std::move(CurrentValue));
83
0
    }
84
0
  }
85
86
0
  CurrentValue = std::move(parentValue);
87
0
  CurrentValue[key] = std::move(childValue);
88
0
}
89
90
void cmSpdxSerializer::AddVectorIfPresent(std::string const& key,
91
                                          std::vector<std::string> const& vec)
92
0
{
93
0
  if (vec.empty()) {
94
0
    return;
95
0
  }
96
0
  Json::Value parentValue = std::move(CurrentValue);
97
0
  Json::Value childValue(Json::arrayValue);
98
99
0
  for (auto const& item : vec) {
100
0
    if (item.empty()) {
101
0
      continue;
102
0
    }
103
0
    childValue.append(item);
104
0
  }
105
106
0
  CurrentValue = std::move(parentValue);
107
0
  CurrentValue[key] = std::move(childValue);
108
0
}
109
110
bool cmSpdxSerializer::WriteSbom(std::ostream& os,
111
                                 cmSbomObject const& document)
112
0
{
113
0
  if (document.IsNull()) {
114
0
    return false;
115
0
  }
116
117
0
  document.Serialize(*this);
118
0
  Writer->write(CurrentValue, &os);
119
0
  return os.good();
120
0
}