2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2018 Intel Corporation.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2018-01-19 23:30:24 +00:00
|
|
|
|
|
|
|
#include "jsonconverter.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
2023-08-25 14:04:41 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2018-01-19 23:30:24 +00:00
|
|
|
static JsonConverter jsonConverter;
|
|
|
|
|
2023-08-25 14:10:27 +00:00
|
|
|
static const char jsonOptionHelp[] = "compact=no|yes Use compact JSON form.\n";
|
2018-01-19 23:30:24 +00:00
|
|
|
|
|
|
|
static QJsonDocument convertFromVariant(const QVariant &v)
|
|
|
|
{
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(v);
|
|
|
|
if (!doc.isObject() && !doc.isArray()) {
|
|
|
|
fprintf(stderr, "Could not convert contents to JSON.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString JsonConverter::name()
|
|
|
|
{
|
2023-08-25 14:04:41 +00:00
|
|
|
return "json"_L1;
|
2018-01-19 23:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Converter::Direction JsonConverter::directions()
|
|
|
|
{
|
|
|
|
return InOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
Converter::Options JsonConverter::outputOptions()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonConverter::optionsHelp()
|
|
|
|
{
|
2023-01-30 13:37:56 +00:00
|
|
|
return jsonOptionHelp;
|
2018-01-19 23:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonConverter::probeFile(QIODevice *f)
|
|
|
|
{
|
|
|
|
if (QFile *file = qobject_cast<QFile *>(f)) {
|
2023-08-25 14:04:41 +00:00
|
|
|
if (file->fileName().endsWith(".json"_L1))
|
2018-01-19 23:30:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f->isReadable()) {
|
|
|
|
QByteArray ba = f->peek(1);
|
|
|
|
return ba == "{" || ba == "[";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant JsonConverter::loadFile(QIODevice *f, Converter *&outputConverter)
|
|
|
|
{
|
|
|
|
if (!outputConverter)
|
|
|
|
outputConverter = this;
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc;
|
|
|
|
if (auto file = qobject_cast<QFile *>(f)) {
|
|
|
|
const char *ptr = reinterpret_cast<char *>(file->map(0, file->size()));
|
|
|
|
if (ptr)
|
|
|
|
doc = QJsonDocument::fromJson(QByteArray::fromRawData(ptr, file->size()), &error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.isNull())
|
|
|
|
doc = QJsonDocument::fromJson(f->readAll(), &error);
|
|
|
|
if (error.error) {
|
|
|
|
fprintf(stderr, "Could not parse JSON content: offset %d: %s",
|
|
|
|
error.offset, qPrintable(error.errorString()));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (outputConverter == null)
|
|
|
|
return QVariant();
|
|
|
|
return doc.toVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options)
|
|
|
|
{
|
|
|
|
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
|
|
|
|
for (const QString &s : options) {
|
2023-08-25 14:04:41 +00:00
|
|
|
if (s == "compact=no"_L1) {
|
2018-01-19 23:30:24 +00:00
|
|
|
format = QJsonDocument::Indented;
|
2023-08-25 14:04:41 +00:00
|
|
|
} else if (s == "compact=yes"_L1) {
|
2018-01-19 23:30:24 +00:00
|
|
|
format = QJsonDocument::Compact;
|
|
|
|
} else {
|
2023-01-30 13:37:56 +00:00
|
|
|
fprintf(stderr, "Unknown option '%s' to JSON output. Valid options are:\n%s",
|
|
|
|
qPrintable(s), jsonOptionHelp);
|
2018-01-19 23:30:24 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f->write(convertFromVariant(contents).toJson(format));
|
|
|
|
}
|