1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "jsongenerator.h"
5
6#include <QtCore/qjsonarray.h>
7#include <QtCore/qjsondocument.h>
8#include <QtCore/qjsonobject.h>
9
10#include <iostream>
11
12using namespace Qt::Literals::StringLiterals;
13
14namespace JsonGenerator {
15
16static QJsonObject generate(Package package)
17{
18 QJsonObject obj;
19
20 obj.insert(key: u"Id"_s, value: package.id);
21 obj.insert(key: u"Path"_s, value: package.path);
22 obj.insert(key: u"Files"_s, value: package.files.join(sep: QLatin1Char(' ')));
23 obj.insert(key: u"QDocModule"_s, value: package.qdocModule);
24 obj.insert(key: u"Name"_s, value: package.name);
25 obj.insert(key: u"QtUsage"_s, value: package.qtUsage);
26 obj.insert(key: u"SecurityCritical"_s, value: package.securityCritical);
27 obj.insert(key: u"QtParts"_s, value: QJsonArray::fromStringList(list: package.qtParts));
28
29 obj.insert(key: u"Description"_s, value: package.description);
30 obj.insert(key: u"Homepage"_s, value: package.homepage);
31 obj.insert(key: u"Version"_s, value: package.version);
32 obj.insert(key: u"DownloadLocation"_s, value: package.downloadLocation);
33
34 obj.insert(key: u"License"_s, value: package.license);
35 obj.insert(key: u"LicenseId"_s, value: package.licenseId);
36 if (package.licenseFiles.isEmpty())
37 obj.insert(key: u"LicenseFile"_s, value: QString());
38 else if (package.licenseFiles.size() == 1)
39 obj.insert(key: u"LicenseFile"_s, value: package.licenseFiles.first());
40 else
41 obj.insert(key: u"LicenseFiles"_s,
42 value: QJsonArray::fromStringList(list: package.licenseFiles));
43
44 obj.insert(key: u"Copyright"_s, value: package.copyright);
45 obj.insert(key: u"CopyrightFile"_s, value: package.copyrightFile);
46 obj.insert(key: u"PackageComment"_s, value: package.packageComment);
47
48 return obj;
49}
50
51void generate(QTextStream &out, const QList<Package> &packages, LogLevel logLevel)
52{
53 if (logLevel == VerboseLog)
54 std::cerr << qPrintable(tr("Generating json...\n"));
55
56 QJsonDocument document;
57 QJsonArray array;
58 for (const Package &package : packages)
59 array.append(value: generate(package));
60 document.setArray(array);
61
62 out << document.toJson();
63}
64
65} // namespace JsonGenerator
66

source code of qttools/src/qtattributionsscanner/jsongenerator.cpp