O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Implementing a JSON parser

Let's extend the PlayerInfoJSON class and equip it with a playerInfoFromJson() method:

PlayerInfo PlayerInfoJson::playerInfoFromJson(const QByteArray &ba) {
    QJsonDocument doc = QJsonDocument::fromJson(ba);
    if(!doc.isArray()) {
        return PlayerInfo();
    }
    QJsonArray array = doc.array();
    PlayerInfo pinfo;
    for(const QJsonValue &value: array) {
        pinfo.players << playerFromJson(value.toObject());
    }
    return pinfo;
}

First, we read the document and check whether it is valid and holds the expected array. Upon failure, an empty structure is returned; otherwise, we iterate over the received array and convert each of its elements to an object. Similar to the serialization example, we create a helper function for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required