Unsolved Passing QJsonObject works, but QJsonArray doesn't ?
-
@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 thoseobjCmds.push()
push anything onto the[]
you have?objCmds
is not an array, it's an object with an item namedcommands
whose value is an array? I would have expected something more likeobjCmds["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.
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 anQJsonArray
.I am pretty sure, if you change your function signature to:
_INVOKABLE void log(quint16 uint16Level,const QVariantMap& crobjJSON ,const QString& crstrFile ,long lngLine);
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 aQVariantMap
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!