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