1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef PROTOBUFSCALARJSONSERIALIZERS_P_H
#define PROTOBUFSCALARJSONSERIALIZERS_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtProtobuf/qtprotobufexports.h>
#include <QtProtobuf/qtprotobuftypes.h>
#include <QtCore/private/qnumeric_p.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qtconfigmacros.h>
#include <limits>
QT_BEGIN_NAMESPACE
namespace ProtobufScalarJsonSerializers {
// Tests if V is JSON compatible integer
// int32 | int64 | uint32 | sint32 | sint64 | fixed32 | sfixed32 | sfixed64
template <typename T>
constexpr inline bool IsJsonInt = false;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::int32> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::int64> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::uint32> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::sint32> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::sint64> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::fixed32> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::sfixed32> = true;
template <>
constexpr inline bool IsJsonInt<QtProtobuf::sfixed64> = true;
template <typename T>
using if_json_int = std::enable_if_t<IsJsonInt<T>, bool>;
// Tests if V is JSON incompatible 64-bit unsigned integer
// uint64 | fixed64
template <typename T>
constexpr inline bool IsJsonInt64 = false;
template <>
constexpr inline bool IsJsonInt64<QtProtobuf::uint64> = true;
template <>
constexpr inline bool IsJsonInt64<QtProtobuf::fixed64> = true;
template <typename T>
using if_json_int64 = std::enable_if_t<IsJsonInt64<T>, bool>;
// Tests if V is JSON compatible floating point number
// float | double
template <typename T>
constexpr inline bool IsJsonFloatingPoint = false;
template <>
constexpr inline bool IsJsonFloatingPoint<float> = true;
template <>
constexpr inline bool IsJsonFloatingPoint<double> = true;
template <typename T>
using if_json_floating_point = std::enable_if_t<IsJsonFloatingPoint<T>, bool>;
// Special value strings
constexpr inline QLatin1StringView Infinity("Infinity");
constexpr inline QLatin1StringView NegInfinity("-Infinity");
constexpr inline QLatin1StringView NaN("NaN");
constexpr inline QByteArrayView InfinityLower("infinity");
constexpr inline QByteArrayView NegInfinityLower("-infinity");
constexpr inline QByteArrayView NaNLower("nan");
constexpr inline QLatin1StringView True("true");
constexpr inline QLatin1StringView False("false");
template <typename T, if_json_int<T> = true>
QJsonValue serialize(T propertyValue)
{
return QJsonValue(qint64(propertyValue));
}
template <typename T, if_json_int64<T> = true>
QJsonValue serialize(T propertyValue)
{
return QJsonValue(QString::number(propertyValue));
}
template <typename T, if_json_floating_point<T> = true>
QJsonValue serialize(T propertyValue)
{
if (propertyValue == -std::numeric_limits<T>::infinity())
return QJsonValue(NegInfinity);
if (propertyValue == std::numeric_limits<T>::infinity())
return QJsonValue(Infinity);
if (propertyValue != propertyValue)
return QJsonValue(NaN);
return QJsonValue(propertyValue);
}
inline QJsonValue serialize(bool propertyValue)
{
return QJsonValue(propertyValue);
}
inline QJsonValue serialize(const QString &propertyValue)
{
return QJsonValue(propertyValue);
}
inline QJsonValue serialize(const QByteArray &propertyValue)
{
return QJsonValue(QString::fromUtf8(propertyValue.toBase64()));
}
template <typename L>
QJsonValue serializeList(const QVariant &propertyValue)
{
QJsonArray arr;
L listValue = propertyValue.value<L>();
for (const auto &value : listValue)
arr.append(serialize(value));
return QJsonValue(arr);
}
template <typename T>
QJsonValue serializeCommon(const QVariant &propertyValue)
{
return serialize(propertyValue.value<T>());
}
Q_PROTOBUF_EXPORT bool validateJsonNumberString(const QString &input);
template <typename T, if_json_int<T> = true>
T deserialize(const QJsonValue &value, bool &ok)
{
auto variantValue = value.toVariant();
qint64 raw = 0;
switch (variantValue.metaType().id()) {
case QMetaType::QString:
if (!validateJsonNumberString(variantValue.toString()))
ok = false;
else
raw = variantValue.toLongLong(&ok);
break;
case QMetaType::LongLong:
raw = variantValue.toLongLong(&ok);
break;
case QMetaType::Double: {
double d = value.toDouble();
ok = convertDoubleTo(d, &raw) && double(raw) == d;
} break;
default:
break;
}
// For types that "smaller" than qint64 we need to check if the value fits its limits range
if constexpr (sizeof(T) != sizeof(qint64)) {
constexpr auto Limits = []() {
if constexpr (std::is_same_v<T, QtProtobuf::sfixed32>
|| std::is_same_v<T, QtProtobuf::int32>)
return std::numeric_limits<qint32>{};
else if constexpr (std::is_same_v<T, QtProtobuf::fixed32>)
return std::numeric_limits<quint32>{};
else
return std::numeric_limits<T>{};
}();
ok = ok && (raw >= Limits.min() && raw <= Limits.max());
}
return ok ? T(raw) : T{};
}
template <typename T, if_json_int64<T> = true>
T deserialize(const QJsonValue &value, bool &ok)
{
quint64 raw = 0;
auto variantValue = value.toVariant();
switch (variantValue.metaType().id()) {
case QMetaType::QString:
case QMetaType::LongLong:
// Here we attempt converting the value to ULongLong
raw = variantValue.toULongLong(&ok);
break;
case QMetaType::Double: {
double d = value.toDouble();
ok = convertDoubleTo(d, &raw) && double(raw) == d;
} break;
default:
break;
}
return T(raw);
}
template <typename T, if_json_floating_point<T> = true>
T deserialize(const QJsonValue &value, bool &ok)
{
ok = true;
QByteArray data = value.toVariant().toByteArray();
if (data.compare(NegInfinityLower, Qt::CaseInsensitive) == 0)
return -std::numeric_limits<T>::infinity();
if (data.compare(InfinityLower, Qt::CaseInsensitive) == 0)
return std::numeric_limits<T>::infinity();
if (data.compare(NaNLower, Qt::CaseInsensitive) == 0)
return T(NAN);
if constexpr (std::is_same_v<T, float>)
return data.toFloat(&ok);
else
return data.toDouble(&ok);
}
template <typename T, std::enable_if_t<std::is_same_v<T, bool>, bool> = true>
bool deserialize(const QJsonValue &value, bool &ok)
{
if (value.isBool()) {
ok = true;
return value.toBool();
} else if (value.isString()) {
if (value.toString() == True) {
ok = true;
return true;
} else if (value.toString() == False) {
ok = true;
return false;
}
}
return false;
}
template <typename T, std::enable_if_t<std::is_same_v<T, QString>, bool> = true>
QString deserialize(const QJsonValue &value, bool &ok)
{
ok = value.isString();
return value.toString();
}
template <typename T, std::enable_if_t<std::is_same_v<T, QByteArray>, bool> = true>
QByteArray deserialize(const QJsonValue &value, bool &ok)
{
QByteArray data = value.toVariant().toByteArray();
if (value.isString()) {
ok = true;
return QByteArray::fromBase64(data);
}
return {};
}
template <typename L /*list*/, typename T /*element*/>
QVariant deserializeList(const QJsonValue &value, bool &ok)
{
if (!value.isArray()) {
ok = false;
return {};
}
L list;
QJsonArray array = value.toArray();
for (auto arrayValue : array) {
ok = false;
T value = deserialize<T>(arrayValue, ok);
if (!ok)
break;
list.append(value);
}
return QVariant::fromValue(list);
}
template <typename T>
QVariant deserializeCommon(const QJsonValue &value, bool &ok)
{
ok = false;
return QVariant::fromValue<T>(deserialize<T>(value, ok));
}
} // namespace ProtobufScalarJsonSerializers
QT_END_NAMESPACE
#endif // PROTOBUFSCALARJSONSERIALIZERS_P_H
|