/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
 *
 * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "basicmessages.qpb.h"

#include <QObject>

#include <QtTest/QtTest>
#include <QProtobufJsonSerializer>

#include <limits>

class QtProtobufBasicTypesJsonDeserializationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufBasicTypesJsonDeserializationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void init()
    {
        serializer.reset(new QProtobufJsonSerializer);
    }

    void BoolDeserializeTest();
    void FixedInt32MessageDeserializeTest();
    void FixedInt64MessageDeserializeTest();
    void SFixedInt32MessageDeserializeTest();
    void SFixedInt64MessageDeserializeTest();
    void FloatMessageDeserializeTest();
    void DoubleMessageDeserializeTest();
    void Int32MessageDeserializeTest();
    void UInt32MessageDeserializeTest();
    void SInt32MessageDeserializeTest();
    void Int64MessageDeserializeTest();
    void UInt64MessageDeserializeTest();
    void SInt64MessageDeserializeTest();
    void StringMessageDeserializeTest();
    void ComplexTypeDeserializeTest();
    void BytesMessageDeserializeTest();
private:
    std::unique_ptr<QProtobufJsonSerializer> serializer;
};

using namespace qtprotobufnamespace::tests;

void QtProtobufBasicTypesJsonDeserializationTest::BoolDeserializeTest()
{
    SimpleBoolMessage test;
    test.deserialize(serializer.get(), "{\"testFieldBool\":true}");
    QVERIFY(test.testFieldBool());

    test.deserialize(serializer.get(), "{\"testFieldBool\":false}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(false);
    test.deserialize(serializer.get(), "{\"testFieldBool\":1}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(true);
    test.deserialize(serializer.get(), "{\"testFieldBool\":0}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(false);
    test.deserialize(serializer.get(), "{\"testFieldBool\":\"true\"}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(true);
    test.deserialize(serializer.get(), "{\"testFieldBool\":\"false\"}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(false);
    test.deserialize(serializer.get(), "{\"testFieldBool\":1.0}");
    QVERIFY(!test.testFieldBool());

    test.setTestFieldBool(true);
    test.deserialize(serializer.get(), "{\"testFieldBool\":0.0}");
    QVERIFY(!test.testFieldBool());
}

void QtProtobufBasicTypesJsonDeserializationTest::FixedInt32MessageDeserializeTest()
{
    SimpleFixedInt32Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\": 5}"));
    QCOMPARE(msg.testFieldFixedInt32(), 5);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\": 0}"));
    QCOMPARE(msg.testFieldFixedInt32(), 0);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":555}"));
    QCOMPARE(msg.testFieldFixedInt32(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":555}"));
    QCOMPARE(msg.testFieldFixedInt32(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":null}"));
    QCOMPARE(msg.testFieldFixedInt32(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::SFixedInt32MessageDeserializeTest()
{
    SimpleSFixedInt32Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":555}"));
    QCOMPARE(msg.testFieldFixedInt32(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":-555}"));
    QCOMPARE(msg.testFieldFixedInt32(), -555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt32\":null}"));
    QCOMPARE(msg.testFieldFixedInt32(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::SFixedInt64MessageDeserializeTest()
{
    SimpleSFixedInt64Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt64\":555}"));
    QCOMPARE(msg.testFieldFixedInt64(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt64\":-555}"));
    QCOMPARE(msg.testFieldFixedInt64(), -555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt64\":null}"));
    QCOMPARE(msg.testFieldFixedInt64(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::Int32MessageDeserializeTest()
{
    qtprotobufnamespace::tests::SimpleIntMessage msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);
}

void QtProtobufBasicTypesJsonDeserializationTest::SInt32MessageDeserializeTest()
{
    SimpleSIntMessage msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":-555}"));
    QCOMPARE(msg.testFieldInt(), -555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":null}"));
    QCOMPARE(msg.testFieldInt(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::UInt32MessageDeserializeTest()
{
    SimpleUIntMessage msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":null}"));
    QCOMPARE(msg.testFieldInt(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::FixedInt64MessageDeserializeTest()
{
    SimpleFixedInt64Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt64\":555}"));
    QCOMPARE(msg.testFieldFixedInt64(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldFixedInt64\":null}"));
    QCOMPARE(msg.testFieldFixedInt64(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::Int64MessageDeserializeTest()
{
    SimpleInt64Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":null}"));
    QCOMPARE(msg.testFieldInt(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::SInt64MessageDeserializeTest()
{
    SimpleSInt64Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":-555}"));
    QCOMPARE(msg.testFieldInt(), -555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":null}"));
    QCOMPARE(msg.testFieldInt(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::UInt64MessageDeserializeTest()
{
    SimpleUInt64Message msg;
    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":555}"));
    QCOMPARE(msg.testFieldInt(), 555);

    msg.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":null}"));
    QCOMPARE(msg.testFieldInt(), 0);
}


void QtProtobufBasicTypesJsonDeserializationTest::FloatMessageDeserializeTest()
{
    SimpleFloatMessage test;
    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": 0.1}"));
    QCOMPARE(test.testFieldFloat(), 0.1f);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": 1.17549e-38}"));
    QCOMPARE(test.testFieldFloat(), 1.17549e-38f);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": 3.40282e+38}"));
    QCOMPARE(test.testFieldFloat(), 3.40282e+38f);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": -4.2}"));
    QCOMPARE(test.testFieldFloat(), -4.2f);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": 0}"));
    QCOMPARE(test.testFieldFloat(), 0.0f);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\": 0.0}"));
    QCOMPARE(test.testFieldFloat(), 0.0f);

    test.setTestFieldFloat(-4.2f);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\":null}"));
    QCOMPARE(test.testFieldFloat(), 0);

    test.setTestFieldFloat(-4.2f);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\":\"NaN\"}"));
    QCOMPARE(test.testFieldFloat(), 0);

    test.setTestFieldFloat(-4.2f);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\":\"Infinity\"}"));
    QCOMPARE(test.testFieldFloat(), 0);

    test.setTestFieldFloat(-4.2f);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldFloat\":\"-Infinity\"}"));
    QCOMPARE(test.testFieldFloat(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::DoubleMessageDeserializeTest()
{
    SimpleDoubleMessage test;
    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\": 0.1}"));
    QCOMPARE(test.testFieldDouble(), 0.1);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\": 2.2250738585072014e-308}"));
    QCOMPARE(test.testFieldDouble(), 2.2250738585072014e-308);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\": 1.7976931348623157e+308}"));
    QCOMPARE(test.testFieldDouble(), 1.7976931348623157e+308);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\": 0}"));
    QCOMPARE(test.testFieldDouble(), 0);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\": 0.0}"));
    QCOMPARE(test.testFieldDouble(), 0);

    test.setTestFieldDouble(-4.2);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\":null}"));
    QCOMPARE(test.testFieldDouble(), 0);

    test.setTestFieldDouble(-4.2);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\":\"NaN\"}"));
    QCOMPARE(test.testFieldDouble(), 0);

    test.setTestFieldDouble(-4.2);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\":\"Infinity\"}"));
    QCOMPARE(test.testFieldDouble(), 0);

    test.setTestFieldDouble(-4.2);
    test.deserialize(serializer.get(), QByteArray("{\"testFieldDouble\":\"-Infinity\"}"));
    QCOMPARE(test.testFieldDouble(), 0);
}

