[Development] QJsonArray concatenation

Sze Howe Koh szehowe.koh at gmail.com
Mon Apr 27 01:56:35 CEST 2015


Hello,

This is the cleanest way I can currently think of to concatenate 2
QJsonArrays:

    for (const auto& value : array2)
        array1 << value;

Most of Qt's array-like containers (QVector<T>, QList<T>, QByteArray,
QString) have concatenation functions, but QJsonArray doesn't. So, I
propose adding the following overloads, to match the other containers:

- QJsonArray::operator+(const QJsonArray&)
- QJsonArray::operator+=(const QJsonArray&)
- QJsonArray::operator<<(const QJsonArray&)


The gotcha: This can cause a behaviour change. Currently, inputting
QJsonArray into operator+=() makes valid code -- the QJsonArray is
implicitly converted into a single QJsonValue first. My proposal would make
the same code avoid the conversion and append each element individually
instead. Is this acceptable for Qt 5.x? (I don't know how widely-used is
the implicit conversion to QJsonValue)

Note that there has been one other behaviour change that I know of:
https://forum.qt.io/topic/50282/

    QJsonArray array1;
    // ...
    QJsonArray array2{array1};

In Qt 5.3, array2 is a copy of array1. However, Qt 5.4 introduced the
initializer list-based constructor. This constructor implicitly converts
array1 into a single QJsonValue, so array2 contains one element only.
Ideally, this change would not have happened (and ideally, my proposed
overloads would have been added back in Qt 5.3, when the single-element
versions were added).

So, we can now choose between maintaining SC, or having a more functional
API that's more consistent with other containers. Which is the correct
choice?


Regards,
Sze-Howe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20150427/093c15b8/attachment.html>


More information about the Development mailing list