diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2012-01-09 16:47:48 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 21:02:44 +0100 |
commit | 0d8eba82a4a5307ff785d157749adf66341a896a (patch) | |
tree | 4126dd8b7fa58df8391acec5d36c24c5aeb67736 | |
parent | 81dc96b485fe093c0fc9c6abf1be6da85d5b38a8 (diff) |
Added debug stream operators
Change-Id: Icaf45e37e41a54558a9c14cbb03e4fd201aad37f
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
-rw-r--r-- | src/qjsonarray.cpp | 23 | ||||
-rw-r--r-- | src/qjsonarray.h | 5 | ||||
-rw-r--r-- | src/qjsondocument.cpp | 26 | ||||
-rw-r--r-- | src/qjsondocument.h | 6 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 22 | ||||
-rw-r--r-- | src/qjsonobject.h | 6 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 40 | ||||
-rw-r--r-- | src/qjsonvalue.h | 5 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 48 | ||||
-rw-r--r-- | src/qjsonwriter_p.h | 4 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 88 |
11 files changed, 243 insertions, 30 deletions
diff --git a/src/qjsonarray.cpp b/src/qjsonarray.cpp index f413c41..f481eb4 100644 --- a/src/qjsonarray.cpp +++ b/src/qjsonarray.cpp @@ -44,11 +44,12 @@ #include <qjsonarray.h> #include <qjson_p.h> #include <qjsonvalue.h> +#include <qjsonwriter_p.h> #include <qstringlist.h> #include <qdebug.h> -using namespace QtJson; +namespace QtJson { QJsonArray::QJsonArray() : d(0), a(0) @@ -309,3 +310,23 @@ void QJsonArray::compact() const d->compact(); const_cast<QJsonArray *>(this)->a = static_cast<Private::Array *>(d->header->root()); } + +} // namespace QtJson + +QT_BEGIN_NAMESPACE + +QDebug operator<<(QDebug dbg, const QtJson::QJsonArray &a) +{ + if (!a.d) { + dbg << "QJsonArray()"; + return dbg; + } + QByteArray json; + QtJson::QJsonWriter::arrayToJson(static_cast<QtJson::Private::Array *>(a.d->header->root()), json, 0, true); + dbg.nospace() << "QJsonArray(" + << json.constData() // print as utf-8 string without extra quotation marks + << ")"; + return dbg.space(); +} + +QT_END_NAMESPACE diff --git a/src/qjsonarray.h b/src/qjsonarray.h index 6e52a6b..94296cf 100644 --- a/src/qjsonarray.h +++ b/src/qjsonarray.h @@ -44,7 +44,11 @@ #include <qjsonglobal.h> #include <qvariant.h> + +QT_BEGIN_NAMESPACE class QStringList; +Q_JSON_EXPORT QDebug operator<<(QDebug, const QtJson::QJsonArray &); +QT_END_NAMESPACE namespace QtJson { @@ -89,6 +93,7 @@ private: friend class Private::Data; friend class QJsonValue; friend class QJsonDocument; + friend QT_PREPEND_NAMESPACE(QDebug) (QT_PREPEND_NAMESPACE(operator<<)) (QT_PREPEND_NAMESPACE(QDebug) dbg, const QJsonArray &a); QJsonArray(Private::Data *data, Private::Array *array); void compact() const; diff --git a/src/qjsondocument.cpp b/src/qjsondocument.cpp index 4702428..764e963 100644 --- a/src/qjsondocument.cpp +++ b/src/qjsondocument.cpp @@ -48,8 +48,7 @@ #include <qjsonparser_p.h> #include <qstringlist.h> -using namespace QtJson; - +namespace QtJson { QJsonDocument::QJsonDocument() : d(0) @@ -301,3 +300,26 @@ bool QJsonDocument::isValid() } return d != 0; } + +} // namespace QtJson + +QT_BEGIN_NAMESPACE + +QDebug operator<<(QDebug dbg, const QtJson::QJsonDocument &o) +{ + if (!o.d) { + dbg << "QJsonDocument()"; + return dbg; + } + QByteArray json; + if (o.d->header->root()->isArray()) + QtJson::QJsonWriter::arrayToJson(static_cast<QtJson::Private::Array *>(o.d->header->root()), json, 0, true); + else + QtJson::QJsonWriter::objectToJson(static_cast<QtJson::Private::Object *>(o.d->header->root()), json, 0, true); + dbg.nospace() << "QJsonDocument(" + << json.constData() // print as utf-8 string without extra quotation marks + << ")"; + return dbg.space(); +} + +QT_END_NAMESPACE diff --git a/src/qjsondocument.h b/src/qjsondocument.h index ba94729..639447b 100644 --- a/src/qjsondocument.h +++ b/src/qjsondocument.h @@ -46,6 +46,10 @@ #include <qjsonvalue.h> #include <qvariant.h> +QT_BEGIN_NAMESPACE +Q_JSON_EXPORT QDebug operator<<(QDebug, const QtJson::QJsonDocument &); +QT_END_NAMESPACE + namespace QtJson { class Q_JSON_EXPORT QJsonDocument @@ -89,6 +93,8 @@ private: friend class Private::Data; friend class QJsonValue; friend class QJsonParser; + friend QT_PREPEND_NAMESPACE(QDebug) (QT_PREPEND_NAMESPACE(operator<<)) (QT_PREPEND_NAMESPACE(QDebug) dbg, const QJsonDocument &a); + QJsonDocument(Private::Data *data); Private::Data *d; diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index bbe9b80..219790e 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -47,7 +47,7 @@ #include <qstringlist.h> #include <qdebug.h> -using namespace QtJson; +namespace QtJson { QJsonObject::QJsonObject() : d(0), o(0) @@ -297,3 +297,23 @@ void QJsonObject::compact() const d->compact(); const_cast<QJsonObject *>(this)->o = static_cast<Private::Object *>(d->header->root()); } + +} // namespace QtJson + +QT_BEGIN_NAMESPACE + +QDebug operator<<(QDebug dbg, const QtJson::QJsonObject &o) +{ + if (!o.d) { + dbg << "QJsonObject()"; + return dbg; + } + QByteArray json; + QtJson::QJsonWriter::objectToJson(static_cast<QtJson::Private::Object *>(o.d->header->root()), json, 0, true); + dbg.nospace() << "QJsonObject(" + << json.constData() // print as utf-8 string without extra quotation marks + << ")"; + return dbg.space(); +} + +QT_END_NAMESPACE diff --git a/src/qjsonobject.h b/src/qjsonobject.h index 29910b7..7b3cae7 100644 --- a/src/qjsonobject.h +++ b/src/qjsonobject.h @@ -44,6 +44,11 @@ #include <qjsonglobal.h> #include <qvariant.h> +#include <qdebug.h> + +QT_BEGIN_NAMESPACE +Q_JSON_EXPORT QDebug operator<<(QDebug, const QtJson::QJsonObject &); +QT_END_NAMESPACE namespace QtJson { @@ -83,6 +88,7 @@ private: friend class QJsonValue; friend class QJsonDocument; friend class QJsonParser; + friend QT_PREPEND_NAMESPACE(QDebug) (QT_PREPEND_NAMESPACE(operator<<)) (QT_PREPEND_NAMESPACE(QDebug) dbg, const QJsonObject &o); QJsonObject(Private::Data *data, Private::Object *object); void detach(uint reserve = 0); diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index 5f598d7..1e402de 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -49,7 +49,7 @@ #include <qstringlist.h> #include <qdebug.h> -using namespace QtJson; +namespace QtJson { static const Private::Base emptyArray = { { qToLittleEndian(sizeof(Private::Base)) }, { 0 }, { 0 } }; static const Private::Base emptyObject = { { qToLittleEndian(sizeof(Private::Base)) }, { 0 }, { 0 } }; @@ -401,3 +401,41 @@ void QJsonValue::copyData(char *dest, bool compressed) const break; } } + +} // namespace QtJson + +QT_BEGIN_NAMESPACE + +QDebug operator<<(QDebug dbg, const QtJson::QJsonValue &o) +{ + switch (o.t) { + case QtJson::QJsonValue::Undefined: + dbg.nospace() << "QJsonValue(undefined)"; + break; + case QtJson::QJsonValue::Null: + dbg.nospace() << "QJsonValue(null)"; + break; + case QtJson::QJsonValue::Bool: + dbg.nospace() << "QJsonValue(bool, " << o.toBool() << ")"; + break; + case QtJson::QJsonValue::Number: + dbg.nospace() << "QJsonValue(number, " << o.toNumber() << ")"; + break; + case QtJson::QJsonValue::String: + dbg.nospace() << "QJsonValue(string, " << o.toString() << ")"; + break; + case QtJson::QJsonValue::Array: + dbg.nospace() << "QJsonValue(array, "; + dbg.nospace() << o.toArray(); + dbg.nospace() << ")"; + break; + case QtJson::QJsonValue::Object: + dbg.nospace() << "QJsonValue(object, "; + dbg.nospace() << o.toObject(); + dbg.nospace() << ")"; + break; + } + return dbg.space(); +} + +QT_END_NAMESPACE diff --git a/src/qjsonvalue.h b/src/qjsonvalue.h index 38335fc..9c62f78 100644 --- a/src/qjsonvalue.h +++ b/src/qjsonvalue.h @@ -44,7 +44,10 @@ #include <qjsonglobal.h> +QT_BEGIN_NAMESPACE class QVariant; +Q_JSON_EXPORT QDebug operator<<(QDebug, const QtJson::QJsonValue &); +QT_END_NAMESPACE namespace QtJson { @@ -103,6 +106,8 @@ private: friend class Value; friend class QJsonArray; friend class QJsonObject; + friend QT_PREPEND_NAMESPACE(QDebug) (QT_PREPEND_NAMESPACE(operator<<)) (QT_PREPEND_NAMESPACE(QDebug), const QJsonValue &); + QJsonValue(Private::Data *d, Private::Base *b, const Private::Value& v); void detach(); diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index b678cf3..7f98886 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -44,8 +44,8 @@ using namespace QtJson; -static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent); -static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent); +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent, bool compact); +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent, bool compact); // some code from qutfcodec.cpp, inlined here for performance reasons // to allow fast escaping of strings @@ -175,7 +175,7 @@ static QByteArray escapedString(const QString &s) return ba; } -static void valueToJson(const Private::Base *b, const Private::Value &v, QByteArray &json, int indent) +static void valueToJson(const Private::Base *b, const Private::Value &v, QByteArray &json, int indent, bool compact) { QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; switch (type) { @@ -191,14 +191,14 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr json += '"'; break; case QJsonValue::Array: - json += "[\n"; - arrayContentToJson(static_cast<Private::Array *>(v.objectOrArray(b)), json, indent + 1); + json += compact ? "[" : "[\n"; + arrayContentToJson(static_cast<Private::Array *>(v.objectOrArray(b)), json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); json += "]"; break; case QJsonValue::Object: - json += "{\n"; - objectContentToJson(static_cast<Private::Object *>(v.objectOrArray(b)), json, indent + 1); + json += compact ? "{" : "{\n"; + objectContentToJson(static_cast<Private::Object *>(v.objectOrArray(b)), json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); json += "}"; break; @@ -208,7 +208,7 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr } } -static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent) +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent, bool compact) { if (!a || !a->length) return; @@ -218,19 +218,20 @@ static void arrayContentToJson(const Private::Array *a, QByteArray &json, int in uint i = 0; while (1) { json += indentString; - valueToJson(a, a->at(i), json, indent); + valueToJson(a, a->at(i), json, indent, compact); if (++i == a->length) { - json += '\n'; + if (!compact) + json += '\n'; break; } - json += ",\n"; + json += compact ? "," : ",\n"; } } -static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent) +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent, bool compact) { if (!o || !o->length) return; @@ -244,31 +245,32 @@ static void objectContentToJson(const Private::Object *o, QByteArray &json, int json += '"'; json += escapedString(e->key()); json += "\": "; - valueToJson(o, e->value, json, indent); + valueToJson(o, e->value, json, indent, compact); if (++i == o->length) { - json += '\n'; + if (!compact) + json += '\n'; break; } - json += ",\n"; + json += compact ? "," : ",\n"; } } -void QJsonWriter::objectToJson(const Private::Object *o, QByteArray &json, int indent) +void QJsonWriter::objectToJson(const Private::Object *o, QByteArray &json, int indent, bool compact) { json.reserve(json.size() + (o ? o->size : 16)); - json += "{\n"; - objectContentToJson(o, json, indent + 1); + json += compact ? "{" : "{\n"; + objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); - json += "}\n"; + json += compact ? "}" : "}\n"; } -void QJsonWriter::arrayToJson(const Private::Array *a, QByteArray &json, int indent) +void QJsonWriter::arrayToJson(const Private::Array *a, QByteArray &json, int indent, bool compact) { json.reserve(json.size() + (a ? a->size : 16)); - json += "[\n"; - arrayContentToJson(a, json, indent + 1); + json += compact ? "[" : "[\n"; + arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); - json += "]\n"; + json += compact ? "]" : "]\n"; } diff --git a/src/qjsonwriter_p.h b/src/qjsonwriter_p.h index 1098716..65b7eb6 100644 --- a/src/qjsonwriter_p.h +++ b/src/qjsonwriter_p.h @@ -59,8 +59,8 @@ namespace QtJson class QJsonWriter { public: - static void objectToJson(const Private::Object *o, QByteArray &json, int indent); - static void arrayToJson(const Private::Array *a, QByteArray &json, int indent); + static void objectToJson(const Private::Object *o, QByteArray &json, int indent, bool compact = false); + static void arrayToJson(const Private::Array *a, QByteArray &json, int indent, bool compact = false); }; } diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index bccbcf9..84243d4 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -94,6 +94,7 @@ private Q_SLOTS: void testDuplicateKeys(); void testCompaction(); + void testDebugStream(); }; TestQtJson::TestQtJson(QObject *parent) : QObject(parent) @@ -1153,6 +1154,93 @@ void TestQtJson::testCompaction() QVERIFY(doc.object() == obj); } +void TestQtJson::testDebugStream() +{ + { + // QJsonObject + + QJsonObject object; + QTest::ignoreMessage(QtDebugMsg, "QJsonObject() "); + qDebug() << object; + + object.insert(QLatin1String("foo"), QLatin1String("bar")); + QTest::ignoreMessage(QtDebugMsg, "QJsonObject({\"foo\": \"bar\"}) "); + qDebug() << object; + } + + { + // QJsonArray + + QJsonArray array; + QTest::ignoreMessage(QtDebugMsg, "QJsonArray() "); + qDebug() << array; + + array.append(1); + array.append(QLatin1String("foo")); + QTest::ignoreMessage(QtDebugMsg, "QJsonArray([1,\"foo\"]) "); + qDebug() << array; + } + + { + // QJsonDocument + + QJsonDocument doc; + QTest::ignoreMessage(QtDebugMsg, "QJsonDocument() "); + qDebug() << doc; + + QJsonObject object; + object.insert(QLatin1String("foo"), QLatin1String("bar")); + doc.setObject(object); + QTest::ignoreMessage(QtDebugMsg, "QJsonDocument({\"foo\": \"bar\"}) "); + qDebug() << doc; + + QJsonArray array; + array.append(1); + array.append(QLatin1String("foo")); + QTest::ignoreMessage(QtDebugMsg, "QJsonDocument([1,\"foo\"]) "); + doc.setArray(array); + qDebug() << doc; + } + + { + // QJsonValue + + QJsonValue value; + + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(null) "); + qDebug() << value; + + value = QJsonValue(true); // bool + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(bool, true) "); + qDebug() << value; + + value = QJsonValue((double)4.2); // double + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(number, 4.2) "); + qDebug() << value; + + value = QJsonValue((int)42); // int + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(number, 42) "); + qDebug() << value; + + value = QJsonValue(QLatin1String("foo")); // string + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(string, \"foo\") "); + qDebug() << value; + + QJsonArray array; + array.append(1); + array.append(QLatin1String("foo")); + value = QJsonValue(array); // array + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(array, QJsonArray([1,\"foo\"]) ) "); + qDebug() << value; + + QJsonObject object; + object.insert(QLatin1String("foo"), QLatin1String("bar")); + value = QJsonValue(object); // object + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(object, QJsonObject({\"foo\": \"bar\"}) ) "); + qDebug() << value; + } +} + QTEST_MAIN(TestQtJson) #include "tst_qtjson.moc" |