2022-05-27 02:48:54 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-05-28 23:08:29 +05:30
|
|
|
|
|
|
|
#include "Json.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace Json
|
|
|
|
{
|
|
|
|
void write(const QJsonDocument &doc, const QString &filename)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
FS::write(filename, doc.toJson());
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
void write(const QJsonObject &object, const QString &filename)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
write(QJsonDocument(object), filename);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
void write(const QJsonArray &array, const QString &filename)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
write(QJsonDocument(array), filename);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray toText(const QJsonObject &obj)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonDocument(obj).toJson(QJsonDocument::Compact);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
QByteArray toText(const QJsonArray &array)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonDocument(array).toJson(QJsonDocument::Compact);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static bool isBinaryJson(const QByteArray &data)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
decltype(QJsonDocument::BinaryFormatTag) tag = QJsonDocument::BinaryFormatTag;
|
|
|
|
return memcmp(data.constData(), &tag, sizeof(QJsonDocument::BinaryFormatTag)) == 0;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
QJsonDocument requireDocument(const QByteArray &data, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (isBinaryJson(data))
|
|
|
|
{
|
2022-05-02 22:40:45 +05:30
|
|
|
// FIXME: Is this needed?
|
|
|
|
throw JsonException(what + ": Invalid JSON. Binary JSON unsupported");
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
throw JsonException(what + ": Error parsing JSON: " + error.errorString());
|
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
QJsonDocument requireDocument(const QString &filename, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return requireDocument(FS::read(filename), what);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
QJsonObject requireObject(const QJsonDocument &doc, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!doc.isObject())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not an object");
|
|
|
|
}
|
|
|
|
return doc.object();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
QJsonArray requireArray(const QJsonDocument &doc, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!doc.isArray())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not an array");
|
|
|
|
}
|
|
|
|
return doc.array();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void writeString(QJsonObject &to, const QString &key, const QString &value)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isEmpty())
|
|
|
|
{
|
|
|
|
to.insert(key, value);
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void writeStringList(QJsonObject &to, const QString &key, const QStringList &values)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!values.isEmpty())
|
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for(auto value: values)
|
|
|
|
{
|
|
|
|
array.append(value);
|
|
|
|
}
|
|
|
|
to.insert(key, array);
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QUrl>(const QUrl &url)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonValue(url.toString(QUrl::FullyEncoded));
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QByteArray>(const QByteArray &data)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonValue(QString::fromLatin1(data.toHex()));
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QDateTime>(const QDateTime &datetime)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonValue(datetime.toString(Qt::ISODate));
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QDir>(const QDir &dir)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QDir::current().relativeFilePath(dir.absolutePath());
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QUuid>(const QUuid &uuid)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return uuid.toString();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QVariant>(const QVariant &variant)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QJsonValue::fromVariant(variant);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QString string = ensureIsType<QString>(value, what);
|
|
|
|
// ensure that the string can be safely cast to Latin1
|
|
|
|
if (string != QString::fromLatin1(string.toLatin1()))
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not encodable as Latin1");
|
|
|
|
}
|
|
|
|
return QByteArray::fromHex(string.toLatin1());
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isArray())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not an array");
|
|
|
|
}
|
|
|
|
return value.toArray();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isString())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a string");
|
|
|
|
}
|
|
|
|
return value.toString();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isBool())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a bool");
|
|
|
|
}
|
|
|
|
return value.toBool();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> double requireIsType<double>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isDouble())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a double");
|
|
|
|
}
|
|
|
|
return value.toDouble();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> int requireIsType<int>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const double doubl = requireIsType<double>(value, what);
|
|
|
|
if (fmod(doubl, 1) != 0)
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not an integer");
|
|
|
|
}
|
|
|
|
return int(doubl);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QString string = requireIsType<QString>(value, what);
|
|
|
|
const QDateTime datetime = QDateTime::fromString(string, Qt::ISODate);
|
|
|
|
if (!datetime.isValid())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a ISO formatted date/time value");
|
|
|
|
}
|
|
|
|
return datetime;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QString string = ensureIsType<QString>(value, what);
|
|
|
|
if (string.isEmpty())
|
|
|
|
{
|
|
|
|
return QUrl();
|
|
|
|
}
|
|
|
|
const QUrl url = QUrl(string, QUrl::StrictMode);
|
|
|
|
if (!url.isValid())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a correctly formatted URL");
|
|
|
|
}
|
|
|
|
return url;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QString string = requireIsType<QString>(value, what);
|
|
|
|
// FIXME: does not handle invalid characters!
|
|
|
|
return QDir::current().absoluteFilePath(string);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QString string = requireIsType<QString>(value, what);
|
|
|
|
const QUuid uuid = QUuid(string);
|
|
|
|
if (uuid.toString() != string) // converts back => valid
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not a valid UUID");
|
|
|
|
}
|
|
|
|
return uuid;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!value.isObject())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is not an object");
|
|
|
|
}
|
|
|
|
return value.toObject();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (value.isNull() || value.isUndefined())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is null or undefined");
|
|
|
|
}
|
|
|
|
return value.toVariant();
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (value.isNull() || value.isUndefined())
|
|
|
|
{
|
|
|
|
throw JsonException(what + " is null or undefined");
|
|
|
|
}
|
|
|
|
return value;
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|