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