Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Strange Json issue, conversion from QByteArray to QJsonDocument not working as intended

    General and Desktop
    json qtcreator installation bytearray
    5
    16
    4272
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      DanBar last edited by

      I have a strange issue working with Json...

      I receive an encrypted byte array from a network source, the length varies but currently under testing it is an array of either about 100 characters or a bit over 20 000 characters. I output the recieved byte array (after decryption) and the resulting json document to a log file to keep track (see in the code below)

      When I run a debug in Qt Creator (version 4.5.1, Based on Qt 5.10.1 (MSVC 2015, 32 bit)) everything works fine, but when I build an installation file and install it I get this really weird issue. The short array (about 100 characters) still works as it should, but the long one doesn't. The output in the log file of the "tmpTestJson" is just empty. I can see that all characters are received, but for some reason the conversion to Json document isn't working.

      Can a Json character array be too long? Why do I get an issue in the installed version but not in the debug version? Any help is appreciated.

      This is the code where i have the issue:

      QByteArray configData = reply->readAll();
      AESCryptoUtil aes;
      QByteArray decryptedConfigData = aes.Decrypt(QByteArray::fromBase64(configData));
      emit log(QString("received data: " + decryptedConfigData));
      
      QJsonDocument configJsonDoc = QJsonDocument::fromJson(decryptedConfigData);
      QString tmpTestJson = configJsonDoc.toJson(QJsonDocument::Indented);
      log(QString("Json Document: " + tmpTestJson));
      
      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @DanBar last edited by

        @DanBar said in Strange Json issue, conversion from QByteArray to QJsonDocument not working as intended:

        QByteArray configData = reply->readAll();

        are you 100% sure that at the time you call reply->readAll() you really have received all data (=valid JSON data)?!

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        D 1 Reply Last reply Reply Quote 4
        • D
          DanBar @raven-worx last edited by

          @raven-worx I get the entire bytearray (including the finishing brackets) when "decryptedConfigData" is printed, but nothing when "tmpTestJson". As far as I know that means that I get all the data and should have valid Json data. The printed "decryptedConfigData" is identical when running in the debugger and the installed version.

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @DanBar last edited by

            @DanBar
            What is the output of the following?

            QJsonParseError jsonError;
            QJsonDocument configJsonDoc = QJsonDocument::fromJson(decryptedConfigData, &jsonError);
            if( jsonError.error != QJsonParseError::NoError )
                 log( QString("Json error: %1").arg(jsonError.errorString()) );
            else if( configJsonDoc .isNull() )
                 log( "Null JsonDocument );
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            D 1 Reply Last reply Reply Quote 3
            • D
              DanBar @raven-worx last edited by

              @raven-worx I get the following:

              "Json error: illegal number"

              what does that mean?

              jsulm raven-worx 2 Replies Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @DanBar last edited by jsulm

                @DanBar Can you show the JSON string you get?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • raven-worx
                  raven-worx Moderators @DanBar last edited by raven-worx

                  @DanBar said in Strange Json issue, conversion from QByteArray to QJsonDocument not working as intended:

                  "Json error: illegal number"
                  what does that mean?

                  possibly an integer/decimal value beginning with 0 (e.g. propertyName: 01234)? unless it's an octal value.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  D 1 Reply Last reply Reply Quote 0
                  • D
                    DanBar @raven-worx last edited by

                    @raven-worx There are many values that are 0 and a few values that are decimal, are those allowed while running in Qtcreator but not as an installed program?

                    a sample part of the json:
                    "name": "Test 4",
                    "valueA": 180,
                    "valueB": "",
                    "valueC": 9,
                    "valueD": 1,
                    "valueE": 0.8869204367,
                    "valueF": 1500,
                    "valueG": "C",
                    "valueH": 0,
                    "valueI": "16d29c44-3d0941",
                    "valueJ": "1392fa45-68c943",
                    "valueK": "2018-08-06 09:07:29",

                    mrjj raven-worx 2 Replies Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @DanBar last edited by

                      @DanBar
                      Hi
                      You you also output
                      http://doc.qt.io/qt-5/qjsonparseerror.html#offset-var
                      as it gives a clue where in the file it gets error.

                      1 Reply Last reply Reply Quote 0
                      • raven-worx
                        raven-worx Moderators @DanBar last edited by raven-worx

                        @DanBar said in Strange Json issue, conversion from QByteArray to QJsonDocument not working as intended:

                        There are many values that are 0 and a few values that are decimal, are those allowed while running in Qtcreator but not as an installed program?
                        a sample part of the json:
                        "name": "Test 4",
                        "valueA": 180,
                        "valueB": "",
                        "valueC": 9,
                        "valueD": 1,
                        "valueE": 0.8869204367,
                        "valueF": 1500,
                        "valueG": "C",
                        "valueH": 0,
                        "valueI": "16d29c44-3d0941",
                        "valueJ": "1392fa45-68c943",
                        "valueK": "2018-08-06 09:07:29",

                        none of those values should raise this error as far i see.
                        I was talking about decimals bigger than 0. So 0 is of course a valid decimal value, but 02934 not

                        But you havent answered my first question. Are you sure you already received the full data before you start to parse the JSON data?!

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          Hi,

                          How long is the long array that makes things fail ?

                          See QTBUG-47629 for a limitation on QJsonObject's size.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply Reply Quote 0
                          • D
                            DanBar last edited by

                            Thank you all for your help!

                            I was able to solve this issue, but the problem wasn't in the Json itself. I got a tip a tip from my colleague to check the .dll-files in the installation folder, and I noticed that they weren't entirely up to date. When I replaced them with the correct version of .dll-files the issue was solved. I still don't know exactly what caused this issue between the versions (version 5.9 and 5.10) but the issue is solved so I'm happy in either case.

                            Again, thanks!

                            jsulm 1 Reply Last reply Reply Quote 1
                            • jsulm
                              jsulm Lifetime Qt Champion @DanBar last edited by

                              @DanBar said in Strange Json issue, conversion from QByteArray to QJsonDocument not working as intended:

                              installation folder

                              Which installation folder do you mean? Qt? Your app?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              D 1 Reply Last reply Reply Quote 0
                              • D
                                DanBar @jsulm last edited by

                                @jsulm Not the Qt folder, my own installation folder for the software I'm creating.

                                jsulm 1 Reply Last reply Reply Quote 0
                                • jsulm
                                  jsulm Lifetime Qt Champion @DanBar last edited by

                                  @DanBar How do you create this installation directory? Qt provides tools for deployment.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  D 1 Reply Last reply Reply Quote 0
                                  • D
                                    DanBar @jsulm last edited by

                                    @jsulm we make an installation file for our combined system, including multiple programs sharing some but not other resources. we use an nsi script to manage this. usually works fine. I just missed updating some of the .dll files.

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post