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

Generalize de/serialization of vectors too

parent 73ab8063
......@@ -18,8 +18,11 @@
#ifndef KPUBLICTRANSPORT_JSON_H
#define KPUBLICTRANSPORT_JSON_H
#include <QJsonArray>
#include <QJsonObject>
#include <vector>
class QMetaObject;
namespace KPublicTransport {
......@@ -35,6 +38,15 @@ namespace Json
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);
/** Deserialize via QMetaObject. */
......@@ -44,6 +56,15 @@ namespace Json
fromJson(&T::staticMetaObject, obj, &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)
QJsonArray Location::toJson(const std::vector<Location> &locs)
{
QJsonArray a;
//a.reserve(locs.size());
std::transform(locs.begin(), locs.end(), std::back_inserter(a), QOverload<const Location&>::of(&Location::toJson));
return a;
return Json::toJson(locs);
}
static Location fromJsonObject(const QJsonObject &obj)
Location Location::fromJson(const QJsonObject &obj)
{
auto loc = Json::fromJson<Location>(obj);
const auto tz = obj.value(QLatin1String("timezone")).toString();
......@@ -206,17 +203,9 @@ static Location fromJsonObject(const QJsonObject &obj)
return loc;
}
std::vector<Location> Location::fromJson(const QJsonValue &v)
std::vector<Location> Location::fromJson(const QJsonArray &a)
{
std::vector<Location> res;
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;
return Json::fromJson<Location>(a);
}
#include "moc_location.cpp"
......@@ -77,8 +77,10 @@ public:
static QJsonObject toJson(const Location &loc);
/** Serializes an array of Location objects to JSON. */
static QJsonArray toJson(const std::vector<Location> &locs);
/** Dezerializes one or more Location objects from JSON. */
static std::vector<Location> fromJson(const QJsonValue &v);
/** Deserialize a Location object from JSON. */
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