Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
PIM
Itinerary
Commits
7ee76e1c
Commit
7ee76e1c
authored
Dec 25, 2018
by
Volker Krause
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
De/serialize routes, lines and colors
parent
94842886
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
3 deletions
+55
-3
src/publictransport/datatypes/departure.cpp
src/publictransport/datatypes/departure.cpp
+2
-2
src/publictransport/datatypes/json.cpp
src/publictransport/datatypes/json.cpp
+14
-1
src/publictransport/datatypes/line.cpp
src/publictransport/datatypes/line.cpp
+29
-0
src/publictransport/datatypes/line.h
src/publictransport/datatypes/line.h
+10
-0
No files found.
src/publictransport/datatypes/departure.cpp
View file @
7ee76e1c
...
...
@@ -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
;
}
...
...
src/publictransport/datatypes/json.cpp
View file @
7ee76e1c
...
...
@@ -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
{};
}
...
...
src/publictransport/datatypes/line.cpp
View file @
7ee76e1c
...
...
@@ -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"
src/publictransport/datatypes/line.h
View file @
7ee76e1c
...
...
@@ -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
);
};
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment