summaryrefslogtreecommitdiffstats
path: root/src/protobuf/qprotobufjsonserializer.cpp
blob: ddb1a2a49fd669bd44a6ade554958774caea286a (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// Copyright (C) 2023 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
// Qt-Security score:critical reason:data-parser

#include <QtProtobuf/qprotobufjsonserializer.h>

#include <QtProtobuf/private/qtprotobuf-config_p.h>
#include <QtProtobuf/private/protobuffieldpresencechecker_p.h>
#include <QtProtobuf/private/protobufscalarjsonserializers_p.h>
#include <QtProtobuf/private/qprotobufdeserializerbase_p.h>
#include <QtProtobuf/private/qprotobufjsonserializer_p.h>
#include <QtProtobuf/private/qprotobufregistration_p.h>
#include <QtProtobuf/private/qprotobufserializerbase_p.h>
#include <QtProtobuf/private/qtprotobufdefs_p.h>
#include <QtProtobuf/private/qtprotobufserializerhelpers_p.h>

#include <QtCore/qcoreapplication.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qhash.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#if !QT_CONFIG(protobuf_unsafe_registry)
#    include <QtCore/qreadwritelock.h>
#endif
#include <QtCore/qtimezone.h>
#include <QtCore/qvariant.h>

#include <cmath>

QT_BEGIN_NAMESPACE

/*!
    \class QProtobufJsonSerializer
    \inmodule QtProtobuf
    \since 6.7
    \brief The QProtobufJsonSerializer class is an interface that represents
           basic functions for serialization/deserialization of QProtobufMessage
           objects to JSON.

    The QProtobufJsonSerializer class registers serializers/deserializers for
    classes implementing a protobuf message, inheriting \l QProtobufMessage. These
    classes are generated automatically, based on a \c{.proto} file, using the CMake
    function \l qt_add_protobuf or by running
    \l {The qtprotobufgen Tool} {qtprotobufgen} directly.
*/

using namespace Qt::StringLiterals;
using namespace QtProtobufPrivate;
using namespace ProtobufScalarJsonSerializers;

namespace {

struct JsonHandlerRegistry
{
    void registerHandler(QMetaType metaType, QtProtobufPrivate::CustomJsonSerializer serializer,
                         QtProtobufPrivate::CustomJsonDeserializer deserializer)
    {
#if !QT_CONFIG(protobuf_unsafe_registry)
        QWriteLocker locker(&m_lock);
#endif
        m_registry[metaType] = { serializer, deserializer };
    }

    QtProtobufPrivate::CustomJsonSerializer findSerializer(QMetaType metaType)
    {
#if !QT_CONFIG(protobuf_unsafe_registry)
        QReadLocker locker(&m_lock);
#endif
        const auto it = m_registry.constFind(metaType);
        if (it != m_registry.constEnd())
            return it.value().first;
        return nullptr;
    }

    QtProtobufPrivate::CustomJsonDeserializer findDeserializer(QMetaType metaType)
    {
#if !QT_CONFIG(protobuf_unsafe_registry)
        QReadLocker locker(&m_lock);
#endif
        const auto it = m_registry.constFind(metaType);
        if (it != m_registry.constEnd())
            return it.value().second;
        return nullptr;
    }

private:
    using Handler = std::pair<QtProtobufPrivate::CustomJsonSerializer,
                              QtProtobufPrivate::CustomJsonDeserializer>;
#if !QT_CONFIG(protobuf_unsafe_registry)
    QReadWriteLock m_lock;
#endif
    QHash<QMetaType, Handler> m_registry;
};
Q_GLOBAL_STATIC(JsonHandlerRegistry, jsonHandlersRegistry)

inline QString convertJsonKeyToJsonName(QStringView name)
{
    QString result;
    result.reserve(name.size());
    bool nextUpperCase = false;
    for (const auto &c : name) {
        if (c == QChar::fromLatin1('_')) {
            nextUpperCase = true;
            continue;
        }
        result.append(nextUpperCase ? c.toUpper() : c);
        nextUpperCase = false;
    }
    return result;
}

}

void QtProtobufPrivate::registerCustomJsonHandler(QMetaType metaType,
                                                  QtProtobufPrivate::CustomJsonSerializer
                                                      serializer,
                                                  QtProtobufPrivate::CustomJsonDeserializer
                                                      deserializer)
{
    jsonHandlersRegistry->registerHandler(metaType, serializer, deserializer);
}

QtProtobufPrivate::CustomJsonSerializer
QtProtobufPrivate::findCustomJsonSerializer(QMetaType metaType)
{
    return jsonHandlersRegistry->findSerializer(metaType);
}

QtProtobufPrivate::CustomJsonDeserializer
QtProtobufPrivate::findCustomJsonDeserializer(QMetaType metaType)
{
    return jsonHandlersRegistry->findDeserializer(metaType);
}

class QProtobufJsonSerializerImpl final : public QProtobufSerializerBase
{
public:
    QProtobufJsonSerializerImpl() = default;
    ~QProtobufJsonSerializerImpl();

    const QJsonObject &result() const { return m_result; }

    void reset();

protected:
    void serializeMessageField(const QProtobufMessage *message,
                               const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override;

private:
    bool serializeEnum(QVariant &value, const QProtobufFieldInfo &fieldInfo) override;
    bool serializeScalarField(const QVariant &value, const QProtobufFieldInfo &fieldInfo) override;
    void serializeMessageFieldBegin() override;
    void serializeMessageFieldEnd(const QProtobufMessage *message,
                                  const QProtobufFieldInfo &fieldInfo) override;

    QJsonObject m_result;
    QList<QJsonObject> m_state;

    Q_DISABLE_COPY_MOVE(QProtobufJsonSerializerImpl)
};

class QProtobufJsonSerializerPrivate;
class QProtobufJsonDeserializerImpl final : public QProtobufDeserializerBase
{
public:
    explicit QProtobufJsonDeserializerImpl(QProtobufJsonSerializerPrivate *parent);
    ~QProtobufJsonDeserializerImpl();

    void reset(QJsonObject obj);

    void setError(QAbstractProtobufSerializer::Error error, QAnyStringView errorString) override;
    void setUnexpectedEndOfStreamError();
    void setInvalidFormatError();

protected:
    bool deserializeMessageField(QProtobufMessage *message) override;

private:
    bool deserializeEnum(QVariant &value,
                         const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override;
    int nextFieldIndex(QProtobufMessage *message) override;
    bool deserializeScalarField(QVariant &, const QtProtobufPrivate::QProtobufFieldInfo &) override;

    struct JsonDeserializerState
    {
        JsonDeserializerState(const QJsonObject &obj) : obj(obj) { }

        QJsonObject obj = {};
        int index = 0;
        QJsonValue scalarValue = {QJsonValue::Undefined};
    };

    QList<JsonDeserializerState> m_state;
    QProtobufJsonSerializerPrivate *m_parent = nullptr;
};

class QProtobufJsonSerializerPrivate final
{
    Q_DISABLE_COPY_MOVE(QProtobufJsonSerializerPrivate)
public:
    using Serializer = std::function<QJsonValue(const QVariant &)>;
    using Deserializer = std::function<QVariant(const QJsonValue&, bool &ok)>;

    struct SerializationHandlers {
        // serializer assigned to class
        Serializer serializer;
        // deserializer assigned to class
        Deserializer deserializer;
        ProtobufFieldPresenceChecker::Function isPresent;
    };

    template <typename T>
    static SerializationHandlers createCommonHandler()
    {
        return { serializeCommon<T>, deserializeCommon<T>,
                 ProtobufFieldPresenceChecker::isPresent<T> };
    }

    template <typename L, typename T>
    static SerializationHandlers createCommonListHandler()
    {
        return { serializeList<L>, deserializeList<L, T>,
                 ProtobufFieldPresenceChecker::isPresent<L> };
    }

    template<typename T, if_json_floating_point<T> = true>
    static bool isPresent(const QVariant &value)
    {
        const T val = value.value<T>();
        return val != 0 || std::signbit(val);
    }

    using SerializerRegistry = QHash<int/*metatypeid*/, SerializationHandlers>;

    QProtobufJsonSerializerPrivate();
    ~QProtobufJsonSerializerPrivate() = default;

    void clearError();

    QAbstractProtobufSerializer::Error lastError = QAbstractProtobufSerializer::Error::None;
    QString lastErrorString;

    static SerializerRegistry handlers;

    QProtobufJsonSerializerImpl serializer;
    QProtobufJsonDeserializerImpl deserializer;
};

QProtobufJsonSerializerImpl::~QProtobufJsonSerializerImpl() = default;

void QProtobufJsonSerializerImpl::reset()
{
    m_result = {};
    m_state.clear();
}

void QProtobufJsonSerializerImpl::serializeMessageField(const QProtobufMessage *message,
                                                        const QtProtobufPrivate::QProtobufFieldInfo
                                                            &fieldInfo)
{
    if (!message)
        return;

    const auto *metaObject = QtProtobufSerializerHelpers::messageMetaObject(message);

    if (auto *serializer = QtProtobufPrivate::findCustomJsonSerializer(metaObject->metaType())) {
        if (const QJsonValue value = serializer(message); !value.isUndefined()) {
            if (fieldInfo.fieldFlags().testAnyFlags(QtProtobufPrivate::FieldFlag::Repeated)) {
                auto array = m_result.value(fieldInfo.jsonName().toString()).toArray();
                array.append(value);
                m_result.insert(fieldInfo.jsonName().toString(), array);
            } else {
                m_result.insert(fieldInfo.jsonName().toString(), value);
            }
        }
    } else {
        QProtobufSerializerBase::serializeMessageField(message, fieldInfo);
    }
}

bool QProtobufJsonSerializerImpl::serializeEnum(QVariant &value,
                                                const QProtobufFieldInfo &fieldInfo)

{
    const auto jsonName = fieldInfo.jsonName();
    if (fieldInfo.fieldFlags().testFlag(QtProtobufPrivate::FieldFlag::Repeated)) {
        if (!value.convert(QMetaType::fromType<QStringList>()))
            return false;
        if (!ProtobufFieldPresenceChecker::isPresent<QStringList>(value))
            return true;
        m_result.insert(jsonName.toString(), serializeList<QStringList>(value));
    } else {
        if (!value.convert(QMetaType::fromType<QString>()))
            return false;
        if (!ProtobufFieldPresenceChecker::isPresent<QString>(value)
            && !isOneofOrOptionalField(fieldInfo.fieldFlags())) {
            return true;
        }
        m_result.insert(jsonName.toString(), serializeCommon<QString>(value));
    }
    return true;
}

bool QProtobufJsonSerializerImpl::serializeScalarField(const QVariant &value,
                                                       const QProtobufFieldInfo &fieldInfo)
{
    const auto it = QProtobufJsonSerializerPrivate::handlers.constFind(value.userType());
    // Is not a protobuf scalar value type
    if (it == QProtobufJsonSerializerPrivate::handlers.cend())
        return false;

    // Field is empty
    if (!it->isPresent(value) && !isOneofOrOptionalField(fieldInfo.fieldFlags()))
        return true;

    // If serializer is not defined we should use the standard QJsonValue cast from variant
    m_result.insert(fieldInfo.jsonName().toString(),
                    it->serializer ? it->serializer(value) : QJsonValue::fromVariant(value));
    return true;
}

void QProtobufJsonSerializerImpl::serializeMessageFieldBegin()
{
    m_state.emplaceBack(std::move(m_result));
    m_result = {};
}

void QProtobufJsonSerializerImpl::serializeMessageFieldEnd(const QProtobufMessage *message,
                                                           const QProtobufFieldInfo &field)
{
    QJsonObject store = m_state.takeLast();
    QString fieldName = field.jsonName().toString();
    if (field.fieldFlags().testFlag(QtProtobufPrivate::FieldFlag::Repeated)) {
        // Repeated fields are stored in array property
        // "repeatedField": [{...}, {...}, ...]
        QJsonArray array = store.value(fieldName).toArray();
        array.append(m_result);
        store.insert(fieldName, array);
    } else if (field.fieldFlags().testFlag(QtProtobufPrivate::FieldFlag::Map)) {
        // Maps are stored as fields in inner JSON object
        // "mapField": { "key1": "value1", ... }
        QJsonObject mapObject = store.value(fieldName).toObject();
        // We don't need to serialize key in general, since all JSON object fields should be
        // represented as strings, and types that can be map keys have valid string converters.
        mapObject.insert(message->property("key").toString(), m_result.value("value"_L1));
        store.insert(fieldName, mapObject);
    } else {
        store.insert(fieldName, m_result);
    }
    m_result = store;
}

QProtobufJsonDeserializerImpl::QProtobufJsonDeserializerImpl(QProtobufJsonSerializerPrivate *parent)
    : m_parent(parent)
{
}

QProtobufJsonDeserializerImpl::~QProtobufJsonDeserializerImpl()
    = default;

void QProtobufJsonDeserializerImpl::reset(QJsonObject obj)
{
    clearCachedValue();
    m_state.clear();
    if (!obj.isEmpty())
        m_state.push_back({ obj });
}

void QProtobufJsonDeserializerImpl::setError(QAbstractProtobufSerializer::Error error,
                                             QAnyStringView errorString)
{
    m_parent->lastError = error;
    m_parent->lastErrorString = errorString.toString();
}

bool QProtobufJsonDeserializerImpl::deserializeMessageField(QProtobufMessage *message)
{
    if (!message)
        return true;

    const auto &value = m_state.last().scalarValue;
    if (!value.isUndefined()) {
        const auto *metaObject = QtProtobufSerializerHelpers::messageMetaObject(message);
        if (auto *deserializer = QtProtobufPrivate::findCustomJsonDeserializer(metaObject
                                                                                   ->metaType())) {
            return deserializer(message, value);
        }
        setInvalidFormatError();
        return false;
    }
    return QProtobufDeserializerBase::deserializeMessageField(message);
}

bool QProtobufJsonDeserializerImpl::deserializeEnum(QVariant &value,
                                                    const QtProtobufPrivate::QProtobufFieldInfo
                                                        &fieldInfo)
{
    bool ok = false;
    auto &state = m_state.last();
    if (fieldInfo.fieldFlags().testFlag(QtProtobufPrivate::FieldFlag::Repeated)) {
        value = deserializeList<QStringList, QString>(state.scalarValue, ok);
    } else {
        // It's allowed to pass single enum value as numeric value.
        // Make the backward value conversion and deserialize enum as QtProtobuf::int64.
        if (state.scalarValue.isString()) {
            value = deserializeCommon<QString>(state.scalarValue, ok);
        } else {
            value = deserializeCommon<QtProtobuf::int64>(state.scalarValue, ok);
        }
    }

    return ok;
}

int QProtobufJsonDeserializerImpl::nextFieldIndex(QProtobufMessage *message)
{
    const auto *ordering = message->propertyOrdering();
    const int fieldCount = ordering->fieldCount();
    if (fieldCount == 0)
        return -1;

    JsonDeserializerState &state = m_state.last();
    state.scalarValue = {};
    while (state.index < fieldCount) {
        const auto jsonName = ordering->jsonName(state.index);
        const auto keys = state.obj.keys();
        const auto it = std::find_if(keys.constBegin(), keys.constEnd(),
                                     [&jsonName](const auto &val) {
                                         return jsonName == val
                                             || jsonName == convertJsonKeyToJsonName(val);
                                     });

        if (it == keys.constEnd()) {
            ++state.index;
            continue;
        }

        QtProtobufPrivate::FieldFlags flags = ordering->fieldFlags(state.index);
        QJsonValue val = state.obj.value(*it);
        if (val.isNull()) {
            ++state.index;
            continue;
        }

        int index = state.index;
        if (flags.testFlags({ QtProtobufPrivate::FieldFlag::Message,
                              QtProtobufPrivate::FieldFlag::Repeated })) {
            if (!val.isArray()) {
                setInvalidFormatError();
                return -1;
            }

            auto array = val.toArray();
            if (array.isEmpty()) {
                ++state.index;
                state.scalarValue = {};
                continue;
            }

            auto nextValue = array.takeAt(0);
            if (nextValue.isNull()) {
                setInvalidFormatError();
                return -1;
            }

            state.obj.insert(*it, array);
            if (nextValue.isObject()) {
                m_state.push_back({ nextValue.toObject() });
            } else {
                state.scalarValue = nextValue;
            }
        } else if (flags.testFlag(QtProtobufPrivate::FieldFlag::Map)) {
            if (!val.isObject()) {
                setInvalidFormatError();
                return -1;
            }

            auto mapObject = val.toObject();
            if (mapObject.isEmpty()) {
                ++state.index;
                continue;
            }

            QString key = mapObject.begin().key();
            QJsonObject nextObject;
            nextObject.insert("key"_L1, key);
            nextObject.insert("value"_L1, mapObject.take(key));
            state.obj.insert(*it, mapObject);
            m_state.push_back({ nextObject });
        } else if (flags.testFlag(QtProtobufPrivate::FieldFlag::Message)) {
            if (val.isArray() || val.isUndefined()) {
                setInvalidFormatError();
                return -1;
            }

            if (val.isObject()) {
                ++state.index;
                m_state.push_back({ val.toObject() });
            } else {
                state.scalarValue = val;
                ++state.index;
            }
        } else {
            state.scalarValue = val;
            ++state.index;
        }
        return index;
    }

    m_state.pop_back();
    return -1;
}

bool QProtobufJsonDeserializerImpl::deserializeScalarField(QVariant &value,
                                                           const QProtobufFieldInfo &)
{
    auto handler = QProtobufJsonSerializerPrivate::handlers.constFind(value.userType());
    if (handler == QProtobufJsonSerializerPrivate::handlers.constEnd()
        || !handler.value().deserializer) {
        return false;
    }

    bool ok = false;
    value = handler.value().deserializer(m_state.last().scalarValue, ok);
    if (!ok)
        setInvalidFormatError();
    return true;
}

void QProtobufJsonDeserializerImpl::setUnexpectedEndOfStreamError()
{
    setError(QAbstractProtobufSerializer::Error::UnexpectedEndOfStream,
             QCoreApplication::translate("QtProtobuf", "JSON: Unexpected end of stream"));
}

void QProtobufJsonDeserializerImpl::setInvalidFormatError()
{
    setError(QAbstractProtobufSerializer::Error::InvalidFormat,
             QCoreApplication::translate("QtProtobuf",
                                         "JSON: One or more fields have invalid format"));
}

QProtobufJsonSerializerPrivate::SerializerRegistry QProtobufJsonSerializerPrivate::handlers = {};

QProtobufJsonSerializerPrivate::QProtobufJsonSerializerPrivate() : deserializer(this)
{
    [[maybe_unused]] static bool initialized = []() -> bool {
        handlers[qMetaTypeId<QtProtobuf::int32>()] = createCommonHandler<QtProtobuf::int32>();
        handlers[qMetaTypeId<QtProtobuf::sfixed32>()] = createCommonHandler<QtProtobuf::sfixed32>();
        handlers[qMetaTypeId<QtProtobuf::sint32>()] = createCommonHandler<QtProtobuf::sint32>();
        handlers[qMetaTypeId<QtProtobuf::uint32>()] = createCommonHandler<QtProtobuf::uint32>();
        handlers[qMetaTypeId<QtProtobuf::fixed32>()] = createCommonHandler<QtProtobuf::fixed32>();
        handlers[qMetaTypeId<QtProtobuf::sint64>()] = createCommonHandler<QtProtobuf::sint64>();
        handlers[qMetaTypeId<QtProtobuf::int64>()] = createCommonHandler<QtProtobuf::int64>();
        handlers[qMetaTypeId<QtProtobuf::sfixed64>()] = createCommonHandler<QtProtobuf::sfixed64>();
        handlers[qMetaTypeId<QtProtobuf::uint64>()] = createCommonHandler<QtProtobuf::uint64>();
        handlers[qMetaTypeId<QtProtobuf::fixed64>()] = createCommonHandler<QtProtobuf::fixed64>();
        handlers[qMetaTypeId<bool>()] = createCommonHandler<bool>();
        handlers[QMetaType::QString] = createCommonHandler<QString>();
        handlers[QMetaType::QByteArray] = createCommonHandler<QByteArray>();
        handlers[QMetaType::Float] = { serializeCommon<float>, deserializeCommon<float>,
                                       QProtobufJsonSerializerPrivate::isPresent<float> };
        handlers[QMetaType::Double] = { serializeCommon<double>, deserializeCommon<double>,
                                        isPresent<double> };

        handlers[qMetaTypeId<QtProtobuf::boolList>()] = createCommonListHandler<
            QtProtobuf::boolList, bool>();
        handlers[qMetaTypeId<QtProtobuf::int32List>()] = createCommonListHandler<
            QtProtobuf::int32List, QtProtobuf::int32>();
        handlers[qMetaTypeId<QtProtobuf::int64List>()] = createCommonListHandler<
            QtProtobuf::int64List, QtProtobuf::int64>();
        handlers[qMetaTypeId<QtProtobuf::sint32List>()] = createCommonListHandler<
            QtProtobuf::sint32List, QtProtobuf::sint32>();
        handlers[qMetaTypeId<QtProtobuf::sint64List>()] = createCommonListHandler<
            QtProtobuf::sint64List, QtProtobuf::sint64>();
        handlers[qMetaTypeId<QtProtobuf::uint32List>()] = createCommonListHandler<
            QtProtobuf::uint32List, QtProtobuf::uint32>();
        handlers[qMetaTypeId<QtProtobuf::uint64List>()] = createCommonListHandler<
            QtProtobuf::uint64List, QtProtobuf::uint64>();
        handlers[qMetaTypeId<QtProtobuf::fixed32List>()] = createCommonListHandler<
            QtProtobuf::fixed32List, QtProtobuf::fixed32>();
        handlers[qMetaTypeId<QtProtobuf::fixed64List>()] = createCommonListHandler<
            QtProtobuf::fixed64List, QtProtobuf::fixed64>();
        handlers[qMetaTypeId<QtProtobuf::sfixed32List>()] = createCommonListHandler<
            QtProtobuf::sfixed32List, QtProtobuf::sfixed32>();
        handlers[qMetaTypeId<QtProtobuf::sfixed64List>()] = createCommonListHandler<
            QtProtobuf::sfixed64List, QtProtobuf::sfixed64>();
        handlers[qMetaTypeId<QtProtobuf::floatList>()] = createCommonListHandler<
            QtProtobuf::floatList, float>();
        handlers[qMetaTypeId<QtProtobuf::doubleList>()] = createCommonListHandler<
            QtProtobuf::doubleList, double>();
        handlers[qMetaTypeId<QStringList>()] = createCommonListHandler<QStringList, QString>();
        handlers[qMetaTypeId<QByteArrayList>()] = createCommonListHandler<QByteArrayList,
                                                                          QByteArray>();
        return true;
    }();
}

void QProtobufJsonSerializerPrivate::clearError()
{
    lastError = QAbstractProtobufSerializer::Error::None;
    lastErrorString.clear();
}

QProtobufJsonSerializer::QProtobufJsonSerializer() : d_ptr(new QProtobufJsonSerializerPrivate)
{
}

QProtobufJsonSerializer::~QProtobufJsonSerializer() = default;

/*!
   Returns the last deserialization error for the serializer instance.
   \sa lastErrorString()
*/
QAbstractProtobufSerializer::Error QProtobufJsonSerializer::lastError() const
{
    return d_ptr->lastError;
}

/*!
   Returns the last deserialization error string for the serializer instance.
   \sa lastError()
*/
QString QProtobufJsonSerializer::lastErrorString() const
{
    return d_ptr->lastErrorString;
}

QByteArray QProtobufJsonSerializer::serializeMessage(const QProtobufMessage *message) const
{
    d_ptr->clearError();
    d_ptr->serializer.reset();
    d_ptr->serializer.serializeMessage(message);
    auto result = QJsonDocument(d_ptr->serializer.result()).toJson(QJsonDocument::Compact);
    d_ptr->serializer.reset();
    return result;
}

bool QProtobufJsonSerializer::deserializeMessage(QProtobufMessage *message,
                                                 QByteArrayView data) const
{
    d_ptr->clearError();
    QJsonParseError err;
    auto document = QJsonDocument::fromJson(data.toByteArray(), &err);
    if (err.error != QJsonParseError::NoError) {
        d_ptr->deserializer.setUnexpectedEndOfStreamError();
        return false;
    }

    if (!document.isObject()) {
        d_ptr->deserializer.setInvalidFormatError();
        return false;
    }

    if (auto obj = document.object(); !obj.isEmpty()) {
        d_ptr->deserializer.reset(obj);
        bool result = d_ptr->deserializer.deserializeMessage(message);
        d_ptr->deserializer.reset({});
        return result;
    }
    return true;
}

bool ProtobufScalarJsonSerializers::validateJsonNumberString(const QString &input)
{
    static const QRegularExpression NumberValidator("^-?\\d+$"_L1);
    return NumberValidator.match(input).hasMatch();
}

QT_END_NAMESPACE