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
QtWS25 Last Chance

QJsonArray, type question

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 542 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #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
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by SPlatten
      #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
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        SPlattenS 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

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

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #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
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            • Login

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