Add json/savegame example.
There wasn't any example documentation besides json.html, which doesn't actually describe usage of the various QJson* classes. This also makes each QJson* class page link back to json.html. Change-Id: If5ad6493d2728df0cec7bdbbc5790f0b755f816c Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: Jerome Pasion <jerome.pasion@digia.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
parent
4e04302672
commit
c7c3a78075
@ -7,6 +7,7 @@ SUBDIRS = \
|
||||
gestures \
|
||||
gui \
|
||||
ipc \
|
||||
json \
|
||||
network \
|
||||
opengl \
|
||||
qpa \
|
||||
|
2
examples/json/json.pro
Normal file
2
examples/json/json.pro
Normal file
@ -0,0 +1,2 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = savegame
|
101
examples/json/savegame/character.cpp
Normal file
101
examples/json/savegame/character.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "character.h"
|
||||
|
||||
Character::Character() :
|
||||
mLevel(0),
|
||||
mClassType(Warrior) {
|
||||
}
|
||||
|
||||
Character::Character(const QString &name, int level, Character::ClassType classType) :
|
||||
mName(name),
|
||||
mLevel(level),
|
||||
mClassType(classType)
|
||||
{
|
||||
}
|
||||
|
||||
QString Character::name() const
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
|
||||
void Character::setName(const QString &name)
|
||||
{
|
||||
mName = name;
|
||||
}
|
||||
|
||||
int Character::level() const
|
||||
{
|
||||
return mLevel;
|
||||
}
|
||||
|
||||
void Character::setLevel(int level)
|
||||
{
|
||||
mLevel = level;
|
||||
}
|
||||
|
||||
Character::ClassType Character::classType() const
|
||||
{
|
||||
return mClassType;
|
||||
}
|
||||
|
||||
void Character::setClassType(Character::ClassType classType)
|
||||
{
|
||||
mClassType = classType;
|
||||
}
|
||||
|
||||
//! [0]
|
||||
void Character::read(const QJsonObject &json)
|
||||
{
|
||||
mName = json["name"].toString();
|
||||
mLevel = json["level"].toDouble();
|
||||
mClassType = ClassType(qRound(json["classType"].toDouble()));
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Character::write(QJsonObject &json) const
|
||||
{
|
||||
json["name"] = mName;
|
||||
json["level"] = mLevel;
|
||||
json["classType"] = mClassType;
|
||||
}
|
||||
//! [1]
|
76
examples/json/savegame/character.h
Normal file
76
examples/json/savegame/character.h
Normal file
@ -0,0 +1,76 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CHARACTER_H
|
||||
#define CHARACTER_H
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QString>
|
||||
|
||||
//! [0]
|
||||
class Character
|
||||
{
|
||||
public:
|
||||
enum ClassType {
|
||||
Warrior, Mage, Archer
|
||||
};
|
||||
|
||||
Character();
|
||||
Character(const QString &name, int level, ClassType classType);
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
int level() const;
|
||||
void setLevel(int level);
|
||||
|
||||
ClassType classType() const;
|
||||
void setClassType(ClassType classType);
|
||||
|
||||
void read(const QJsonObject &json);
|
||||
void write(QJsonObject &json) const;
|
||||
private:
|
||||
QString mName;
|
||||
int mLevel;
|
||||
ClassType mClassType;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // CHARACTER_H
|
184
examples/json/savegame/doc/src/savegame.qdoc
Normal file
184
examples/json/savegame/doc/src/savegame.qdoc
Normal file
@ -0,0 +1,184 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the documentation of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:FDL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Digia. For licensing terms and
|
||||
** conditions see http://qt.digia.com/licensing. For further information
|
||||
** use the contact form at http://qt.digia.com/contact-us.
|
||||
**
|
||||
** GNU Free Documentation License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Free
|
||||
** Documentation License version 1.3 as published by the Free Software
|
||||
** Foundation and appearing in the file included in the packaging of
|
||||
** this file. Please review the following information to ensure
|
||||
** the GNU Free Documentation License version 1.3 requirements
|
||||
** will be met: http://www.gnu.org/copyleft/fdl.html.
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
/*!
|
||||
\example savegame
|
||||
\title JSON Save Game Example
|
||||
|
||||
\brief The JSON Save Game example demonstrates how to save and load a
|
||||
small game using QJsonDocument, QJsonObject and QJsonArray.
|
||||
|
||||
Many games provide save functionality, so that the player's progress through
|
||||
the game can be saved and loaded at a later time. The process of saving a
|
||||
game generally involves serializing each game object's member variables
|
||||
to a file. Many formats can be used for this purpose, one of which is JSON.
|
||||
With QJsonDocument, you also have the ability to serialize a document in a
|
||||
binary format, which is great if you don't want the save file to be
|
||||
readable, or if you need to keep the file size down.
|
||||
|
||||
In this example, we'll demonstrate how to save and load a simple game to
|
||||
and from JSON and binary formats.
|
||||
|
||||
\section1 The Character class
|
||||
|
||||
The Character class represents a non-player character (NPC) in our game, and
|
||||
stores the player's name, level, and class type.
|
||||
|
||||
It provides read() and write() functions to serialise its member variables.
|
||||
|
||||
\snippet savegame/character.h 0
|
||||
|
||||
Of particular interest to us are the read and write function
|
||||
implementations:
|
||||
|
||||
\snippet savegame/character.cpp 0
|
||||
|
||||
In the read() function, we assign Character's members values from the
|
||||
QJsonObject argument. You can use either \l QJsonObject::operator[]() or
|
||||
QJsonObject::value() to access values within the JSON object; both are
|
||||
const functions and return QJsonValue::Undefined if the key is invalid. We
|
||||
could check if the keys are valid before attempting to read them with
|
||||
QJsonObject::contains(), but we assume that they are.
|
||||
|
||||
\snippet savegame/character.cpp 1
|
||||
|
||||
In the write() function, we do the reverse of the read() function; assign
|
||||
values from the Character object to the JSON object. As with accessing
|
||||
values, there are two ways to set values on a QJsonObject:
|
||||
\l QJsonObject::operator[]() and QJsonObject::insert(). Both will override
|
||||
any existing value at the given key.
|
||||
|
||||
Next up is the Level class:
|
||||
|
||||
\snippet savegame/level.h 0
|
||||
|
||||
We want to have several levels in our game, each with several NPCs, so we
|
||||
keep a QList of Character objects. We also provide the familiar read() and
|
||||
write() functions.
|
||||
|
||||
\snippet savegame/level.cpp 0
|
||||
|
||||
Containers can be written and read to and from JSON using QJsonArray. In our
|
||||
case, we construct a QJsonArray from the value associated with the key
|
||||
\c "npcs". Then, for each QJsonValue element in the array, we call
|
||||
toObject() to get the Character's JSON object. The Character object can then
|
||||
read their JSON and be appended to our NPC list.
|
||||
|
||||
\note \l{Container Classes}{Associate containers} can be written by storing
|
||||
the key in each value object (if it's not already). With this approach, the
|
||||
container is stored as a regular array of objects, but the index of each
|
||||
element is used as the key to construct the container when reading it back
|
||||
in.
|
||||
|
||||
\snippet savegame/level.cpp 1
|
||||
|
||||
Again, the write() function is similar to the read() function, except
|
||||
reversed.
|
||||
|
||||
Having established the Character and Level classes, we can move on to
|
||||
the Game class:
|
||||
|
||||
\snippet savegame/game.h 0
|
||||
|
||||
First of all, we define the \c SaveFormat enum. This will allow us to
|
||||
specify the format in which the game should be saved: \c Json or \c Binary.
|
||||
|
||||
Next, we provide accessors for the player and levels. We then expose three
|
||||
functions: newGame(), saveGame() and loadGame().
|
||||
|
||||
The read() and write() functions are used by saveGame() and loadGame().
|
||||
|
||||
\snippet savegame/game.cpp 0
|
||||
|
||||
To setup a new game, we create the player and populate the levels and their
|
||||
NPCs.
|
||||
|
||||
\snippet savegame/game.cpp 1
|
||||
|
||||
The first thing we do in the read() function is tell the player to read
|
||||
itself. We then clear the levels list so that calling loadGame() on the same
|
||||
Game object twice doesn't result in old levels hanging around.
|
||||
|
||||
We then populate the level list by reading each Level from a QJsonArray.
|
||||
|
||||
\snippet savegame/game.cpp 2
|
||||
|
||||
We write the game to JSON similarly to how we write Level.
|
||||
|
||||
\snippet savegame/game.cpp 3
|
||||
|
||||
When loading a saved game in loadGame(), the first thing we do is open the
|
||||
save file based on which format it was saved to; \c "save.json" for JSON,
|
||||
and \c "save.dat" for binary. We print a warning and return \c false if the
|
||||
file couldn't be opened.
|
||||
|
||||
Since QJsonDocument's \l{QJsonDocument::fromJson()}{fromJson()} and
|
||||
\l{QJsonDocument::fromBinaryData()}{fromBinaryData()} functions both take a
|
||||
QByteArray, we can read the entire contents of the save file into one,
|
||||
regardless of the save format.
|
||||
|
||||
After constructing the QJsonDocument, we instruct the Game object to read
|
||||
itself and then return \c true to indicate success.
|
||||
|
||||
\snippet savegame/game.cpp 4
|
||||
|
||||
Not surprisingly, saveGame() looks very much like loadGame(). We determine
|
||||
the file extension based on the format, print a warning and return \c false
|
||||
if the opening of the file fails. We then write the Game object to a
|
||||
QJsonDocument, and call either QJsonDocument::toJson() or to
|
||||
QJsonDocument::toBinaryData() to save the game, depending on which format
|
||||
was specified.
|
||||
|
||||
We are now ready to enter main():
|
||||
|
||||
\snippet savegame/main.cpp 0
|
||||
|
||||
Since we're only interested in demonstrating \e serialization of a game with
|
||||
JSON, our game is not actually playable. Therefore, we only need
|
||||
QCoreApplication and have no event loop. We create our game and assume that
|
||||
the player had a great time and made lots of progress, altering the internal
|
||||
state of our Character, Level and Game objects.
|
||||
|
||||
\snippet savegame/main.cpp 1
|
||||
|
||||
When the player has finished, we save their game. For demonstration
|
||||
purposes, we serialize to both JSON and binary. You can examine the contents
|
||||
of the files in the same directory as the executable, although the binary
|
||||
save file will contain some garbage characters (which is normal).
|
||||
|
||||
To show that the saved files can be loaded again, we call loadGame() for
|
||||
each format, returning \c 1 on failure. Assuming everything went well, we
|
||||
return \c 0 to indicate success.
|
||||
|
||||
That concludes our example. As you can see, serialization with Qt's JSON
|
||||
classes is very simple and convenient. The advantages of using QJsonDocument
|
||||
and friends over QDataStream, for example, is that you not only get
|
||||
human-readable JSON files, but you also have the option to use a binary
|
||||
format if it's required, \e without rewriting any code.
|
||||
|
||||
\sa {JSON Support in Qt}, {Data Storage}
|
||||
*/
|
164
examples/json/savegame/game.cpp
Normal file
164
examples/json/savegame/game.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "game.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
|
||||
Game::Game()
|
||||
{
|
||||
}
|
||||
|
||||
const Character &Game::player() const
|
||||
{
|
||||
return mPlayer;
|
||||
}
|
||||
|
||||
const QList<Level> &Game::levels() const {
|
||||
return mLevels;
|
||||
}
|
||||
|
||||
//! [0]
|
||||
void Game::newGame() {
|
||||
mPlayer = Character();
|
||||
mPlayer.setName(QStringLiteral("Hero"));
|
||||
mPlayer.setClassType(Character::Archer);
|
||||
mPlayer.setLevel(15);
|
||||
|
||||
mLevels.clear();
|
||||
|
||||
Level village;
|
||||
QList<Character> villageNpcs;
|
||||
villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"), 10, Character::Warrior));
|
||||
villageNpcs.append(Character(QStringLiteral("Terry the Trader"), 10, Character::Warrior));
|
||||
village.setNpcs(villageNpcs);
|
||||
mLevels.append(village);
|
||||
|
||||
Level dungeon;
|
||||
QList<Character> dungeonNpcs;
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"), 20, Character::Mage));
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #1"), 5, Character::Warrior));
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #2"), 5, Character::Warrior));
|
||||
dungeon.setNpcs(dungeonNpcs);
|
||||
mLevels.append(dungeon);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [3]
|
||||
bool Game::loadGame(Game::SaveFormat saveFormat)
|
||||
{
|
||||
QFile loadFile(saveFormat == Json
|
||||
? QStringLiteral("save.json")
|
||||
: QStringLiteral("save.dat"));
|
||||
|
||||
if (!loadFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open save file.");
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray saveData = loadFile.readAll();
|
||||
|
||||
QJsonDocument loadDoc(saveFormat == Json
|
||||
? QJsonDocument::fromJson(saveData)
|
||||
: QJsonDocument::fromBinaryData(saveData));
|
||||
|
||||
read(loadDoc.object());
|
||||
|
||||
return true;
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
bool Game::saveGame(Game::SaveFormat saveFormat) const
|
||||
{
|
||||
QFile saveFile(saveFormat == Json
|
||||
? QStringLiteral("save.json")
|
||||
: QStringLiteral("save.dat"));
|
||||
|
||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||
qWarning("Couldn't open save file.");
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonObject gameObject;
|
||||
write(gameObject);
|
||||
QJsonDocument saveDoc(gameObject);
|
||||
saveFile.write(saveFormat == Json
|
||||
? saveDoc.toJson()
|
||||
: saveDoc.toBinaryData());
|
||||
|
||||
return true;
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [1]
|
||||
void Game::read(const QJsonObject &json)
|
||||
{
|
||||
mPlayer.read(json["player"].toObject());
|
||||
|
||||
mLevels.clear();
|
||||
QJsonArray levelArray = json["levels"].toArray();
|
||||
for (int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex) {
|
||||
QJsonObject levelObject = levelArray[levelIndex].toObject();
|
||||
Level level;
|
||||
level.read(levelObject);
|
||||
mLevels.append(level);
|
||||
}
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Game::write(QJsonObject &json) const
|
||||
{
|
||||
QJsonObject playerObject;
|
||||
mPlayer.write(playerObject);
|
||||
json["player"] = playerObject;
|
||||
|
||||
QJsonArray levelArray;
|
||||
foreach (const Level level, mLevels) {
|
||||
QJsonObject levelObject;
|
||||
level.write(levelObject);
|
||||
levelArray.append(levelObject);
|
||||
}
|
||||
json["levels"] = levelArray;
|
||||
}
|
||||
//! [2]
|
75
examples/json/savegame/game.h
Normal file
75
examples/json/savegame/game.h
Normal file
@ -0,0 +1,75 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QList>
|
||||
|
||||
#include "character.h"
|
||||
#include "level.h"
|
||||
|
||||
//! [0]
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
Game();
|
||||
|
||||
enum SaveFormat {
|
||||
Json, Binary
|
||||
};
|
||||
|
||||
const Character &player() const;
|
||||
const QList<Level> &levels() const;
|
||||
|
||||
void newGame();
|
||||
bool loadGame(SaveFormat saveFormat);
|
||||
bool saveGame(SaveFormat saveFormat) const;
|
||||
|
||||
void read(const QJsonObject &json);
|
||||
void write(QJsonObject &json) const;
|
||||
private:
|
||||
Character mPlayer;
|
||||
QList<Level> mLevels;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // GAME_H
|
83
examples/json/savegame/level.cpp
Normal file
83
examples/json/savegame/level.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "level.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
|
||||
Level::Level() {
|
||||
}
|
||||
|
||||
const QList<Character> &Level::npcs() const
|
||||
{
|
||||
return mNpcs;
|
||||
}
|
||||
|
||||
void Level::setNpcs(const QList<Character> &npcs)
|
||||
{
|
||||
mNpcs = npcs;
|
||||
}
|
||||
|
||||
//! [0]
|
||||
void Level::read(const QJsonObject &json)
|
||||
{
|
||||
mNpcs.clear();
|
||||
QJsonArray npcArray = json["npcs"].toArray();
|
||||
for (int npcIndex = 0; npcIndex < npcArray.size(); ++npcIndex) {
|
||||
QJsonObject npcObject = npcArray[npcIndex].toObject();
|
||||
Character npc;
|
||||
npc.read(npcObject);
|
||||
mNpcs.append(npc);
|
||||
}
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Level::write(QJsonObject &json) const
|
||||
{
|
||||
QJsonArray npcArray;
|
||||
foreach (const Character npc, mNpcs) {
|
||||
QJsonObject npcObject;
|
||||
npc.write(npcObject);
|
||||
npcArray.append(npcObject);
|
||||
}
|
||||
json["npcs"] = npcArray;
|
||||
}
|
||||
//! [1]
|
65
examples/json/savegame/level.h
Normal file
65
examples/json/savegame/level.h
Normal file
@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef LEVEL_H
|
||||
#define LEVEL_H
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QList>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
//! [0]
|
||||
class Level
|
||||
{
|
||||
public:
|
||||
Level();
|
||||
|
||||
const QList<Character> &npcs() const;
|
||||
void setNpcs(const QList<Character> &npcs);
|
||||
|
||||
void read(const QJsonObject &json);
|
||||
void write(QJsonObject &json) const;
|
||||
private:
|
||||
QList<Character> mNpcs;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // LEVEL_H
|
70
examples/json/savegame/main.cpp
Normal file
70
examples/json/savegame/main.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
||||
** of its contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "game.h"
|
||||
//! [0]
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
Game game;
|
||||
game.newGame();
|
||||
// Game is played; changes are made...
|
||||
//! [0]
|
||||
//! [1]
|
||||
if (!game.saveGame(Game::Json))
|
||||
return 1;
|
||||
|
||||
if (!game.saveGame(Game::Binary))
|
||||
return 1;
|
||||
|
||||
Game fromJsonGame;
|
||||
if (!fromJsonGame.loadGame(Game::Json))
|
||||
return 1;
|
||||
|
||||
Game fromBinaryGame;
|
||||
if (!fromBinaryGame.loadGame(Game::Binary))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [1]
|
22
examples/json/savegame/savegame.pro
Normal file
22
examples/json/savegame/savegame.pro
Normal file
@ -0,0 +1,22 @@
|
||||
QT += core
|
||||
QT -= gui
|
||||
|
||||
TARGET = savegame
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/json/savegame
|
||||
INSTALLS += target
|
||||
|
||||
SOURCES += main.cpp \
|
||||
character.cpp \
|
||||
game.cpp \
|
||||
level.cpp
|
||||
|
||||
HEADERS += \
|
||||
character.h \
|
||||
game.h \
|
||||
level.h
|
@ -36,6 +36,7 @@ exampledirs += \
|
||||
../ \
|
||||
snippets \
|
||||
../../../examples/threads/ \
|
||||
../../../examples/tools/
|
||||
../../../examples/tools/ \
|
||||
../../../examples/json/
|
||||
|
||||
imagedirs += images
|
||||
|
@ -369,4 +369,6 @@
|
||||
\li The items (T)
|
||||
\endlist
|
||||
\endtable
|
||||
|
||||
\sa {JSON Support in Qt}
|
||||
*/
|
||||
|
@ -99,6 +99,8 @@
|
||||
A valid JSON document is either an array or an object, so a document always starts
|
||||
with a square or curly bracket.
|
||||
|
||||
\sa {JSON Save Game Example}
|
||||
|
||||
|
||||
\section1 The JSON Classes
|
||||
|
||||
|
@ -71,6 +71,8 @@ QT_BEGIN_NAMESPACE
|
||||
it has been created from as long as it is not being modified.
|
||||
|
||||
You can convert the array to and from text based JSON through QJsonDocument.
|
||||
|
||||
\sa {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
@ -76,6 +76,8 @@ QT_BEGIN_NAMESPACE
|
||||
|
||||
A document can also be created from a stored binary representation using fromBinaryData() or
|
||||
fromRawData().
|
||||
|
||||
\sa {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
@ -70,6 +70,8 @@ QT_BEGIN_NAMESPACE
|
||||
it has been created from as long as it is not being modified.
|
||||
|
||||
You can convert the object to and from text based JSON through QJsonDocument.
|
||||
|
||||
\sa {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*!
|
||||
@ -604,7 +606,7 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const
|
||||
Multiple iterators can be used on the same object. Existing iterators will however
|
||||
become dangling once the object gets modified.
|
||||
|
||||
\sa QJsonObject::const_iterator
|
||||
\sa QJsonObject::const_iterator, {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*! \typedef QJsonObject::iterator::difference_type
|
||||
@ -799,7 +801,7 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const
|
||||
Multiple iterators can be used on the same object. Existing iterators
|
||||
will however become dangling if the object gets modified.
|
||||
|
||||
\sa QJsonObject::iterator
|
||||
\sa QJsonObject::iterator, {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*! \typedef QJsonObject::const_iterator::difference_type
|
||||
|
@ -86,6 +86,8 @@ QT_BEGIN_NAMESPACE
|
||||
\since 5.0
|
||||
|
||||
\brief The QJsonParseError class is used to report errors during JSON parsing.
|
||||
|
||||
\sa {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
@ -81,6 +81,8 @@ QT_BEGIN_NAMESPACE
|
||||
Values are strictly typed internally and contrary to QVariant will not attempt to do any implicit type
|
||||
conversions. This implies that converting to a type that is not stored in the value will return a default
|
||||
constructed return value.
|
||||
|
||||
\sa {JSON Support in Qt}, {JSON Save Game Example}
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
Loading…
Reference in New Issue
Block a user