Commit 4e45b010 authored by Volker Krause's avatar Volker Krause

Generalize de/serialization of vectors too

parent 73ab8063
...@@ -18,8 +18,11 @@ ...@@ -18,8 +18,11 @@
#ifndef KPUBLICTRANSPORT_JSON_H #ifndef KPUBLICTRANSPORT_JSON_H
#define KPUBLICTRANSPORT_JSON_H #define KPUBLICTRANSPORT_JSON_H
#include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
#include <vector>
class QMetaObject; class QMetaObject;
namespace KPublicTransport { namespace KPublicTransport {
...@@ -35,6 +38,15 @@ namespace Json ...@@ -35,6 +38,15 @@ namespace Json
return toJson(&T::staticMetaObject, &elem); return toJson(&T::staticMetaObject, &elem);
} }
/** Serialize an array of elements. */
template <typename T> inline QJsonArray toJson(const std::vector<T> &elems)
{
QJsonArray a;
//a.reserve(elems.size());
std::transform(elems.begin(), elems.end(), std::back_inserter(a), QOverload<const T&>::of(&T::toJson));
return a;
}
void fromJson(const QMetaObject *mo, const QJsonObject &obj, void *elem); void fromJson(const QMetaObject *mo, const QJsonObject &obj, void *elem);
/** Deserialize via QMetaObject. */ /** Deserialize via QMetaObject. */
...@@ -44,6 +56,15 @@ namespace Json ...@@ -44,6 +56,15 @@ namespace Json
fromJson(&T::staticMetaObject, obj, &elem); fromJson(&T::staticMetaObject, obj, &elem);
return elem; return elem;
} }
/** Deserialize an array of elements. */
template <typename T> inline std::vector<T> fromJson(const QJsonArray &a)
{
std::vector<T> res;
res.reserve(a.size());
std::transform(a.begin(), a.end(), std::back_inserter(res), [](const auto &v) { return T::fromJson(v.toObject()); });
return res;
}
} }
} }
......
...@@ -184,13 +184,10 @@ QJsonObject Location::toJson(const Location &loc) ...@@ -184,13 +184,10 @@ QJsonObject Location::toJson(const Location &loc)
QJsonArray Location::toJson(const std::vector<Location> &locs) QJsonArray Location::toJson(const std::vector<Location> &locs)
{ {
QJsonArray a; return Json::toJson(locs);
//a.reserve(locs.size());
std::transform(locs.begin(), locs.end(), std::back_inserter(a), QOverload<const Location&>::of(&Location::toJson));
return a;
} }
static Location fromJsonObject(const QJsonObject &obj) Location Location::fromJson(const QJsonObject &obj)
{ {
auto loc = Json::fromJson<Location>(obj); auto loc = Json::fromJson<Location>(obj);
const auto tz = obj.value(QLatin1String("timezone")).toString(); const auto tz = obj.value(QLatin1String("timezone")).toString();
...@@ -206,17 +203,9 @@ static Location fromJsonObject(const QJsonObject &obj) ...@@ -206,17 +203,9 @@ static Location fromJsonObject(const QJsonObject &obj)
return loc; return loc;
} }
std::vector<Location> Location::fromJson(const QJsonValue &v) std::vector<Location> Location::fromJson(const QJsonArray &a)
{ {
std::vector<Location> res; return Json::fromJson<Location>(a);
if (v.isArray()) {
const auto a = v.toArray();
res.reserve(a.size());
std::transform(a.begin(), a.end(), std::back_inserter(res), [](const auto &v) { return fromJsonObject(v.toObject()); });
} else if (v.isObject()) {
res.push_back(fromJsonObject(v.toObject()));
}
return res;
} }
#include "moc_location.cpp" #include "moc_location.cpp"
...@@ -77,8 +77,10 @@ public: ...@@ -77,8 +77,10 @@ public:
static QJsonObject toJson(const Location &loc); static QJsonObject toJson(const Location &loc);
/** Serializes an array of Location objects to JSON. */ /** Serializes an array of Location objects to JSON. */
static QJsonArray toJson(const std::vector<Location> &locs); static QJsonArray toJson(const std::vector<Location> &locs);
/** Dezerializes one or more Location objects from JSON. */ /** Deserialize a Location object from JSON. */
static std::vector<Location> fromJson(const QJsonValue &v); static Location fromJson(const QJsonObject &obj);
/** Dezerializes an array Location objects from JSON. */
static std::vector<Location> fromJson(const QJsonArray &a);
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment