Add QRect, QSize, QStringList nlohmann helpers

This commit is contained in:
Waqar Ahmed
2022-04-02 00:40:20 +05:00
parent 57dc7396ee
commit c8127c1b36

View File

@@ -13,6 +13,8 @@
#include <QVariantMap>
#include <QDebug>
#include <QRect>
#include <QSize>
#include <iostream>
@@ -28,6 +30,27 @@ inline void to_json(nlohmann::json &j, const QString &s)
j = s.toStdString();
}
inline void from_json(const nlohmann::json &j, QStringList &stringList)
{
if (!j.is_array()) {
qWarning() << Q_FUNC_INFO << "This is not an array, fix the code";
stringList.clear();
return;
}
stringList.reserve((int)j.size());
for (const auto &v : j) {
QString s = v;
stringList.push_back(std::move(s));
}
}
inline void to_json(nlohmann::json &j, const QStringList &stringList)
{
for (const auto &s : stringList) {
j.push_back(s);
}
}
inline void to_json(nlohmann::json& j, const QVariantList& list);
inline void from_json(const nlohmann::json& j, QVariantList& list);
inline void to_json(nlohmann::json& j, const QVariantMap& map);
@@ -166,4 +189,18 @@ inline void from_json(const nlohmann::json& j, QVariantMap& map)
}
}
inline void to_json(nlohmann::json& j, const QSize& size)
{
j["width"] = size.width();
j["height"] = size.height();
}
inline void to_json(nlohmann::json& j, const QRect& rect)
{
j["x"] = rect.x();
j["y"] = rect.y();
j["width"] = rect.width();
j["height"] = rect.height();
}
QT_END_NAMESPACE