[Development] Serialising UI state in QML via QSettings and JSON: QByteArray vs QString

Mitch Curtis mitch.curtis at qt.io
Tue Oct 30 14:13:54 CET 2018



> -----Original Message-----
> From: Development <development-bounces+mitch.curtis=qt.io at qt-
> project.org> On Behalf Of Mitch Curtis
> Sent: Tuesday, 30 October 2018 12:30 PM
> To: Edward Welbourne <edward.welbourne at qt.io>
> Cc: Qt development mailing list <development at qt-project.org>
> Subject: Re: [Development] Serialising UI state in QML via QSettings and
> JSON: QByteArray vs QString
> 
> > -----Original Message-----
> > From: Edward Welbourne
> > Sent: Tuesday, 30 October 2018 11:37 AM
> > To: Mitch Curtis <mitch.curtis at qt.io>
> > Cc: Qt development mailing list <development at qt-project.org>
> > Subject: Re: [Development] Serialising UI state in QML via QSettings
> > and
> > JSON: QByteArray vs QString
> >
> > Edward Welbourne (Monday, 29 October 2018 7:31 PM) wrote
> > >> I'm not sure where JSON got involved in that
> >
> > Mitch Curtis (30 October 2018 08:37) replied
> > > What do you mean? It's in the title of the email; it's a popular
> > > choice for storing data like this, so I want SplitView's
> > > serialisation to be compatible with it.
> >
> > Perhaps I need to rephrase: to what extent is the choice of JSON, in
> > the present discussion, necessary or incidental ?  If what you're
> > trying to do has to use JSON with QSettings, then it's necessary.  If
> > JSON just looked, at first sight, like it would solve your primary
> > problem, then it's incidental and can be replaced by something compatible.
> >
> > 	Eddy.
> 
> I want users to be able to call SplitView's saveState() function and store the
> result in a JSON file. To do that, it needs to be able be stored in a QVariant
> because QJsonObject isn't exposed to QML. The next issue for users would
> be the lack of ability to convert that QVariant into a QJsonObject.
> 
> By the way, QCborValue and friends is working quite well so far. :)

I ran into an issue. An approach that uses QCborArray/Map/Value to serialise SplitView's state into a byte array still runs into the same problem when it comes to JSON files:

https://doc-snapshots.qt.io/qt5-5.12/qcborvalue.html#toJsonValue

This table says that byte arrays will be "converted to a lossless encoding like Base64url, but the distinction between strings and byte arrays is lost". So even if I were to document that QCborValue is used to save the state (which I don't really want to do), someone wanting to serialise their UI state to a JSON file would need to use QCbor* API to do so, and at that point the type information that identifies the data as a byte array is lost anyway.

Since QCborValue also uses the Base64 string approach and it what is what I was planning on doing if no other solution came up, I think I will just go ahead and return a Base64 QString from QQuickSplitView::saveState() instead of a QVariant containing a QByteArray. I will use the CBOR stuff to serialise it though, as it's much nicer than using QDataStream.

> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list