void QtProtobufBasicTypesJsonDeserializationTest::StringMessageDeserializeTest()
{
    SimpleStringMessage test;
    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"qwerty\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "qwerty");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "");

    test.setTestFieldString("qwerty");
    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":null}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"null\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "null");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"NaN\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "NaN");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"Infinity\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "Infinity");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldString\":\"-Infinity\"}"));
    QCOMPARE(test.testFieldString().toStdString().c_str(), "-Infinity");
}

void QtProtobufBasicTypesJsonDeserializationTest::BytesMessageDeserializeTest()
{
    SimpleBytesMessage test;
    test.deserialize(serializer.get(), QByteArray("{\"testFieldBytes\":\"ABKEBDKBBFlWAFJmSnZqeHFiZ3ZGd1MxWXZPWlhndGo1ZmZHTFM3QAFpTkh6OW9aSW9LYm03ejhIATc5eEJ1cAlQa3BRWHZHb08wOU9ZOXhSYXd4M2VPQXM5eGpvVEExeEpocncyOFRBY3ExQ2ViWWxDOVdVZlFDNmhJYW50YU5keUhpS1RvZmZpMFp0N2xhNDJTUnhYWlNQNEd1eGJjWklwNTNwSm55Q3dmQ3kxcWRGY3pUMGRtbjdoOGZweUFkZW1FYXZ3RmVkYTRkMFBBcEdmU1Uyakx0MzlYOGtZVUJ4Tk0yV2dBTFJCZ0hkVmRlODdxNlBpNVU2OVRqaE1kMjhXMVNGRDFEeHlvZ0NDcnFPY3QyWlBJQ29MbnJxZEYzT2ROempSVkxmZXl2UThMZ0x2Uk5GUjlXZldBeUF6NzluS2dCYW1kOE50bHZ0NE1nMzVFNWdWUzJnN0FRN3JrbTcyY0Jkblc5c0NFeUdhYmVYQXVINWo0R1JidUxUN3FCWldEY0ZMRjRTc0NkUzNXZkZHZE5IZndhaWp6eWtCeW83MVB2RlZsVFhIMldKV29GdlI1RkFMakJUbjdiQ2RQMHBBaVNiTENZOFh6Mk1zYzNkQmI1RmY5R0lTUGJVcE5tVXZCZE1aTUhRdnFPbVROWEVQcE4wYjc0TURPTVFmV0pTaE9vM05rQXZNanMIKg\"}"));
    QCOMPARE(test.testFieldBytes().toHex().toStdString().c_str(), "0012840432810459560052664a766a78716267764677533159764f5a5867746a356666474c53374001694e487a396f5a496f4b626d377a38480137397842757009506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73082a");

    test.deserialize(serializer.get(), QByteArray("{\"testFieldBytes\":\"\"}"));
    QVERIFY(test.testFieldBytes().isEmpty());

    test.setTestFieldBytes("qwerty");
    test.deserialize(serializer.get(), QByteArray("{\"testFieldBytes\":null}"));
    QVERIFY(test.testFieldBytes().isEmpty());
}

