Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QJsonArray, type question



  • 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.



  • 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];
    }```


  • 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];
    }```

  • Qt Champions 2019

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



  • @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.


  • Qt Champions 2019

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


Log in to reply