The Converter class, in the eponymous example, had a null member variable that wasn't a nullptr - it pointed to an instance of NullConverter - so that other converters could test whether a Converter * they'd been passed was null (in the sense of pointing to a NullConverter). This, however, was susceptible to misreading - I misread one such comparison as a nullptr check and thus thought it redundant with an earlier actual nullptr check. To spare future readers similar confusion, replace the public static member variable with a protected (since only other derived classes need it) static predicate, to at least give the reader a clue that this is using the word null in a class-specific sense. Pick-to: 6.5 Task-number: QTBUG-111228 Change-Id: I1e4f494b303d1bf90107f8c6fa3a4a22f6d81b90 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 0b1670134b43ba390f35b52d2458e4269a804de3) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
// Copyright (C) 2018 Intel Corporation.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
#include "jsonconverter.h"
|
|
|
|
#include <QFile>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
static JsonConverter jsonConverter;
|
|
|
|
static const char jsonOptionHelp[] = "compact=no|yes Use compact JSON form.\n";
|
|
|
|
static QJsonDocument convertFromVariant(const QVariant &v)
|
|
{
|
|
QJsonDocument doc = QJsonDocument::fromVariant(v);
|
|
if (!doc.isObject() && !doc.isArray())
|
|
qFatal("Could not convert contents to JSON.");
|
|
return doc;
|
|
}
|
|
|
|
QString JsonConverter::name() const
|
|
{
|
|
return "json"_L1;
|
|
}
|
|
|
|
Converter::Directions JsonConverter::directions() const
|
|
{
|
|
return Direction::InOut;
|
|
}
|
|
|
|
Converter::Options JsonConverter::outputOptions() const
|
|
{
|
|
return {};
|
|
}
|
|
|
|
const char *JsonConverter::optionsHelp() const
|
|
{
|
|
return jsonOptionHelp;
|
|
}
|
|
|
|
bool JsonConverter::probeFile(QIODevice *f) const
|
|
{
|
|
if (QFile *file = qobject_cast<QFile *>(f)) {
|
|
if (file->fileName().endsWith(".json"_L1))
|
|
return true;
|
|
}
|
|
|
|
if (f->isReadable()) {
|
|
QByteArray ba = f->peek(1);
|
|
return ba == "{" || ba == "[";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QVariant JsonConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
|
|
{
|
|
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) {
|
|
qFatal("Could not parse JSON content: offset %d: %s",
|
|
error.offset, qPrintable(error.errorString()));
|
|
}
|
|
if (isNull(outputConverter))
|
|
return QVariant();
|
|
return doc.toVariant();
|
|
}
|
|
|
|
void JsonConverter::saveFile(QIODevice *f, const QVariant &contents,
|
|
const QStringList &options) const
|
|
{
|
|
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
|
|
for (const QString &s : options) {
|
|
if (s == "compact=no"_L1) {
|
|
format = QJsonDocument::Indented;
|
|
} else if (s == "compact=yes"_L1) {
|
|
format = QJsonDocument::Compact;
|
|
} else {
|
|
qFatal("Unknown option '%s' to JSON output. Valid options are:\n%s",
|
|
qPrintable(s), jsonOptionHelp);
|
|
}
|
|
}
|
|
|
|
f->write(convertFromVariant(contents).toJson(format));
|
|
}
|