void QtProtobufBasicTypesJsonDeserializationTest::ComplexTypeDeserializeTest()
{
    ComplexMessage test;

    test.deserialize(serializer.get(), QByteArray("{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":42}"));
    QCOMPARE(test.testFieldInt(), 42);
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "qwerty");

    test.setTestFieldInt(99);
    test.setTestComplexField({});
    test.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":42,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}"));
    QCOMPARE(test.testFieldInt(), 42);
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "qwerty");

    test.setTestFieldInt(0);
    test.setTestComplexField({});
    test.deserialize(serializer.get(), QByteArray("{\"testComplexField\":{\"testFieldString\":\"YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs\"},\"testFieldInt\":42}"));
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs");
    QCOMPARE(test.testFieldInt(), 42);

    test.setTestFieldInt(0);
    test.setTestComplexField({});
    test.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":42,\"testComplexField\":{\"testFieldString\":\"YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs\"}}"));
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs");
    QCOMPARE(test.testFieldInt(), 42);

    test.setTestFieldInt(0);
    test.setTestComplexField({});
    test.deserialize(serializer.get(), QByteArray("{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":-45}"));
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "qwerty");
    QCOMPARE(test.testFieldInt(), -45);

    test.setTestFieldInt(0);
    test.setTestComplexField({});
    test.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":-45,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}"));
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "qwerty");
    QCOMPARE(test.testFieldInt(), -45);

    test.deserialize(serializer.get(), QByteArray("{\"testFieldInt\":-45,\"testComplexField\":null}"));
    QCOMPARE(test.testComplexField().testFieldString().toStdString().c_str(), "");
    QCOMPARE(test.testFieldInt(), -45);
}

QTEST_MAIN(QtProtobufBasicTypesJsonDeserializationTest)
#include "deserializationbasictypes.moc"
