Posts Tagged “qjson”

I’m really pleased to announce that latest version of QJson on master is working on Symbian. You can find the installation instruction here.

Since I’m not a Symbian developer it has been a little hard for me to achieve that. I would like to thank Antti Luoma for his help.

There are also good news for Windows developers: now building QJson under Windows is easier. Checkout the new installation instruction page.

I hope this will help all the Windows developers who want to use QJson.

Tags: , ,

Comments No Comments »

Just a quick note: I have just moved QJson source code to this git repository hosted by gitorious.

I’ll keep the code on KDE’s svn synchronized with the git repository.

Tags: , ,

Comments 2 Comments »

Some 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 JSON

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));

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 QObject

Suppose 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 release

These changes have been included inside the new release of QJson: 0.7.0.

Packages for openSUSE are building right now.

Tags: , ,

Comments 22 Comments »

I 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: , ,

Comments 4 Comments »

Recently 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: , , ,

Comments 4 Comments »

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:

  • the API has been cleaned, now it can be considered stable
  • unicode support has been completely rewritten
  • it’s now possible to convert QVariant objects into JSON ones

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.
openSUSE packages has been moved from my home repo to KDE:Qt one.

One last note, if you have problems with QJson please contact me using the qjson-devel mailing list. You can subscribe here.

Tags: , ,

Comments No Comments »

Now 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:

  • the advantages brought by QJson.
  • the usage of QJson.
  • some real programs using QJson.

See you soon!

gcds_summit_badge.serendipityThumb

Tags: , ,

Comments No Comments »

During the last weekend I hacked a bit on rockmarble and I added a new feature: retrieve all the events happening in a certain city.

Read the rest of this entry »

Tags: , , , , ,

Comments 2 Comments »

During 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

Read the rest of this entry »

Tags: , , , , ,

Comments 7 Comments »

In order to realize a project of mine I started looking for a Qt library for mapping JSON data to Qt objects.

I came over a couple of solutions but none of them made me happy. So in the last weekend I wrote my own library : QJson
Read the rest of this entry »

Tags: , , , ,

Comments 32 Comments »