Preserve custom header arrays and objects

This commit is contained in:
GriffinR 2022-10-16 02:30:13 -04:00
parent 6b55dc935d
commit 67945cb297
3 changed files with 33 additions and 22 deletions

View file

@ -60,6 +60,9 @@
#include <QPair>
#include <QFile>
#include <QTextStream>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <memory>
#include <initializer_list>
@ -129,6 +132,8 @@ public:
int>::type = 0>
Json(const V & v) : Json(array(v.begin(), v.end())) {}
static const Json fromQJsonValue(QJsonValue value);
// This prevents Json(some_pointer) from accidentally producing a bool. Use
// Json(bool(some_pointer)) if that behavior is desired.
Json(void *) = delete;

View file

@ -308,6 +308,33 @@ const Json & JsonArray::operator[] (int i) const {
else return m_value[i];
}
const Json Json::fromQJsonValue(QJsonValue value) {
switch (value.type())
{
default:
case QJsonValue::String: return value.toString();
case QJsonValue::Double: return value.toInt();
case QJsonValue::Bool: return value.toBool();
case QJsonValue::Array:
{
QJsonArray qArr = value.toArray();
Json::array arr;
for (const auto &i: qArr)
arr.push_back(Json::fromQJsonValue(i));
return arr;
}
case QJsonValue::Object:
{
QJsonObject qObj = value.toObject();
Json::object obj;
for (auto it = qObj.constBegin(); it != qObj.constEnd(); it++)
obj[it.key()] = Json::fromQJsonValue(it.value());
return obj;
}
}
}
/* * * * * * * * * * * * * * * * * * * *
* Comparison
*/

View file

@ -1440,28 +1440,7 @@ void Project::saveMap(Map *map) {
// Custom header fields.
for (QString key : map->customHeaders.keys()) {
QJsonValue value = map->customHeaders[key];
switch (value.type())
{
default:
case QJsonValue::String:
mapObj[key] = value.toString();
break;
case QJsonValue::Double:
mapObj[key] = value.toInt();
break;
case QJsonValue::Bool:
mapObj[key] = value.toBool();
break;
case QJsonValue::Array:
// TODO: Custom array values are replaced with empty strings
mapObj[key] = value.toString();
break;
case QJsonValue::Object:
// TODO: Custom object values are replaced with empty strings
mapObj[key] = value.toString();
break;
}
mapObj[key] = OrderedJson::fromQJsonValue(map->customHeaders[key]);
}
OrderedJson mapJson(mapObj);