Add default value for getters in QJsonValue
Done-with: Debao Zhang <dbzhang800@gmail.com> Change-Id: I3ddd8dd89dc75d91ac9977bf9b6eb3174d7669e4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
parent
d6bb52b196
commit
54d9fd61e1
@ -369,36 +369,36 @@ QJsonValue::Type QJsonValue::type() const
|
|||||||
/*!
|
/*!
|
||||||
Converts the value to a bool and returns it.
|
Converts the value to a bool and returns it.
|
||||||
|
|
||||||
If type() is not bool, false will be returned.
|
If type() is not bool, the defaultValue will be returned.
|
||||||
*/
|
*/
|
||||||
bool QJsonValue::toBool() const
|
bool QJsonValue::toBool(bool defaultValue) const
|
||||||
{
|
{
|
||||||
if (t != Bool)
|
if (t != Bool)
|
||||||
return false;
|
return defaultValue;
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Converts the value to a double and returns it.
|
Converts the value to a double and returns it.
|
||||||
|
|
||||||
If type() is not Double, 0. will be returned.
|
If type() is not Double, the defaultValue will be returned.
|
||||||
*/
|
*/
|
||||||
double QJsonValue::toDouble() const
|
double QJsonValue::toDouble(double defaultValue) const
|
||||||
{
|
{
|
||||||
if (t != Double)
|
if (t != Double)
|
||||||
return 0;
|
return defaultValue;
|
||||||
return dbl;
|
return dbl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Converts the value to a QString and returns it.
|
Converts the value to a QString and returns it.
|
||||||
|
|
||||||
If type() is not String, a QString() will be returned.
|
If type() is not String, the defaultValue will be returned.
|
||||||
*/
|
*/
|
||||||
QString QJsonValue::toString() const
|
QString QJsonValue::toString(const QString &defaultValue) const
|
||||||
{
|
{
|
||||||
if (t != String)
|
if (t != String)
|
||||||
return QString();
|
return defaultValue;
|
||||||
stringData->ref.ref(); // the constructor below doesn't add a ref.
|
stringData->ref.ref(); // the constructor below doesn't add a ref.
|
||||||
QStringDataPtr holder = { stringData };
|
QStringDataPtr holder = { stringData };
|
||||||
return QString(holder);
|
return QString(holder);
|
||||||
@ -407,27 +407,51 @@ QString QJsonValue::toString() const
|
|||||||
/*!
|
/*!
|
||||||
Converts the value to an array and returns it.
|
Converts the value to an array and returns it.
|
||||||
|
|
||||||
If type() is not Array, a QJsonArray() will be returned.
|
If type() is not Array, the defaultValue will be returned.
|
||||||
*/
|
*/
|
||||||
QJsonArray QJsonValue::toArray() const
|
QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const
|
||||||
{
|
{
|
||||||
if (!d || t != Array)
|
if (!d || t != Array)
|
||||||
return QJsonArray();
|
return defaultValue;
|
||||||
|
|
||||||
return QJsonArray(d, static_cast<QJsonPrivate::Array *>(base));
|
return QJsonArray(d, static_cast<QJsonPrivate::Array *>(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\overload
|
||||||
|
|
||||||
|
Converts the value to an array and returns it.
|
||||||
|
|
||||||
|
If type() is not Array, a QJsonArray() will be returned.
|
||||||
|
*/
|
||||||
|
QJsonArray QJsonValue::toArray() const
|
||||||
|
{
|
||||||
|
return toArray(QJsonArray());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Converts the value to an object and returns it.
|
Converts the value to an object and returns it.
|
||||||
|
|
||||||
If type() is not Object, a QJsonObject() will be returned.
|
If type() is not Object, the defaultValue will be returned.
|
||||||
|
*/
|
||||||
|
QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const
|
||||||
|
{
|
||||||
|
if (!d || t != Object)
|
||||||
|
return defaultValue;
|
||||||
|
|
||||||
|
return QJsonObject(d, static_cast<QJsonPrivate::Object *>(base));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\overload
|
||||||
|
|
||||||
|
Converts the value to an object and returns it.
|
||||||
|
|
||||||
|
If type() is not Object, the QJsonObject() will be returned.
|
||||||
*/
|
*/
|
||||||
QJsonObject QJsonValue::toObject() const
|
QJsonObject QJsonValue::toObject() const
|
||||||
{
|
{
|
||||||
if (!d || t != Object)
|
return toObject(QJsonObject());
|
||||||
return QJsonObject();
|
|
||||||
|
|
||||||
return QJsonObject(d, static_cast<QJsonPrivate::Object *>(base));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -103,11 +103,13 @@ public:
|
|||||||
inline bool isObject() const { return type() == Object; }
|
inline bool isObject() const { return type() == Object; }
|
||||||
inline bool isUndefined() const { return type() == Undefined; }
|
inline bool isUndefined() const { return type() == Undefined; }
|
||||||
|
|
||||||
bool toBool() const;
|
bool toBool(bool defaultValue = false) const;
|
||||||
double toDouble() const;
|
double toDouble(double defaultValue = 0) const;
|
||||||
QString toString() const;
|
QString toString(const QString &defaultValue = QString()) const;
|
||||||
QJsonArray toArray() const;
|
QJsonArray toArray() const;
|
||||||
|
QJsonArray toArray(const QJsonArray &defaultValue) const;
|
||||||
QJsonObject toObject() const;
|
QJsonObject toObject() const;
|
||||||
|
QJsonObject toObject(const QJsonObject &defaultValue) const;
|
||||||
|
|
||||||
bool operator==(const QJsonValue &other) const;
|
bool operator==(const QJsonValue &other) const;
|
||||||
bool operator!=(const QJsonValue &other) const;
|
bool operator!=(const QJsonValue &other) const;
|
||||||
|
@ -154,6 +154,12 @@ void TestQtJson::cleanup()
|
|||||||
|
|
||||||
void TestQtJson::testValueSimple()
|
void TestQtJson::testValueSimple()
|
||||||
{
|
{
|
||||||
|
QJsonObject object;
|
||||||
|
object.insert("number", 999.);
|
||||||
|
QJsonArray array;
|
||||||
|
for (int i = 0; i < 10; ++i)
|
||||||
|
array.append((double)i);
|
||||||
|
|
||||||
QJsonValue value(true);
|
QJsonValue value(true);
|
||||||
QCOMPARE(value.type(), QJsonValue::Bool);
|
QCOMPARE(value.type(), QJsonValue::Bool);
|
||||||
QCOMPARE(value.toDouble(), 0.);
|
QCOMPARE(value.toDouble(), 0.);
|
||||||
@ -161,12 +167,17 @@ void TestQtJson::testValueSimple()
|
|||||||
QCOMPARE(value.toBool(), true);
|
QCOMPARE(value.toBool(), true);
|
||||||
QCOMPARE(value.toObject(), QJsonObject());
|
QCOMPARE(value.toObject(), QJsonObject());
|
||||||
QCOMPARE(value.toArray(), QJsonArray());
|
QCOMPARE(value.toArray(), QJsonArray());
|
||||||
|
QCOMPARE(value.toDouble(99.), 99.);
|
||||||
|
QCOMPARE(value.toString(QString("test")), QString("test"));
|
||||||
|
QCOMPARE(value.toObject(object), object);
|
||||||
|
QCOMPARE(value.toArray(array), array);
|
||||||
|
|
||||||
value = 999.;
|
value = 999.;
|
||||||
QCOMPARE(value.type(), QJsonValue::Double);
|
QCOMPARE(value.type(), QJsonValue::Double);
|
||||||
QCOMPARE(value.toDouble(), 999.);
|
QCOMPARE(value.toDouble(), 999.);
|
||||||
QCOMPARE(value.toString(), QString());
|
QCOMPARE(value.toString(), QString());
|
||||||
QCOMPARE(value.toBool(), false);
|
QCOMPARE(value.toBool(), false);
|
||||||
|
QCOMPARE(value.toBool(true), true);
|
||||||
QCOMPARE(value.toObject(), QJsonObject());
|
QCOMPARE(value.toObject(), QJsonObject());
|
||||||
QCOMPARE(value.toArray(), QJsonArray());
|
QCOMPARE(value.toArray(), QJsonArray());
|
||||||
|
|
||||||
@ -190,7 +201,6 @@ void TestQtJson::testValueSimple()
|
|||||||
QCOMPARE(value.toBool(), false);
|
QCOMPARE(value.toBool(), false);
|
||||||
QCOMPARE(value.toObject(), QJsonObject());
|
QCOMPARE(value.toObject(), QJsonObject());
|
||||||
QCOMPARE(value.toArray(), QJsonArray());
|
QCOMPARE(value.toArray(), QJsonArray());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestQtJson::testNumbers()
|
void TestQtJson::testNumbers()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user