qt6windows7/examples/corelib/serialization/convert/jsonconverter.cpp

96 lines
2.5 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// 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>
2023-11-02 01:02:52 +08:00
using namespace Qt::StringLiterals;
2023-10-30 06:33:08 +08:00
static JsonConverter jsonConverter;
2023-11-02 01:02:52 +08:00
static const char jsonOptionHelp[] = "compact=no|yes Use compact JSON form.\n";
2023-10-30 06:33:08 +08:00
static QJsonDocument convertFromVariant(const QVariant &v)
{
QJsonDocument doc = QJsonDocument::fromVariant(v);
2023-12-05 01:42:35 +08:00
if (!doc.isObject() && !doc.isArray())
qFatal("Could not convert contents to JSON.");
2023-10-30 06:33:08 +08:00
return doc;
}
2023-11-02 01:02:52 +08:00
QString JsonConverter::name() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return "json"_L1;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
Converter::Directions JsonConverter::directions() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return Direction::InOut;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
const char *JsonConverter::optionsHelp() const
2023-10-30 06:33:08 +08:00
{
return jsonOptionHelp;
}
2023-11-02 01:02:52 +08:00
bool JsonConverter::probeFile(QIODevice *f) const
2023-10-30 06:33:08 +08:00
{
if (QFile *file = qobject_cast<QFile *>(f)) {
2023-11-02 01:02:52 +08:00
if (file->fileName().endsWith(".json"_L1))
2023-10-30 06:33:08 +08:00
return true;
}
if (f->isReadable()) {
QByteArray ba = f->peek(1);
return ba == "{" || ba == "[";
}
return false;
}
2023-11-02 01:02:52 +08:00
QVariant JsonConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
2023-10-30 06:33:08 +08:00
{
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) {
2023-12-05 01:42:35 +08:00
qFatal("Could not parse JSON content: offset %d: %s",
error.offset, qPrintable(error.errorString()));
2023-10-30 06:33:08 +08:00
}
2023-12-05 01:42:35 +08:00
if (isNull(outputConverter))
2023-10-30 06:33:08 +08:00
return QVariant();
return doc.toVariant();
}
2023-11-02 01:02:52 +08:00
void JsonConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
2023-10-30 06:33:08 +08:00
{
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
for (const QString &s : options) {
2023-11-02 01:02:52 +08:00
if (s == "compact=no"_L1) {
2023-10-30 06:33:08 +08:00
format = QJsonDocument::Indented;
2023-11-02 01:02:52 +08:00
} else if (s == "compact=yes"_L1) {
2023-10-30 06:33:08 +08:00
format = QJsonDocument::Compact;
} else {
2023-12-05 01:42:35 +08:00
qFatal("Unknown option '%s' to JSON output. Valid options are:\n%s",
qPrintable(s), jsonOptionHelp);
2023-10-30 06:33:08 +08:00
}
}
f->write(convertFromVariant(contents).toJson(format));
}