Time for action – the player data JSON serializer

Our next exercise is to create a serializer of the same PlayerInfo structure as we used for the XML exercise, but this time the destination data format is going to be JSON.

Start by creating a PlayerInfoJSON class and give it an interface similar to the one shown in the following code:

class PlayerInfoJSON {
public:
  PlayerInfoJSON(){}
  QByteArray writePlayerInfo(const PlayerInfo &pinfo) const;
};

All that is really required is to implement the writePlayerInfo method. This method will use QJsonDocument::fromVariant() to perform the serialization; thus, what we really have to do is convert our player data to a variant. Let's add a protected method to do that:

QVariant PlayerInfoJSON::toVariant(const PlayerInfo ...

Get Game Programming Using Qt now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.