Jellyfin Qt
QML Library for interacting with the Jellyfin multimedia server
Toggle main menu visibility
Loading...
Searching...
No Matches
jsonconvimpl.h
Go to the documentation of this file.
1
/*
2
* Sailfin: a Jellyfin client written using Qt
3
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
*/
19
#ifndef JELLYFIN_SUPPORT_JSONCONVIMPL_H
20
#define JELLYFIN_SUPPORT_JSONCONVIMPL_H
21
22
#include <QtGlobal>
23
#include <QDateTime>
24
#include <QJsonArray>
25
#include <QJsonDocument>
26
#include <QJsonObject>
27
#include <QJsonValue>
28
#include <QList>
29
#include <QSharedPointer>
30
#include <QUuid>
31
#include <QVariant>
32
33
#include <optional>
34
35
namespace
Jellyfin
{
36
namespace
Support
{
37
38
// Helper functions
39
//QString uuidToString(const QUuid &source);
40
//QUuid stringToUuid(const QString &source);
41
42
43
// https://www.fluentcpp.com/2017/08/15/function-templates-partial-specialization-cpp/
44
template
<
typename
T>
45
struct
convertType
{};
46
50
template
<
typename
T>
51
T
fromJsonValue
(
const
QJsonValue &source,
convertType<T>
) {
52
Q_UNUSED(source)
53
Q_ASSERT_X(
false
,
"fromJsonValue<T>"
,
"fromJsonValue called with unimplemented type"
);
54
}
55
59
template
<
typename
T>
60
QJsonValue
toJsonValue
(
const
T &source,
convertType<T>
) {
61
Q_UNUSED(source)
62
std::string msg =
"toJsonValue called with unimplemented type "
;
63
msg +=
typeid
(T).name();
64
Q_ASSERT_X(
false
,
"toJsonValue<T>"
, msg.c_str());
65
return
QJsonValue();
66
}
67
68
template
<
typename
T>
69
T
fromJsonValue
(
const
QJsonValue &source) {
70
return
fromJsonValue
(source,
convertType<T>
{});
71
}
72
73
74
template
<
typename
T>
75
QJsonValue
toJsonValue
(
const
T &source) {
76
return
toJsonValue
(source,
convertType<T>
{});
77
}
78
79
// QList
80
template
<
typename
T>
81
QList<T>
fromJsonValue
(
const
QJsonValue &source,
convertType
<
QList<T>
>) {
82
QList<T>
result;
83
QJsonArray arr = source.toArray();
84
result.reserve(arr.size());
85
for
(
auto
it = arr.constBegin(); it != arr.constEnd(); it++) {
86
result.append(
fromJsonValue<T>
(*it));
87
}
88
return
result;
89
}
90
91
template
<
typename
T>
92
QJsonValue
toJsonValue
(
const
QList<T>
&source,
convertType
<
QList<T>
>) {
93
QJsonArray result;
94
for
(
auto
it = source.cbegin(); it != source.cend(); it++) {
95
result.push_back(
toJsonValue<T>
(*it));
96
}
97
return
result;
98
}
99
100
// Optional
101
102
template
<
typename
T>
103
std::optional<T>
fromJsonValue
(
const
QJsonValue &source,
convertType
<std::optional<T>>) {
104
if
(source.isNull()) {
105
return
std::nullopt;
106
}
else
{
107
return
fromJsonValue<T>
(source,
convertType<T>
{});
108
}
109
}
110
111
template
<
typename
T>
112
QJsonValue
toJsonValue
(
const
std::optional<T> &source,
convertType
<std::optional<T>>) {
113
if
(source.has_value()) {
114
return
toJsonValue<T>
(source.value(),
convertType<T>
{});
115
}
else
{
116
// Null
117
return
QJsonValue();
118
}
119
}
120
121
// QSharedPointer
122
template
<
typename
T>
123
QSharedPointer<T>
fromJsonValue
(
const
QJsonValue &source,
convertType
<QSharedPointer<T>>) {
124
if
(source.isNull()) {
125
return
QSharedPointer<T>();
126
}
127
return
QSharedPointer<T>::create(
fromJsonValue<T>
(source));
128
}
129
130
template
<
typename
T>
131
QJsonValue
toJsonValue
(
const
QSharedPointer<T> &source,
convertType
<QSharedPointer<T>>) {
132
if
(source.isNull()) {
133
return
QJsonValue();
134
}
135
return
toJsonValue<T>
(*source);
136
}
137
138
142
143
144
template
<
typename
T>
145
QString
toString
(
const
T &source) {
146
return
toString
(source,
convertType<T>
{});
147
}
148
149
template
<
typename
T>
150
QString
toString
(
const
T &source,
convertType<T>
) {
151
QJsonValue val =
toJsonValue
(source);
152
const
QJsonDocument::JsonFormat format = QJsonDocument::Compact;
153
switch
(val.type()) {
154
case
QJsonValue::Array:
155
return
QJsonDocument(val.toArray()).toJson(format);
156
case
QJsonValue::Object:
157
return
QJsonDocument(val.toObject()).toJson(format);
158
case
QJsonValue::String:
159
return
val.toString();
160
case
QJsonValue::Null:
161
default
:
162
return
QString();
163
}
164
}
165
166
template
<
typename
T>
167
QString
toString
(
const
std::optional<T> &source,
convertType
<std::optional<T>>) {
168
if
(source.has_value()) {
169
return
toString<T>
(source.value(),
convertType<T>
{});
170
}
else
{
171
return
QString();
172
}
173
}
174
175
template
<
typename
T>
176
QString
toString
(
const
QList<T>
&source,
convertType
<
QList<T>
>) {
177
QStringList tmp;
178
tmp.reserve(source.size());
179
for
(
auto
it = source.cbegin(); it != source.cend(); it++) {
180
tmp.append(
toString<T>
(*it,
convertType<T>
{}));
181
}
182
return
tmp.join(
','
);
183
}
184
185
}
// NS Support
186
}
// NS Jellyfin
187
188
#endif
// JELLYFIN_SUPPORT_JSONCONVIMPL_H
QList
Definition
mediaplayer2.h:20
Jellyfin::Support
Definition
accessschedule.h:128
Jellyfin::Support::fromJsonValue
AccessSchedule fromJsonValue(const QJsonValue &source, convertType< AccessSchedule >)
Definition
accessschedule.cpp:133
Jellyfin::Support::toString
template QString toString(const QUuid &source, convertType< QUuid >)
Definition
jsonconv.cpp:227
Jellyfin::Support::toJsonValue
QJsonValue toJsonValue(const AccessSchedule &source, convertType< AccessSchedule >)
Definition
accessschedule.cpp:139
Jellyfin
Jellyfin::Support::convertType
Definition
jsonconvimpl.h:45
core
include
JellyfinQt
support
jsonconvimpl.h
Generated by
1.17.0