Author ArchiveSome days ago I introduced the possibility to serialize a QObject instance to JSON. Today I’m going to show you the opposite operation: initializing a QObject using a JSON object. I refactored a bit my latest changes: I created a new class called QObjectHelper that provides the methods required to convert a QObject instance to a QVariantMap and vice-versa. This class can be used in conjunction with the Serializer and Parser classes to serialize and deserialize QObject instances to and from JSON. Let me show a quick example, suppose the declaration of Person class looks like this:
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
Q_PROPERTY(Gender gender READ gender WRITE setGender)
Q_PROPERTY(QDate dob READ dob WRITE setDob)
Q_ENUMS(Gender)
public:
Person(QObject* parent = 0);
~Person();
QString name() const;
void setName(const QString& name);
int phoneNumber() const;
void setPhoneNumber(const int phoneNumber);
enum Gender {Male, Female};
void setGender(Gender gender);
Gender gender() const;
QDate dob() const;
void setDob(const QDate& dob);
private:
QString m_name;
int m_phoneNumber;
Gender m_gender;
QDate m_dob;
};
From QObject to JSONThe following code will serialize an instance of Person to JSON :
Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12));
QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
Serializer serializer;
qDebug() << serializer.serialize( variant);
The generated output will be:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
From JSON to QObjectSuppose you have the following JSON data stored into a QString:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
The following code will initialize an already allocated instance of Person using the JSON values: Parser parser; QVariant variant = parser.parse(json); Person person; QObjectHelper::qvariant2qobject(variant.toMap(), &person); A new releaseThese changes have been included inside the new release of QJson: 0.7.0. Packages for openSUSE are building right now. Tags: KDE, qjson, Qt
Nov
30
2009
QJson: easier serialization of QObject instances to JSONPosted by Flavio in C++, KDE, Qt, UncategorizedI have just committed into trunk a couple of changes that make easier to serialize a QObject instance to JSON. This solution relies on the awesome Qt’s property system. Suppose the declaration of Person class looks like this:
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
Q_PROPERTY(Gender gender READ gender WRITE setGender)
Q_PROPERTY(QDate dob READ dob WRITE setDob)
Q_ENUMS(Gender)
public:
Person(QObject* parent = 0);
~Person();
QString name() const;
void setName(const QString& name);
int phoneNumber() const;
void setPhoneNumber(const int phoneNumber);
enum Gender {Male, Female};
void setGender(Gender gender);
Gender gender() const;
QDate dob() const;
void setDob(const QDate& dob);
private:
QString m_name;
int m_phoneNumber;
Gender m_gender;
QDate m_dob;
};
The following code will serialize an instance of Person to JSON:
Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12));
Serializer serializer;
qDebug() << serializer.serialize( &person);
The generated output will be:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
I hope you will find this new feature useful. I'm also considering to create a similar method inside the Parser class. As usual suggestions are welcome. Tags: KDE, qjson, QtSome weeks have passed since the announcement of kaveau. I’m really proud and happy about this project because I received a lot of positive feedback messages and it has been chosen as one of the best Hackweek’s projects. In the meantime I kept working on kaveau, so let me show you what has changed:
The switch to rsyncPreviously kaveau used rdiff-backup as backup back-end. rdiff-backup is a great program but unfortunately it relies on the outdated librsync library. The latest release of librsync is dated 2004. It has a couple of serious bugs still open and, while rsync has reached version three, this library is still stuck at version one. These are the reasons of the switch from rdiff-backup to rsync. This choice breaks the compatibility with the previous backups but it introduces a lot of advantages. Backup directory structureOn the backup device everything is saved under the kaveau/hostname/username path. The directory will have a similar structure: drwxr-xr-x 3 flavio users 4096 2009-09-12 18:50 2009-09-12T18:50:19 drwxr-xr-x 3 flavio users 4096 2009-09-14 23:07 2009-09-14T23:07:46 drwxr-xr-x 3 flavio users 4096 2009-09-14 23:30 2009-09-14T23:30:36 lrwxrwxrwx 1 flavio users 19 2009-09-14 23:30 current -> 2009-09-14T23:30:36 As you can see there’s one directory per backup, plus a symlink called current pointing to the latest backup. Old backup deletionNowadays big external storage devices are pretty cheap, but it’s always good to save some disk space.
Thanks to hard links’ magic, old backups can be deleted without causing damages to the other ones. Plans for the futureBefore starting to work on the restore user interface I will spend some time figuring out how to add support for network devices. A lot of users requested this feature, hence I want to make them happy I’m planning to use avahi to discover network shares (nfs, samba) or network machines running ssh and use them as backup devices. Honestly, I want to achieve something similar to Apple’s time capsule. As usual, feedback messages are really appreciated. Tags: kaveau, KDE, QtRecently lots of people asked me how to build QJson under Windows. Most of them reported build/link errors, so I decided to try personally. The good news is that QJson can be successfully built under Window, I can show you proof I have written the build instructions on QJson website: just take a look here. One last note: if you have problems with QJson please subscribe to the developer mailing list and post a message. Tags: JSON, KDE, qjson, QtDuring the last week I had the possibility to work on anything I wanted, Novell’s hackweek is so cool I decided to dedicate myself to an idea that has been obsessing me since a long time. Last December my brand new hard disk suddenly died, making impossible to recover anything. Fortunately I had just synchronized the most important documents between my workstation and my laptop, so I didn’t lose anything important. This incident make me realize that I should perform backups regularly and I immediately started looking for a good solution. Personally I think that doing backups is pretty boring so I wanted something damned easy to setup and use. Something that once configured runs in the background and does the dirty job without bothering you. Let’s face the truth: I wanted Apple’s Time Machine for KDE. After some searches I realized that nothing was fitting my requirements and I decided to create something new: kaveau. What is kaveau?Kaveau is a backup program that makes backup easy for the average user. As you will see while coding/planning kaveau I made some assumptions and so only few things are configurable. I really think that sometimes “less is more”. What does kaveau?Current features:
More enhancements are coming… What technologies does it use?Backups are performed using rdiff-backup because it’s damned easy to use, well tested (it’s used also in production environments) and packaged by all distributions. The awesome solid library is used for interacting with the external disks is super easy. Status of kaveauI have been working on kaveau just for five days, so there’s still a lot of work to do. A screenshot tour will give you the right idea of its status.
Right now the code is available on this git repository but I don’t recommend you to try it (unless you want to find and fix bugs I would really appreciate:
Gran Canaria Desktop Summit has been great and really productive. I had the pleasure to meet people interested in QJson, chat with them but also hack with them. In fact we hacked a lot, doing lots of changes to QJson:
So it’s with a great pleasure that I announce the release of QJson 0.6.0. Beware, since the API has been changed your application will probably break. I’m really sorry about that, but I guarantee it won’t happen in the future (as I said both API and ABI interfaces can now be considered stable). QJson web site has been updated, reflecting all the changes made to the library. One last note, if you have problems with QJson please contact me using the qjson-devel mailing list. You can subscribe here. Tags: JSON, qjson, QtNow that I have booked both the flights and the hotel it’s official: I’ll attend the Gran Canaria Desktop Summit. On Thursday 9th July I’ll give a BoF about QJson. During the talk I will show:
See you soon!
Apr
07
2009
rockmarble: see who is going to rock in your townPosted by Flavio in C++, KDE, Programming, Qt
Apr
02
2009
rockmarble: how to follow your favourite artists tour with MarblePosted by Flavio in C++, KDE, ProgrammingDuring the last weekend I wanted to have some fun with QJson. So I came out with this idea: retrieve from last.fm the tour dates of my favourite artists and display the locations using Marble. After some hacking I created this small application: rockmarble… Tags: C++, KDE, last.fm, marble, Programming, qjson |
.











Entries (RSS)