diff --git a/include/lib/orderedjson.h b/include/lib/orderedjson.h index 20463cee..0b43080c 100644 --- a/include/lib/orderedjson.h +++ b/include/lib/orderedjson.h @@ -60,6 +60,9 @@ #include #include #include +#include +#include +#include #include #include @@ -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; diff --git a/src/lib/orderedjson.cpp b/src/lib/orderedjson.cpp index 699c8d12..025607ca 100644 --- a/src/lib/orderedjson.cpp +++ b/src/lib/orderedjson.cpp @@ -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 */ diff --git a/src/project.cpp b/src/project.cpp index 6207fe5b..4df26f18 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -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);