Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
PIM
Itinerary
Commits
4e45b010
Commit
4e45b010
authored
Dec 24, 2018
by
Volker Krause
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generalize de/serialization of vectors too
parent
73ab8063
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
17 deletions
+29
-17
src/publictransport/datatypes/json.h
src/publictransport/datatypes/json.h
+21
-0
src/publictransport/datatypes/location.cpp
src/publictransport/datatypes/location.cpp
+4
-15
src/publictransport/datatypes/location.h
src/publictransport/datatypes/location.h
+4
-2
No files found.
src/publictransport/datatypes/json.h
View file @
4e45b010
...
...
@@ -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
;
}
}
}
...
...
src/publictransport/datatypes/location.cpp
View file @
4e45b010
...
...
@@ -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
);
}
st
ati
c
Location
fromJson
Object
(
const
QJsonObject
&
obj
)
Loc
ati
on
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
QJson
Value
&
v
)
std
::
vector
<
Location
>
Location
::
fromJson
(
const
QJson
Array
&
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"
src/publictransport/datatypes/location.h
View file @
4e45b010
...
...
@@ -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
);
};
...
...
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