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"
|
2016-01-09 22:23:55 +02:00
|
|
|
#include "../lib/filesystem/FileStream.h"
|
2013-09-21 21:29:26 +03:00
|
|
|
|
|
|
|
static QVariantMap JsonToMap(const JsonMap & json)
|
|
|
|
{
|
|
|
|
QVariantMap map;
|
|
|
|
for (auto & entry : json)
|
|
|
|
{
|
|
|
|
map.insert(QString::fromUtf8(entry.first.c_str()), JsonUtils::toVariant(entry.second));
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVariantList JsonToList(const JsonVector & json)
|
|
|
|
{
|
|
|
|
QVariantList list;
|
|
|
|
for (auto & entry : json)
|
|
|
|
{
|
|
|
|
list.push_back(JsonUtils::toVariant(entry));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonVector VariantToList(QVariantList variant)
|
|
|
|
{
|
|
|
|
JsonVector vector;
|
|
|
|
for (auto & entry : variant)
|
|
|
|
{
|
|
|
|
vector.push_back(JsonUtils::toJson(entry));
|
|
|
|
}
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonMap VariantToMap(QVariantMap variant)
|
|
|
|
{
|
|
|
|
JsonMap map;
|
|
|
|
for (auto & entry : variant.toStdMap())
|
|
|
|
{
|
|
|
|
map[entry.first.toUtf8().data()] = JsonUtils::toJson(entry.second);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace JsonUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
QVariant toVariant(const JsonNode & node)
|
|
|
|
{
|
|
|
|
switch (node.getType())
|
|
|
|
{
|
|
|
|
break; case JsonNode::DATA_NULL: return QVariant();
|
|
|
|
break; case JsonNode::DATA_BOOL: return QVariant(node.Bool());
|
|
|
|
break; case JsonNode::DATA_FLOAT: return QVariant(node.Float());
|
|
|
|
break; case JsonNode::DATA_STRING: return QVariant(QString::fromUtf8(node.String().c_str()));
|
|
|
|
break; case JsonNode::DATA_VECTOR: return JsonToList(node.Vector());
|
|
|
|
break; case JsonNode::DATA_STRUCT: return JsonToMap(node.Struct());
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant JsonFromFile(QString filename)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
auto data = file.readAll();
|
|
|
|
|
2013-11-09 22:10:16 +03:00
|
|
|
if (data.size() == 0)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Failed to open file %s", filename.toUtf8().data());
|
2013-11-09 22:10:16 +03:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonNode node(data.data(), data.size());
|
|
|
|
return toVariant(node);
|
|
|
|
}
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode toJson(QVariant object)
|
|
|
|
{
|
|
|
|
JsonNode ret;
|
|
|
|
|
|
|
|
if (object.canConvert<QVariantMap>())
|
|
|
|
ret.Struct() = VariantToMap(object.toMap());
|
2013-11-09 22:10:16 +03:00
|
|
|
else if (object.canConvert<QVariantList>())
|
2013-09-21 21:29:26 +03:00
|
|
|
ret.Vector() = VariantToList(object.toList());
|
2014-11-12 22:41:52 +02:00
|
|
|
else if (static_cast<QMetaType::Type>(object.type()) == QMetaType::QString)
|
2013-09-21 21:29:26 +03:00
|
|
|
ret.String() = object.toString().toUtf8().data();
|
2014-11-12 22:41:52 +02:00
|
|
|
else if (static_cast<QMetaType::Type>(object.type()) == QMetaType::Bool)
|
2013-09-21 21:29:26 +03:00
|
|
|
ret.Bool() = object.toBool();
|
2013-11-09 22:10:16 +03:00
|
|
|
else if (object.canConvert<double>())
|
|
|
|
ret.Float() = object.toFloat();
|
2013-09-21 21:29:26 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonToFile(QString filename, QVariant object)
|
|
|
|
{
|
2016-01-12 23:40:05 +02:00
|
|
|
FileStream file(qstringToPath(filename), std::ios::out | std::ios_base::binary);
|
2017-08-11 16:38:28 +02:00
|
|
|
file << toJson(object).toJson();
|
2013-09-21 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
2013-11-09 22:10:16 +03:00
|
|
|
}
|