Re: [Interest] Can't JSON.stringify an object in QML
Hello, It seems your App.result is not a js object, but a c++ object derived from QObject. Is it? On 5/28/2021 10:24 PM, Jason H wrote: I've got a really weird error: qml: type: object [DEBUG-12344567890,N] qrc:/screens/Result.qml:32: TypeError: Type error The statements are: console.log("type:", typeof App.result, Object.keys(App.result)) // this works console.log("result:", JSON.stringify(App.result)); // this does not App is an import (`import "app.js" as App`) result is s simple JS Object: var result = { "DEBUG-12344567890": {...},"N": { ... } }; Is it possible to figure out why the stringification is failing? How could it even fail? Better yet, can it fail with a better error. or are we locked into some terrible Javascript standard? I added a C++ function to print it out, but the contents are as I expect. QString Wtf::stringify(const QJSValue ) const { QVariant v = var.toVariant(); return QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact); // works } Any idea why that console.log is failing? ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Can't JSON.stringify an object in QML
> Sent: Tuesday, June 01, 2021 at 9:22 PM > From: "Tony Rietwyk" > To: interest@qt-project.org > Subject: Re: [Interest] Can't JSON.stringify an object in QML > > HI Jason, > > I'm pretty sure this is NOT valid JSON: > >{"that": "other": {} } > > Either the value and comma for "that" is missing, or "other": {} should Ok, so I was typing that as an example, and meant to type: {"that": {"other": {} } } In that I was trying to illustrate an empty object (which may or may not be valid JSON)... but it works with Qt's QJsonDocument::toJson() ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Can't JSON.stringify an object in QML
HI Jason, I'm pretty sure this is NOT valid JSON: {"that": "other": {} } Either the value and comma for "that" is missing, or "other": {} should be encapsulated as an object in braces as the value of "that". Regards, Tony On 2/06/2021 12:29 am, Jason H wrote: Some JSON serialisers replace that with null nowadays. I guess you could write some code to test whether it’s cyclical and at which elements it recurses. If you feel not up to it, write back and I’ll try (ECMAscript is not a language I feel comfortable in either but I think I could manage). Well, if it were cyclical (I really don't think it is), I would expect that the line: return QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact); Would never return. However it does, and it gives me the exact string I would expect. So I'm still at a loss there. The object does contain an empty object, could that be it? For example: {"that": "other": {} } ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Can't JSON.stringify an object in QML
On Tue, 1 Jun 2021, Jason H wrote: > > Well, if it were cyclical (I really don't think it is), I would expect that > the line: > return QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact); > > Would never return. No, error-tolerant behaviour just returns null in the place where the self-recursion is first detected. bye, //mirabilos -- Infrastrukturexperte • tarent solutions GmbH Am Dickobskreuz 10, D-53121 Bonn • http://www.tarent.de/ Telephon +49 228 54881-393 • Fax: +49 228 54881-235 HRB AG Bonn 5168 • USt-ID (VAT): DE122264941 Geschäftsführer: Dr. Stefan Barth, Kai Ebenrett, Boris Esser, Alexander Steeg * Mit dem tarent-Newsletter nichts mehr verpassen: www.tarent.de/newsletter * ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Can't JSON.stringify an object in QML
> Some JSON serialisers replace that with null nowadays. > > I guess you could write some code to test whether it’s cyclical > and at which elements it recurses. If you feel not up to it, > write back and I’ll try (ECMAscript is not a language I feel > comfortable in either but I think I could manage). Well, if it were cyclical (I really don't think it is), I would expect that the line: return QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact); Would never return. However it does, and it gives me the exact string I would expect. So I'm still at a loss there. The object does contain an empty object, could that be it? For example: {"that": "other": {} } ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Can't JSON.stringify an object in QML
On Fri, 28 May 2021, Jason H wrote: > console.log("result:", JSON.stringify(App.result)); // this does not > qrc:/screens/Result.qml:32: TypeError: Type error My copy of ECMA-262, 5ᵗʰ edition, has JSON.stringify fail (in the “abstract operation JO”, p.205, last ordered list on the page, step 1) with TypeError if the object is cyclical, i.e. contains a reference to itself. Some JSON serialisers replace that with null nowadays. I guess you could write some code to test whether it’s cyclical and at which elements it recurses. If you feel not up to it, write back and I’ll try (ECMAscript is not a language I feel comfortable in either but I think I could manage). bye, //mirabilos -- Infrastrukturexperte • tarent solutions GmbH Am Dickobskreuz 10, D-53121 Bonn • http://www.tarent.de/ Telephon +49 228 54881-393 • Fax: +49 228 54881-235 HRB AG Bonn 5168 • USt-ID (VAT): DE122264941 Geschäftsführer: Dr. Stefan Barth, Kai Ebenrett, Boris Esser, Alexander Steeg * Mit dem tarent-Newsletter nichts mehr verpassen: www.tarent.de/newsletter * ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest