Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonArray, type question

QJsonArray, type question

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 550 Views
  • 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 6 Sept 2020, 15:32 last edited by SPlatten 9 Jun 2020, 16:03
    #1

    I am working with the QJson classes and putting together a descriptive string using the methods available. One such method is type(). I see in the debugger that if I test a QJsonValue for it's type using isArray() it returns true which is correct in the case I'm testing, but what I don't understand is when I then use:

    QMetaType::typeName(jsonValue.type());
    

    What I'm seeing returned for an array is:

    qlonglong
    

    Why? Is this a bug? Shouldn't it be array?

    The other types seem correct.

    Here is the code that creates the object:

    QJsonArray aryTest;
    QJsonObject objTest;
    QJsonObject test;
    aryTest.insert(0, QJsonValue("a"));
    aryTest.insert(1, QJsonValue("b"));
    aryTest.insert(2, QJsonValue("c"));
    test.insert("aryTest", aryTest);
    objTest.insert("a", QJsonValue("d"));
    objTest.insert("b", QJsonValue("e"));
    objTest.insert("c", QJsonValue("f"));
    test.insert("objTest", objTest);
    

    Something is clearly wrong, when I single step through this object, for the first element and the QJsonValue("a"), I'm actually getting:

    0 for the index which is correct, but "uint" for the type which isn't correct, the value is "a" which is also correct.

    On searching for a solution it seems that the only supported types are:

    QJsonValue::Null	Empty QVariant
    QJsonValue::Bool	QMetaType::Bool
    QJsonValue::Double	QMetaType::Double
    QJsonValue::String	QString
    QJsonValue::Array	QVariantList
    QJsonValue::Object	QVariantMap
    QJsonValue::Undefined	Empty QVariant
    

    And it looks like I will need to revert back to my original method which was a QStringList of types.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SPlatten
      wrote on 6 Sept 2020, 16:19 last edited by SPlatten 9 Jun 2020, 16:59
      #2

      My solution, static array of types, values obtained from https://doc.qt.io/qt-5/qjsonvalue.html#Type-enum:

      QString clsScriptHelper::strType(QJsonValue::Type type) {
          static const uint8_t scuint8Undefined = 0x80
                              ,scuint8UndefinedIdx = 6;
          static const char* scpszTypes[] = {"Null"       /*0*/
                                            ,"Bool"       /*1*/
                                            ,"Double"     /*2*/
                                            ,"String"     /*3*/
                                            ,"Array"      /*4*/
                                            ,"Object"     /*5*/
                                            ,"Undefined"  /*6*/
                                              };
          uint8_t uint8Idx = scuint8Undefined;
      
          if ( type != scuint8Undefined ) {
              uint8Idx = static_cast<uint8_t>(type);
          }
          if ( uint8Idx >= scuint8Undefined ) {
              uint8Idx = scuint8UndefinedIdx;
          }
          return scpszTypes[uint8Idx];
      }```

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 6 Sept 2020, 16:34 last edited by
        #3

        Please provide some compilable code - I don't understand the problem or where you call jsonValue.type() or the problem with the first element.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply 6 Sept 2020, 16:44
        0
        • C Christian Ehrlicher
          6 Sept 2020, 16:34

          Please provide some compilable code - I don't understand the problem or where you call jsonValue.type() or the problem with the first element.

          S Offline
          S Offline
          SPlatten
          wrote on 6 Sept 2020, 16:44 last edited by SPlatten 9 Jun 2020, 16:45
          #4

          @Christian-Ehrlicher I think it was pretty obvious, the QJson classes have a ::type function that returns a numeric representation of the type, there isn't a function that translates this into a string.

          That's what I wanted.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 6 Sept 2020, 16:50 last edited by
            #5

            It returns a QMetaType so you should take a look for such a function there and not in the json classes.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0

            2/5

            6 Sept 2020, 16:19

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved