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

Passing QJsonObject works, but QJsonArray doesn't ?



  • I have a function defined in my class:

    Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                                 ,const QString& crstrFile
                                                 ,long lngLine);
    

    The above works fine when called from JavaScript:

    xmleng.log(0, objCmds, strThis, 164);
    

    However I need to pass a QJsonArray so I added the prototype:

     Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                                  ,const QString& crstrFile
                                                  ,long lngLine);
    

    And in the JavaScript I defined an array and call the same with:

    xmleng.log(0, aryCmds, strThis, 164);
    

    The array in JavaScript has two elements, but when I debug the C++ with a break point in the function that accepts an array, the array has 0 items.

    [Edit] I just tried modifying the JavaScript to use an object that contains an array:

    objCmds = {"commands":[]};
    objCmds.push(objCmd1);
    objCmds.push(objCmd2);
    

    The JsonObject version of the function is called, but the array is still empty....

    What's going on? It doesn't look like QJsonArray is working at all.



  • @SPlatten
    I am getting lost whether you are dealing with JavaScript versus JSON here. But are you sure your code is right? Why do you think those objCmds.push() push anything onto the [] you have? objCmds is not an array, it's an object with an item named commands whose value is an array? I would have expected something more like

    objCmds["commands"].push(...)
    

    ?



  • @JonB Doh, you are perfectly correct, blond moment....however, in my last trial, I did:

    In JavaScript:

    vr aryCmds = [], objCmds = {"commands":aryCmds};		 
    if ( typeof arguments == "object" && arguments.length > 0
      && typeof arguments[0]["IDs"] == "object" ) {
        objCmd["IDs"] = arguments[0]["IDs"];
        aryCmds.push(objCmd);
        aryCmds.push({"command":{"save":objCmd["IDs"]}});
    }
    xmleng.log(0, objCmds, strThis, 164);
    

    xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.
    Screenshot 2021-03-21 at 16.43.04.png

    Just to check it wasn't a problem with the reference I also tried:

    var aryCmds = [], objCmds = {};		 
    if ( typeof arguments == "object" && arguments.length > 0
      && typeof arguments[0]["IDs"] == "object" ) {
    	objCmd["IDs"] = arguments[0]["IDs"];
    	aryCmds.push(objCmd);
    	aryCmds.push({"command":{"save":objCmd["IDs"]}});
    }	
    objCmds["comands"] = aryCmds;
    xmleng.log(0, objCmds, strThis, 165);
    

    Still the same.



  • @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

    xmleng.log is my C++ call, when I break in this function for this call, the debugger shows 0 elements for the array.

    I can not understand how you expect this to work?!?

    As far as I can understand your code, you are creating an QVariantMap not an QJsonArray.

    I am pretty sure, if you change your function signature to:

    _INVOKABLE void log(quint16 uint16Level,const QVariantMap& crobjJSON
                                                 ,const QString& crstrFile
                                                 ,long lngLine);
    

    ==> https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object

    I don't think QJsonArray is available on QML side.



  • @KroMignon No, here is the actual prototype(s):

    Q_INVOKABLE void log(quint16 uint16Level,const QJsonArray& craryJSON
                                            ,const QString& crstrFile
                                            ,long lngLine);
    Q_INVOKABLE void log(quint16 uint16Level,const QJsonObject& crobjJSON
                                            ,const QString& crstrFile
                                            ,long lngLine);
    


  • @SPlatten Sorry, my reply goes out too fast! I have edited it, to avoid noise on this feed.



  • @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

    No, here is the actual prototype(s):

    Yes, I saw it, but as I write: I don't think QJsonArray can be created on QML side.

    I found nothing about this in Qt documentation (https://doc.qt.io/qt-5/qtqml-cppintegration-data.html).

    And, for me var objCmds = {} will be converted into a QVariantMap on C++ side



  • @KroMignon , I'm not using QML at all, this is purely JavaScript calling my API routines which are in C++.

    var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.



  • @SPlatten, can anyone help?



  • @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

    var objCmds = {} is converted to QJsonObject correctly, but var aryCmds = [] isn't converted to QJsonArray or appears not to be.

    Your code is so complex. If you want to test this theory, have you tried with a data structure which just has a var aryCmds = [...] in it, and nothing else?



  • @JonB, the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.



  • @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

    the code isn't complex, looks very simple, I've tried various different and "simple" versions just passing an array, although the array has at least one element when the C++ routine is called, when it gets to the routine the array is empty.

    What I don't understand is why you expect that Qt/C++ should implicitly convert a Java array into QJsonArray?



  • @KroMignon , it isn't Java, its JavaScript and I expect the Qt API to convert the array because it does exactly that for a JavaScript object. What is the point of offering only half a solution? If JavaScript objects are processed then arrays should also be processed.



  • @SPlatten said in Passing QJsonObject works, but QJsonArray doesn't ?:

    it isn't Java, its JavaScript and I expect the Qt API to convert the array because it does exactly that for a JavaScript object. What is the point of offering only half a solution? If JavaScript objects are processed then arrays should also be processed.

    Yes, you are right it is JavaScript, and JavaScript is not known as strongly typed language.
    There are many implicit conversion done, so awaiting that this is just working seems not really serious to me.

    I wish you good luke!


Log in to reply