2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* jsonutils.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-09-21 21:29:26 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "jsonutils.h"
|
|
|
|
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../lib/json/JsonNode.h"
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
static QVariantMap JsonToMap(const JsonMap & json)
|
|
|
|
{
|
|
|
|
QVariantMap map;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : json)
|
2013-09-21 21:29:26 +03:00
|
|
|
{
|
2022-09-26 15:37:31 +02:00
|
|
|
map.insert(QString::fromStdString(entry.first), JsonUtils::toVariant(entry.second));
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVariantList JsonToList(const JsonVector & json)
|
|
|
|
{
|
|
|
|
QVariantList list;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : json)
|
2013-09-21 21:29:26 +03:00
|
|
|
{
|
|
|
|
list.push_back(JsonUtils::toVariant(entry));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonVector VariantToList(QVariantList variant)
|
|
|
|
{
|
|
|
|
JsonVector vector;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : variant)
|
2013-09-21 21:29:26 +03:00
|
|
|
{
|
|
|
|
vector.push_back(JsonUtils::toJson(entry));
|
|
|
|
}
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonMap VariantToMap(QVariantMap variant)
|
|
|
|
{
|
|
|
|
JsonMap map;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : variant.toStdMap())
|
2013-09-21 21:29:26 +03:00
|
|
|
{
|
|
|
|
map[entry.first.toUtf8().data()] = JsonUtils::toJson(entry.second);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2022-07-29 08:52:39 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
namespace JsonUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
QVariant toVariant(const JsonNode & node)
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
switch(node.getType())
|
2013-09-21 21:29:26 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
case JsonNode::JsonType::DATA_NULL:
|
|
|
|
return QVariant();
|
|
|
|
case JsonNode::JsonType::DATA_BOOL:
|
|
|
|
return QVariant(node.Bool());
|
|
|
|
case JsonNode::JsonType::DATA_FLOAT:
|
|
|
|
return QVariant(node.Float());
|
2022-09-26 15:38:37 +02:00
|
|
|
case JsonNode::JsonType::DATA_INTEGER:
|
|
|
|
return QVariant{static_cast<qlonglong>(node.Integer())};
|
2018-04-13 07:34:58 +02:00
|
|
|
case JsonNode::JsonType::DATA_STRING:
|
2022-09-26 15:37:31 +02:00
|
|
|
return QVariant(QString::fromStdString(node.String()));
|
2018-04-13 07:34:58 +02:00
|
|
|
case JsonNode::JsonType::DATA_VECTOR:
|
|
|
|
return JsonToList(node.Vector());
|
|
|
|
case JsonNode::JsonType::DATA_STRUCT:
|
|
|
|
return JsonToMap(node.Struct());
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant JsonFromFile(QString filename)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
2022-09-26 15:37:31 +02:00
|
|
|
if(!file.open(QFile::ReadOnly))
|
2013-11-09 22:10:16 +03:00
|
|
|
{
|
2022-09-26 15:37:31 +02:00
|
|
|
logGlobal->error("Failed to open file %s. Reason: %s", qUtf8Printable(filename), qUtf8Printable(file.errorString()));
|
|
|
|
return {};
|
2013-11-09 22:10:16 +03:00
|
|
|
}
|
2022-09-26 15:37:31 +02:00
|
|
|
|
|
|
|
const auto data = file.readAll();
|
2024-07-17 13:07:57 +02:00
|
|
|
JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size(), filename.toStdString());
|
2022-09-26 15:37:31 +02:00
|
|
|
return toVariant(node);
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode toJson(QVariant object)
|
|
|
|
{
|
|
|
|
JsonNode ret;
|
|
|
|
|
2023-04-19 22:10:34 +02:00
|
|
|
if(object.userType() == QMetaType::QString)
|
2013-09-21 21:29:26 +03:00
|
|
|
ret.String() = object.toString().toUtf8().data();
|
2022-05-28 15:32:20 +02:00
|
|
|
else if(object.userType() == QMetaType::Bool)
|
2013-09-21 21:29:26 +03:00
|
|
|
ret.Bool() = object.toBool();
|
2023-04-19 22:10:34 +02:00
|
|
|
else if(object.canConvert<QVariantMap>())
|
|
|
|
ret.Struct() = VariantToMap(object.toMap());
|
|
|
|
else if(object.canConvert<QVariantList>())
|
|
|
|
ret.Vector() = VariantToList(object.toList());
|
2022-09-26 15:38:37 +02:00
|
|
|
else if(object.canConvert<int>())
|
|
|
|
ret.Integer() = object.toInt();
|
2018-04-13 07:34:58 +02:00
|
|
|
else if(object.canConvert<double>())
|
2013-11-09 22:10:16 +03:00
|
|
|
ret.Float() = object.toFloat();
|
2013-09-21 21:29:26 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonToFile(QString filename, QVariant object)
|
|
|
|
{
|
2023-07-28 14:51:14 +02:00
|
|
|
std::fstream file(qstringToPath(filename).c_str(), std::ios::out | std::ios_base::binary);
|
2024-02-12 01:22:16 +02:00
|
|
|
file << toJson(object).toString();
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
2013-11-09 22:10:16 +03:00
|
|
|
}
|
2022-07-29 08:52:39 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|