Commit 7ee76e1c authored by Volker Krause's avatar Volker Krause

De/serialize routes, lines and colors

parent 94842886
......@@ -197,7 +197,7 @@ Departure Departure::merge(const Departure &lhs, const Departure &rhs)
QJsonObject Departure::toJson(const Departure &dep)
{
auto obj = Json::toJson(dep);
// obj.insert(QLatin1String("route"), Route::toJson(dep.route());
obj.insert(QLatin1String("route"), Route::toJson(dep.route()));
obj.insert(QLatin1String("stopPoint"), Location::toJson(dep.stopPoint()));
return obj;
}
......@@ -205,7 +205,7 @@ QJsonObject Departure::toJson(const Departure &dep)
Departure Departure::fromJson(const QJsonObject &obj)
{
auto dep = Json::fromJson<Departure>(obj);
// dep.setRoute(Route::fromJson(obj.value(QLatin1String("route")).toObject()));
dep.setRoute(Route::fromJson(obj.value(QLatin1String("route")).toObject()));
dep.setStopPoint(Location::fromJson(obj.value(QLatin1String("stopPoint")).toObject()));
return dep;
}
......
......@@ -17,6 +17,7 @@
#include "json.h"
#include <QColor>
#include <QMetaObject>
#include <QMetaProperty>
#include <QVariant>
......@@ -27,7 +28,10 @@ static QJsonValue variantToJson(const QVariant &v)
{
switch (v.userType()) {
case QMetaType::QString:
return v.toString();
{
const auto s = v.toString();
return s.isNull() ? QJsonValue() : v.toString();
}
case QMetaType::Double:
case QMetaType::Float:
return v.toDouble();
......@@ -35,6 +39,11 @@ static QJsonValue variantToJson(const QVariant &v)
return v.toInt();
}
if (v.userType() == qMetaTypeId<QColor>()) {
const auto c = v.value<QColor>();
return c.isValid() ? v.value<QColor>().name() : QJsonValue();;
}
return {};
}
......@@ -69,6 +78,10 @@ static QVariant variantFromJson(const QJsonValue &v, int mt)
return v.toInt();
}
if (mt == qMetaTypeId<QColor>()) {
return QColor(v.toString());
}
return {};
}
......
......@@ -17,6 +17,7 @@
#include "line.h"
#include "datatypes_p.h"
#include "json.h"
#include <QColor>
#include <QDebug>
......@@ -137,6 +138,19 @@ Line Line::merge(const Line &lhs, const Line &rhs)
return l;
}
QJsonObject Line::toJson(const Line &l)
{
auto obj = Json::toJson(l);
return obj;
}
Line Line::fromJson(const QJsonObject &obj)
{
auto l = Json::fromJson<Line>(obj);
return l;
}
KPUBLICTRANSPORT_MAKE_GADGET(Route)
Line Route::line() const
......@@ -174,4 +188,19 @@ Route Route::merge(const Route &lhs, const Route &rhs)
return r;
}
QJsonObject Route::toJson(const Route &r)
{
auto obj = Json::toJson(r);
obj.insert(QLatin1String("line"), Line::toJson(r.line()));
return obj;
}
Route Route::fromJson(const QJsonObject &obj)
{
auto r = Json::fromJson<Route>(obj);
r.setLine(Line::fromJson(obj.value(QLatin1String("line")).toObject()));
return r;
}
#include "moc_line.cpp"
......@@ -82,6 +82,11 @@ public:
* This assumes isSame(lhs, rhs) and tries to preserve the most detailed information.
*/
static Line merge(const Line &lhs, const Line &rhs);
/** Serializes one object to JSON. */
static QJsonObject toJson(const Line &l);
/** Deserialize an object from JSON. */
static Line fromJson(const QJsonObject &obj);
};
class RoutePrivate;
......@@ -108,6 +113,11 @@ public:
* This assumes isSame(lhs, rhs) and tries to preserve the most detailed information.
*/
static Route merge(const Route &lhs, const Route &rhs);
/** Serializes one object to JSON. */
static QJsonObject toJson(const Route &r);
/** Deserialize an object from JSON. */
static Route fromJson(const QJsonObject &obj);
};
}
......
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