Hello community,
here is the log from the commit of package libqjson for openSUSE:Factory
checked in at Tue Aug 25 00:16:46 CEST 2009.
--------
--- KDE/libqjson/libqjson.changes 2009-07-23 10:56:53.000000000 +0200
+++ libqjson/libqjson.changes 2009-08-24 17:28:43.000000000 +0200
@@ -1,0 +2,5 @@
+Mon Aug 24 17:14:00 CEST 2009 - flavio@castelli.name
+
+- updated to latest svn version: moved the SerializerRunnable class inside QJson namespace
+
+-------------------------------------------------------------------
calling whatdependson for head-i586
Old:
----
qjson-svn-r1001402.tar.bz2
New:
----
qjson-svn-r1015115.tar.bz2
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Other differences:
------------------
++++++ libqjson.spec ++++++
--- /var/tmp/diff_new_pack.lZUcYl/_old 2009-08-25 00:15:41.000000000 +0200
+++ /var/tmp/diff_new_pack.lZUcYl/_new 2009-08-25 00:15:41.000000000 +0200
@@ -17,12 +17,12 @@
# norootforbuild
-%define svn_rev 1001402
+%define svn_rev 1015115
Name: libqjson
Summary: QJson is a qt-based library that maps JSON data to QVariant objects
Version: 0.6.0
-Release: 1
+Release: 2
License: LGPL
Url: http://qjson.sourceforge.net/
Source: qjson-svn-r%{svn_rev}.tar.bz2
++++++ qjson-svn-r1001402.tar.bz2 -> qjson-svn-r1015115.tar.bz2 ++++++
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/src/json_parser.cc new/qjson/src/json_parser.cc
--- old/qjson/src/json_parser.cc 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/src/json_parser.cc 2009-08-24 17:10:55.000000000 +0200
@@ -593,7 +593,7 @@
case 36:
#line 175 "json_parser.yy"
- { (yyval) = QVariant (QByteArray("")); ;}
+ { (yyval) = QVariant (QString(QLatin1String(""))); ;}
break;
case 37:
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/src/json_parser.yy new/qjson/src/json_parser.yy
--- old/qjson/src/json_parser.yy 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/src/json_parser.yy 2009-08-24 17:10:55.000000000 +0200
@@ -172,7 +172,7 @@
string: QUOTMARKOPEN string_arg QUOTMARKCLOSE { $$ = $2 };
-string_arg: /*empty */ { $$ = QVariant (QByteArray("")); }
+string_arg: /*empty */ { $$ = QVariant (QString(QLatin1String(""))); }
| STRING {
$$ = $1;
};
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/src/serializer.cpp new/qjson/src/serializer.cpp
--- old/qjson/src/serializer.cpp 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/src/serializer.cpp 2009-08-24 17:10:55.000000000 +0200
@@ -67,10 +67,16 @@
}
}
-static QString sanitizeString( const QString& s )
+static QString sanitizeString( QString str )
{
- const QString str = QLatin1String("\"") + s + QLatin1String("\"");
- return str;
+ str.replace( QLatin1String( "\\" ), QLatin1String( "\\\\" ) );
+ str.replace( QLatin1String( "\"" ), QLatin1String( "\\\"" ) );
+ str.replace( QLatin1String( "\b" ), QLatin1String( "\\b" ) );
+ str.replace( QLatin1String( "\f" ), QLatin1String( "\\f" ) );
+ str.replace( QLatin1String( "\n" ), QLatin1String( "\\n" ) );
+ str.replace( QLatin1String( "\r" ), QLatin1String( "\\r" ) );
+ str.replace( QLatin1String( "\t" ), QLatin1String( "\\t" ) );
+ return QString( QLatin1String( "\"%1\"" ) ).arg( str );
}
static QByteArray join( const QList<QByteArray>& list, const QByteArray& sep ) {
@@ -85,38 +91,55 @@
QByteArray Serializer::serialize( const QVariant &v )
{
- if ( !v.isValid() )
- return ""; // no parse error
QByteArray str;
bool error = false;
- // two major cases, either it's an array or ... not
- if ( v.canConvert<QVariantList>() ) {
+ if ( v.isNull() || ! v.isValid() ) { // invalid or null?
+ str = "null";
+ } else if ( v.type() == QVariant::List ) { // variant is a list?
const QVariantList list = v.toList();
QList<QByteArray> values;
Q_FOREACH( const QVariant& v, list )
- values << serialize( v );
+ {
+ QByteArray serializedValue = serialize( v );
+ if ( serializedValue.isNull() ) {
+ error = true;
+ break;
+ }
+ values << serializedValue;
+ }
str = "[ " + join( values, ", " ) + " ]";
- } else {
- // not a list, so it must be an object
- if ( !v.canConvert<QVariantMap>() ) {
- // not a map, so it must be a value
- if ( v.type() == QVariant::String )
- str = sanitizeString( v.toString() ).toUtf8();
- else
- str = v.toByteArray();
- } else {
- const QVariantMap vmap = v.toMap();
- QMapIterator it( vmap );
- str = "{ ";
- QList<QByteArray> pairs;
- while ( it.hasNext() ) {
- it.next();
- pairs << sanitizeString(it.key()).toUtf8() + " : " + serialize(it.value() );
+ } else if ( v.type() == QVariant::Map ) { // variant is a map?
+ const QVariantMap vmap = v.toMap();
+ QMapIterator it( vmap );
+ str = "{ ";
+ QList<QByteArray> pairs;
+ while ( it.hasNext() ) {
+ it.next();
+ QByteArray serializedValue = serialize( it.value() );
+ if ( serializedValue.isNull() ) {
+ error = true;
+ break;
}
- str += join( pairs, ", " );
- str += " }";
+ pairs << sanitizeString( it.key() ).toUtf8() + " : " + serializedValue;
}
+ str += join( pairs, ", " );
+ str += " }";
+ } else if (( v.type() == QVariant::String ) || ( v.type() == QVariant::ByteArray )) { // a string or a byte array?
+ str = sanitizeString( v.toString() ).toUtf8();
+ } else if ( v.type() == QVariant::Double ) { // a double?
+ str = QByteArray::number( v.toDouble() );
+ if( ! str.contains( "." ) && ! str.contains( "e" ) ) {
+ str += ".0";
+ }
+ } else if ( v.type() == QVariant::Bool ) { // boolean value?
+ str = ( v.toBool() ? "true" : "false" );
+ } else if ( v.canConvert<qlonglong>() ) { // any signed number?
+ str = QByteArray::number( v.value<qlonglong>() );
+ } else if ( v.canConvert<qulonglong>() ) { // large unsigned number?
+ str = QByteArray::number( v.value<qulonglong>() );
+ } else {
+ error = true;
}
if ( !error )
return str;
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/src/serializer.h new/qjson/src/serializer.h
--- old/qjson/src/serializer.h 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/src/serializer.h 2009-08-24 17:10:55.000000000 +0200
@@ -28,6 +28,12 @@
class QVariant;
namespace QJson {
+ /**
+ * @brief Main class used for converting QVariant objects to JSON data.
+ *
+ * QVariant objects are converted to a string containing the JSON data.
+ * If QVariant object is empty or not valid a <em>null</em> json object is returned.
+ */
class QJSON_EXPORT Serializer {
public:
Serializer();
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/src/serializerrunnable.h new/qjson/src/serializerrunnable.h
--- old/qjson/src/serializerrunnable.h 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/src/serializerrunnable.h 2009-08-24 17:10:55.000000000 +0200
@@ -30,43 +30,45 @@
class QString;
class QVariant;
-/**
-* @brief Convenience class for converting JSON data to QVariant objects using a dedicated thread
-*/
-class QJSON_EXPORT SerializerRunnable : public QObject, public QRunnable
-{
- Q_OBJECT
- public:
- /**
- * This signal is emitted when the conversion process has been completed
- * @param data contains the JSON data that has to be converted
- * @param parent parent of the object
- **/
- explicit SerializerRunnable(QObject* parent = 0);
- ~SerializerRunnable();
-
- /**
- * Sets the json object to serialize.
- *
- * @param json QVariant containing the json representation to be serialized
- */
- void setJsonObject( const QVariant& json );
-
- /* reimp */ void run();
-
- Q_SIGNALS:
- /**
- * This signal is emitted when the serialization process has been completed
- * @param serialized contains the result of the serialization
- * @param ok if a serialization error occurs ok is set to false, otherwise it's set to true.
- * @param error_msg contains a string explaining the failure reason
- **/
- void parsingFinished(const QByteArray& serialized, bool ok, const QString& error_msg);
-
- private:
- Q_DISABLE_COPY(SerializerRunnable)
- class Private;
- Private* const d;
-};
+namespace QJson {
+ /**
+ * @brief Convenience class for converting JSON data to QVariant objects using a dedicated thread
+ */
+ class QJSON_EXPORT SerializerRunnable : public QObject, public QRunnable
+ {
+ Q_OBJECT
+ public:
+ /**
+ * This signal is emitted when the conversion process has been completed
+ * @param data contains the JSON data that has to be converted
+ * @param parent parent of the object
+ **/
+ explicit SerializerRunnable(QObject* parent = 0);
+ ~SerializerRunnable();
+
+ /**
+ * Sets the json object to serialize.
+ *
+ * @param json QVariant containing the json representation to be serialized
+ */
+ void setJsonObject( const QVariant& json );
+
+ /* reimp */ void run();
+
+ Q_SIGNALS:
+ /**
+ * This signal is emitted when the serialization process has been completed
+ * @param serialized contains the result of the serialization
+ * @param ok if a serialization error occurs ok is set to false, otherwise it's set to true.
+ * @param error_msg contains a string explaining the failure reason
+ **/
+ void parsingFinished(const QByteArray& serialized, bool ok, const QString& error_msg);
+
+ private:
+ Q_DISABLE_COPY(SerializerRunnable)
+ class Private;
+ Private* const d;
+ };
+}
#endif // SERIALIZERRUNNABLE_H
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/tests/CMakeLists.txt new/qjson/tests/CMakeLists.txt
--- old/qjson/tests/CMakeLists.txt 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/tests/CMakeLists.txt 2009-08-24 17:11:01.000000000 +0200
@@ -27,7 +27,8 @@
)
SET( UNIT_TESTS
- testjsondriver
+ testparser
+ testserializer
)
# Build the tests
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/tests/testjsondriver.cpp new/qjson/tests/testjsondriver.cpp
--- old/qjson/tests/testjsondriver.cpp 2009-07-23 10:15:47.000000000 +0200
+++ new/qjson/tests/testjsondriver.cpp 1970-01-01 01:00:00.000000000 +0100
@@ -1,303 +0,0 @@
-/* This file is part of QJson
- *
- * Copyright (C) 2008 Flavio Castelli
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include
-
-#include "parser.h"
-#include "serializer.h"
-
-#include
-
-class TestParser: public QObject
-{
- Q_OBJECT
- private slots:
- void parseNonAsciiString();
- void parseSimpleObject();
- void parseEmptyObject();
- void parseUrl();
- void parseMultipleObject();
-
- void parseSimpleArray();
- void parseInvalidObject();
- void parseMultipleArray();
-
- void testTrueFalseNullValues();
- void testEscapeChars();
- void testNumbers();
-
- void testReadWriteEmptyDocument();
- void testReadWrite();
- void testReadWrite_data();
-};
-
-Q_DECLARE_METATYPE(QVariant)
-
-using namespace QJson;
-
-void TestParser::parseSimpleObject() {
- QByteArray json = "{\"foo\":\"bar\"}";
- QVariantMap map;
- map.insert (QLatin1String("foo"), QLatin1String("bar"));
- QVariant expected(map);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
-void TestParser::parseEmptyObject() {
- QByteArray json = "{}";
- QVariantMap map;
- QVariant expected (map);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
-void TestParser::parseInvalidObject() {
- QByteArray json = "{\"foo\":\"bar\"";
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (!ok);
-}
-
-void TestParser::parseNonAsciiString() {
- QByteArray json = "{\"artist\":\"Queensr\\u00ffche\"}";
- QVariantMap map;
-
- QChar unicode_char (0x00ff);
- QString unicode_string;
- unicode_string.setUnicode(&unicode_char, 1);
- unicode_string = QLatin1String("Queensr") + unicode_string + QLatin1String("che");
-
- map.insert (QLatin1String("artist"), unicode_string);
- QVariant expected (map);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
-void TestParser::parseMultipleObject() {
- //put also some extra spaces inside the json string
- QByteArray json = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}";
- QVariantMap map;
- map.insert (QLatin1String("foo"), QLatin1String("bar"));
- map.insert (QLatin1String("number"), 51.3);
- QVariantList list;
- list.append (QLatin1String("item1"));
- list.append (QLatin1String("123"));
- map.insert (QLatin1String("array"), list);
- QVariant expected (map);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
- QVERIFY (result.toMap().value(QLatin1String("number")).canConvert<float>());
- QVERIFY (result.toMap().value(QLatin1String("array")).canConvert<QVariantList>());
-}
-
-void TestParser::parseUrl(){
- //"http:\/\/www.last.fm\/venue\/8926427"
- QByteArray json = "[\"http:\\/\\/www.last.fm\\/venue\\/8926427\"]";
- QVariantList list;
- list.append (QVariant(QLatin1String("http://www.last.fm/venue/8926427")));
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
- void TestParser::parseSimpleArray() {
- QByteArray json = "[\"foo\",\"bar\"]";
- QVariantList list;
- list.append (QLatin1String("foo"));
- list.append (QLatin1String("bar"));
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
-void TestParser::parseMultipleArray() {
- //put also some extra spaces inside the json string
- QByteArray json = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]";
- QVariantMap map;
- map.insert (QLatin1String("foo"), QLatin1String("bar"));
-
- QVariantList array;
- array.append (QLatin1String("item1"));
- array.append (123);
-
- QVariantList list;
- list.append (map);
- list.append (QLatin1String("number"));
- list.append (QLatin1String("51.3"));
- list.append ((QVariant) array);
-
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-}
-
-void TestParser::testTrueFalseNullValues() {
- QByteArray json = "[true,false, null, {\"foo\" : true}]";
- QVariantList list;
- list.append (QVariant(true));
- list.append (QVariant(false));
- list.append (QVariant());
- QVariantMap map;
- map.insert (QLatin1String("foo"), true);
- list.append (map);
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
- QCOMPARE (result.toList().at(0).toBool(), true);
- QCOMPARE (result.toList().at(1).toBool(), false);
- QVERIFY (result.toList().at(2).isNull());
-}
-
-void TestParser::testEscapeChars() {
- QByteArray json = "[\"\\b \\f \\n \\r \\t \", \" \\\\ \\/ \\\" \", \"http://foo.com\"]";
-
- QVariantList list;
- list.append (QLatin1String("\b \f \n \r \t "));
- list.append (QLatin1String(" \\ / \" "));
- list.append (QLatin1String("http://foo.com"));
-
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result.toList().size(), expected.toList().size() );
- QCOMPARE(result, expected);
-}
-
-void TestParser::testNumbers() {
- QByteArray json = "[1,2.4, -100, -3.4, 1.00004, 01.01, -5e+, 2e,3e+,4.3E,5.4E-]";
- QVariantList list;
- list.append (QVariant(1));
- list.append (QVariant(2.4));
- list.append (QVariant(-100));
- list.append (QVariant(-3.4));
- list.append (QVariant(1.00004));
- list.append (QVariant(1.01));
- list.append (QLatin1String("-5e+"));
- list.append (QLatin1String("2e"));
- list.append (QLatin1String("3e+"));
- list.append (QLatin1String("4.3E"));
- list.append (QLatin1String("5.4E-"));
- QVariant expected (list);
-
- Parser driver;
- bool ok;
- QVariant result = driver.parse (json, &ok);
- QVERIFY (ok);
- QCOMPARE(result, expected);
-
- QVariantList numbers = result.toList();
- QCOMPARE( numbers[0].type(),QVariant::Int );
- QCOMPARE( numbers[1].type(), QVariant::Double );
- QCOMPARE( numbers[2].type(), QVariant::Int );
- QCOMPARE( numbers[3].type(), QVariant::Double );
-}
-
-void TestParser::testReadWriteEmptyDocument()
-{
- QByteArray json = "";
- Parser driver;
- bool ok;
- QVariant result = driver.parse( json, &ok );
- QVERIFY(ok);
- Serializer serializer;
- const QByteArray serialized = serializer.serialize( result );
- QVERIFY( !serialized.isNull() );
- QVERIFY( serialized.isEmpty() );
-}
-
-void TestParser::testReadWrite()
-{
- QFETCH( QByteArray, json );
- Parser driver;
- bool ok;
- QVariant result = driver.parse( json, &ok );
- QVERIFY(ok);
- Serializer serializer;
- const QByteArray serialized = serializer.serialize( result );
-// qWarning() << serialized;
- QVariant writtenThenRead = driver.parse( serialized, &ok );
- QVERIFY(ok);
- QCOMPARE( result, writtenThenRead );
-}
-
-void TestParser::testReadWrite_data()
-{
- QTest::addColumn<QByteArray>( "json" );
-
- // array tests
- QTest::newRow( "empty array" ) << QByteArray("[]");
- QTest::newRow( "basic array" ) << QByteArray("[\"foo\",\"bar\"]");
- QTest::newRow( "single int array" ) << QByteArray("[6]");
- QTest::newRow( "int array" ) << QByteArray("[6,5,6,7]");
- const QByteArray json = "[1,2.4, -100, -3.4, -5e+, 2e,3e+,4.3E,5.4E-]";
- QTest::newRow( QByteArray("array of various numbers") ) << json;
-
- // document tests
- QTest::newRow( "empty object" ) << QByteArray("{}");
- QTest::newRow( "basic document" ) << QByteArray("{\"foo\":\"bar\"}");
- QTest::newRow( "object with ints" ) << QByteArray("{\"foo\":6}");
- const QByteArray json2 = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}";
- QTest::newRow( "complicated document" ) << json2;
-
- // more complex cases
- const QByteArray json3 = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]";
- QTest::newRow( "complicated array" ) << json3;
-}
-
-QTEST_MAIN(TestParser)
-#include "moc_testjsondriver.cxx"
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/tests/testparser.cpp new/qjson/tests/testparser.cpp
--- old/qjson/tests/testparser.cpp 1970-01-01 01:00:00.000000000 +0100
+++ new/qjson/tests/testparser.cpp 2009-08-24 17:11:01.000000000 +0200
@@ -0,0 +1,265 @@
+/* This file is part of QJson
+ *
+ * Copyright (C) 2008 Flavio Castelli
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include
+
+#include "parser.h"
+
+#include
+
+class TestParser: public QObject
+{
+ Q_OBJECT
+ private slots:
+ void parseNonAsciiString();
+ void parseSimpleObject();
+ void parseEmptyObject();
+ void parseEmptyValue();
+ void parseUrl();
+ void parseMultipleObject();
+
+ void parseSimpleArray();
+ void parseInvalidObject();
+ void parseMultipleArray();
+
+ void testTrueFalseNullValues();
+ void testEscapeChars();
+ void testNumbers();
+};
+
+Q_DECLARE_METATYPE(QVariant)
+
+using namespace QJson;
+
+void TestParser::parseSimpleObject() {
+ QByteArray json = "{\"foo\":\"bar\"}";
+ QVariantMap map;
+ map.insert (QLatin1String("foo"), QLatin1String("bar"));
+ QVariant expected(map);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+void TestParser::parseEmptyObject() {
+ QByteArray json = "{}";
+ QVariantMap map;
+ QVariant expected (map);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+void TestParser::parseEmptyValue() {
+ QByteArray json = "{\"value\": \"\"}";
+
+ QVariantMap map;
+ map.insert (QLatin1String("value"), QString(QLatin1String("")));
+ QVariant expected (map);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+ QVERIFY (result.toMap().value(QLatin1String("value")).type() == QVariant::String);
+
+ QString value = result.toMap().value(QLatin1String("value")).toString();
+ QVERIFY (value.isEmpty());
+}
+
+void TestParser::parseInvalidObject() {
+ QByteArray json = "{\"foo\":\"bar\"";
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (!ok);
+}
+
+void TestParser::parseNonAsciiString() {
+ QByteArray json = "{\"artist\":\"Queensr\\u00ffche\"}";
+ QVariantMap map;
+
+ QChar unicode_char (0x00ff);
+ QString unicode_string;
+ unicode_string.setUnicode(&unicode_char, 1);
+ unicode_string = QLatin1String("Queensr") + unicode_string + QLatin1String("che");
+
+ map.insert (QLatin1String("artist"), unicode_string);
+ QVariant expected (map);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+void TestParser::parseMultipleObject() {
+ //put also some extra spaces inside the json string
+ QByteArray json = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}";
+ QVariantMap map;
+ map.insert (QLatin1String("foo"), QLatin1String("bar"));
+ map.insert (QLatin1String("number"), 51.3);
+ QVariantList list;
+ list.append (QLatin1String("item1"));
+ list.append (QLatin1String("123"));
+ map.insert (QLatin1String("array"), list);
+ QVariant expected (map);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+ QVERIFY (result.toMap().value(QLatin1String("number")).canConvert<float>());
+ QVERIFY (result.toMap().value(QLatin1String("array")).canConvert<QVariantList>());
+}
+
+void TestParser::parseUrl(){
+ //"http:\/\/www.last.fm\/venue\/8926427"
+ QByteArray json = "[\"http:\\/\\/www.last.fm\\/venue\\/8926427\"]";
+ QVariantList list;
+ list.append (QVariant(QLatin1String("http://www.last.fm/venue/8926427")));
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+ void TestParser::parseSimpleArray() {
+ QByteArray json = "[\"foo\",\"bar\"]";
+ QVariantList list;
+ list.append (QLatin1String("foo"));
+ list.append (QLatin1String("bar"));
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+void TestParser::parseMultipleArray() {
+ //put also some extra spaces inside the json string
+ QByteArray json = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]";
+ QVariantMap map;
+ map.insert (QLatin1String("foo"), QLatin1String("bar"));
+
+ QVariantList array;
+ array.append (QLatin1String("item1"));
+ array.append (123);
+
+ QVariantList list;
+ list.append (map);
+ list.append (QLatin1String("number"));
+ list.append (QLatin1String("51.3"));
+ list.append ((QVariant) array);
+
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+}
+
+void TestParser::testTrueFalseNullValues() {
+ QByteArray json = "[true,false, null, {\"foo\" : true}]";
+ QVariantList list;
+ list.append (QVariant(true));
+ list.append (QVariant(false));
+ list.append (QVariant());
+ QVariantMap map;
+ map.insert (QLatin1String("foo"), true);
+ list.append (map);
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+ QCOMPARE (result.toList().at(0).toBool(), true);
+ QCOMPARE (result.toList().at(1).toBool(), false);
+ QVERIFY (result.toList().at(2).isNull());
+}
+
+void TestParser::testEscapeChars() {
+ QByteArray json = "[\"\\b \\f \\n \\r \\t \", \" \\\\ \\/ \\\" \", \"http://foo.com\"]";
+
+ QVariantList list;
+ list.append (QLatin1String("\b \f \n \r \t "));
+ list.append (QLatin1String(" \\ / \" "));
+ list.append (QLatin1String("http://foo.com"));
+
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result.toList().size(), expected.toList().size() );
+ QCOMPARE(result, expected);
+}
+
+void TestParser::testNumbers() {
+ QByteArray json = "[1,2.4, -100, -3.4, 1.00004, 01.01, -5e+, 2e,3e+,4.3E,5.4E-]";
+ QVariantList list;
+ list.append (QVariant(1));
+ list.append (QVariant(2.4));
+ list.append (QVariant(-100));
+ list.append (QVariant(-3.4));
+ list.append (QVariant(1.00004));
+ list.append (QVariant(1.01));
+ list.append (QLatin1String("-5e+"));
+ list.append (QLatin1String("2e"));
+ list.append (QLatin1String("3e+"));
+ list.append (QLatin1String("4.3E"));
+ list.append (QLatin1String("5.4E-"));
+ QVariant expected (list);
+
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse (json, &ok);
+ QVERIFY (ok);
+ QCOMPARE(result, expected);
+
+ QVariantList numbers = result.toList();
+ QCOMPARE( numbers[0].type(),QVariant::Int );
+ QCOMPARE( numbers[1].type(), QVariant::Double );
+ QCOMPARE( numbers[2].type(), QVariant::Int );
+ QCOMPARE( numbers[3].type(), QVariant::Double );
+}
+
+QTEST_MAIN(TestParser)
+#include "moc_testparser.cxx"
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/qjson/tests/testserializer.cpp new/qjson/tests/testserializer.cpp
--- old/qjson/tests/testserializer.cpp 1970-01-01 01:00:00.000000000 +0100
+++ new/qjson/tests/testserializer.cpp 2009-08-24 17:11:01.000000000 +0200
@@ -0,0 +1,224 @@
+/* This file is part of QJson
+ *
+ * Copyright (C) 2009 Flavio Castelli
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include
+
+#include "parser.h"
+#include "serializer.h"
+
+#include
+
+class TestSerializer: public QObject
+{
+ Q_OBJECT
+ private slots:
+ void testReadWriteEmptyDocument();
+ void testReadWrite();
+ void testReadWrite_data();
+ void testValueNull();
+ void testValueString();
+ void testValueString_data();
+ void testValueInteger();
+ void testValueInteger_data();
+ void testValueDouble();
+ void testValueDouble_data();
+ void testValueBoolean();
+ void testValueBoolean_data();
+
+ private:
+ void valueTest( const QVariant& value, const QString& expectedRegExp );
+};
+
+Q_DECLARE_METATYPE(QVariant)
+
+using namespace QJson;
+
+void TestSerializer::testReadWriteEmptyDocument()
+{
+ QByteArray json = "";
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse( json, &ok );
+ QVERIFY(ok);
+ QVERIFY( ! result.isValid() );
+ Serializer serializer;
+ const QByteArray serialized = serializer.serialize( result );
+ QVERIFY( !serialized.isNull() );
+ QByteArray expected = "null";
+ QCOMPARE(expected, serialized);
+}
+
+void TestSerializer::testReadWrite()
+{
+ QFETCH( QByteArray, json );
+ Parser parser;
+ bool ok;
+ QVariant result = parser.parse( json, &ok );
+ QVERIFY(ok);
+ Serializer serializer;
+ const QByteArray serialized = serializer.serialize( result );
+ QVariant writtenThenRead = parser.parse( serialized, &ok );
+ QVERIFY(ok);
+ QCOMPARE( result, writtenThenRead );
+}
+
+void TestSerializer::testReadWrite_data()
+{
+ QTest::addColumn<QByteArray>( "json" );
+
+ // array tests
+ QTest::newRow( "empty array" ) << QByteArray("[]");
+ QTest::newRow( "basic array" ) << QByteArray("[\"foo\",\"bar\"]");
+ QTest::newRow( "single int array" ) << QByteArray("[6]");
+ QTest::newRow( "int array" ) << QByteArray("[6,5,6,7]");
+ const QByteArray json = "[1,2.4, -100, -3.4, -5e+, 2e,3e+,4.3E,5.4E-]";
+ QTest::newRow( QByteArray("array of various numbers") ) << json;
+
+ // document tests
+ QTest::newRow( "empty object" ) << QByteArray("{}");
+ QTest::newRow( "basic document" ) << QByteArray("{\"foo\":\"bar\"}");
+ QTest::newRow( "object with ints" ) << QByteArray("{\"foo\":6}");
+ const QByteArray json2 = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}";
+ QTest::newRow( "complicated document" ) << json2;
+
+ // more complex cases
+ const QByteArray json3 = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]";
+ QTest::newRow( "complicated array" ) << json3;
+}
+
+
+void TestSerializer::valueTest( const QVariant& value, const QString& expectedRegExp )
+{
+ Serializer serializer;
+ const QByteArray serialized = serializer.serialize( value );
+ const QString serializedUnicode = QString::fromUtf8( serialized );
+ QRegExp expected( expectedRegExp );
+ QVERIFY( expected.isValid() );
+ QVERIFY2( expected.exactMatch( serializedUnicode ),
+ qPrintable( QString( QLatin1String( "Expected regexp \"%1\" but got \"%2\"." ) )
+ .arg( expectedRegExp ).arg( serializedUnicode ) ) );
+}
+
+void TestSerializer::testValueNull()
+{
+ valueTest( QVariant(), QLatin1String( "\\s*null\\s*" ) );
+ QVariantMap map;
+ map[QLatin1String("value")] = QVariant();
+ valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:\\s*null\\s*\\}\\s*" ) );
+}
+
+void TestSerializer::testValueString()
+{
+ QFETCH( QVariant, value );
+ QFETCH( QString, expected );
+ valueTest( value, expected );
+
+ QVariantMap map;
+ map[QLatin1String("value")] = value;
+ valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) );
+}
+
+void TestSerializer::testValueString_data()
+{
+ QTest::addColumn<QVariant>( "value" );
+ QTest::addColumn<QString>( "expected" );
+
+ QTest::newRow( "empty string" ) << QVariant( QString( QLatin1String( "" ) ) ) << QString( QLatin1String( "\\s*\"\"\\s*" ) );
+ QTest::newRow( "Simple String" ) << QVariant( QString( QLatin1String( "simpleString" ) ) ) << QString( QLatin1String( "\\s*\"simpleString\"\\s*" ) );
+ QTest::newRow( "string with tab" ) << QVariant( QString( QLatin1String( "string\tstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\tstring\"\\s*" ) );
+ QTest::newRow( "string with newline" ) << QVariant( QString( QLatin1String( "string\nstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\nstring\"\\s*" ) );
+ QTest::newRow( "string with bell" ) << QVariant( QString( QLatin1String( "string\bstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\bstring\"\\s*" ) );
+ QTest::newRow( "string with return" ) << QVariant( QString( QLatin1String( "string\rstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\rstring\"\\s*" ) );
+ QTest::newRow( "string with double quote" ) << QVariant( QString( QLatin1String( "string\"string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\"string\"\\s*" ) );
+ QTest::newRow( "string with backslash" ) << QVariant( QString( QLatin1String( "string\\string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\\\\string\"\\s*" ) );
+ QString testStringWithUnicode = QString( QLatin1String( "string" ) ) + QChar( 0x2665 ) + QLatin1String( "string" );
+ QTest::newRow( "string with unicode" ) << QVariant( testStringWithUnicode ) << QLatin1String( "\\s*\"" ) + testStringWithUnicode + QLatin1String( "\"\\s*" );
+}
+
+void TestSerializer::testValueInteger()
+{
+ QFETCH( QVariant, value );
+ QFETCH( QString, expected );
+ valueTest( value, expected );
+
+ QVariantMap map;
+ map[QLatin1String("value")] = value;
+ valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) );
+}
+
+void TestSerializer::testValueInteger_data()
+{
+ QTest::addColumn<QVariant>( "value" );
+ QTest::addColumn<QString>( "expected" );
+
+ QTest::newRow( "int 0" ) << QVariant( static_cast<int>( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) );
+ QTest::newRow( "uint 0" ) << QVariant( static_cast<uint>( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) );
+ QTest::newRow( "int -1" ) << QVariant( static_cast<int>( -1 ) ) << QString( QLatin1String( "\\s*-1\\s*" ) );
+ QTest::newRow( "int 0" ) << QVariant( static_cast<int>( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) );
+ QTest::newRow( "int 2133149800" ) << QVariant( static_cast<int>(2133149800) ) << QString( QLatin1String( "\\s*2133149800\\s*" ) );
+ QTest::newRow( "uint 4133149800" ) << QVariant( static_cast<uint>(4133149800u) ) << QString( QLatin1String( "\\s*4133149800\\s*" ) );
+ QTest::newRow( "uint64 932838457459459" ) << QVariant( Q_UINT64_C(932838457459459) ) << QString( QLatin1String( "\\s*932838457459459\\s*" ) );
+}
+
+void TestSerializer::testValueDouble()
+{
+ QFETCH( QVariant, value );
+ QFETCH( QString, expected );
+ valueTest( value, expected );
+
+ QVariantMap map;
+ map[QLatin1String("value")] = value;
+ valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) );
+}
+
+void TestSerializer::testValueDouble_data()
+{
+ QTest::addColumn<QVariant>( "value" );
+ QTest::addColumn<QString>( "expected" );
+
+ QTest::newRow( "double 0" ) << QVariant( 0.0 ) << QString( QLatin1String( "\\s*0.0\\s*" ) );
+ QTest::newRow( "double -1" ) << QVariant( -1.0 ) << QString( QLatin1String( "\\s*-1.0\\s*" ) );
+ QTest::newRow( "double 1.5E-20" ) << QVariant( 1.5e-20 ) << QString( QLatin1String( "\\s*1.5[Ee]-20\\s*" ) );
+ QTest::newRow( "double -1.5E-20" ) << QVariant( -1.5e-20 ) << QString( QLatin1String( "\\s*-1.5[Ee]-20\\s*" ) );
+ QTest::newRow( "double 2.0E-20" ) << QVariant( 2.0e-20 ) << QString( QLatin1String( "\\s*2(?:.0)?[Ee]-20\\s*" ) );
+}
+
+void TestSerializer::testValueBoolean()
+{
+ QFETCH( QVariant, value );
+ QFETCH( QString, expected );
+ valueTest( value, expected );
+
+ QVariantMap map;
+ map[QLatin1String("value")] = value;
+ valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) );
+}
+
+void TestSerializer::testValueBoolean_data()
+{
+ QTest::addColumn<QVariant>( "value" );
+ QTest::addColumn<QString>( "expected" );
+
+ QTest::newRow( "bool false" ) << QVariant( false ) << QString( QLatin1String( "\\s*false\\s*" ) );
+ QTest::newRow( "bool true" ) << QVariant( true ) << QString( QLatin1String( "\\s*true\\s*" ) );
+}
+
+QTEST_MAIN(TestSerializer)
+#include "moc_testserializer.cxx"
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Remember to have fun...
--
To unsubscribe, e-mail: opensuse-commit+unsubscribe@opensuse.org
For additional commands, e-mail: opensuse-commit+help@opensuse.org