summaryrefslogtreecommitdiffstats
path: root/examples/corelib/serialization/savegame
diff options
context:
space:
mode:
Diffstat (limited to 'examples/corelib/serialization/savegame')
-rw-r--r--examples/corelib/serialization/savegame/CMakeLists.txt30
-rw-r--r--examples/corelib/serialization/savegame/character.cpp53
-rw-r--r--examples/corelib/serialization/savegame/character.h19
-rw-r--r--examples/corelib/serialization/savegame/doc/src/savegame.qdoc154
-rw-r--r--examples/corelib/serialization/savegame/game.cpp130
-rw-r--r--examples/corelib/serialization/savegame/game.h15
-rw-r--r--examples/corelib/serialization/savegame/level.cpp55
-rw-r--r--examples/corelib/serialization/savegame/level.h13
-rw-r--r--examples/corelib/serialization/savegame/main.cpp27
9 files changed, 275 insertions, 221 deletions
diff --git a/examples/corelib/serialization/savegame/CMakeLists.txt b/examples/corelib/serialization/savegame/CMakeLists.txt
index f757d1359b..14621ccc23 100644
--- a/examples/corelib/serialization/savegame/CMakeLists.txt
+++ b/examples/corelib/serialization/savegame/CMakeLists.txt
@@ -1,16 +1,17 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
cmake_minimum_required(VERSION 3.16)
project(savegame LANGUAGES CXX)
-set(CMAKE_AUTOMOC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
+if (ANDROID)
+ message(FATAL_ERROR "This project cannot be built on Android.")
endif()
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/serialization/savegame")
-
find_package(Qt6 REQUIRED COMPONENTS Core)
+qt_standard_project_setup()
+
qt_add_executable(savegame
character.cpp character.h
game.cpp game.h
@@ -18,12 +19,19 @@ qt_add_executable(savegame
main.cpp
)
-target_link_libraries(savegame PUBLIC
- Qt::Core
+target_link_libraries(savegame PRIVATE
+ Qt6::Core
)
install(TARGETS savegame
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET savegame
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/corelib/serialization/savegame/character.cpp b/examples/corelib/serialization/savegame/character.cpp
index 43be084d3e..863fcb9153 100644
--- a/examples/corelib/serialization/savegame/character.cpp
+++ b/examples/corelib/serialization/savegame/character.cpp
@@ -6,17 +6,10 @@
#include <QMetaEnum>
#include <QTextStream>
-Character::Character() :
- mLevel(0),
- mClassType(Warrior) {
-}
+Character::Character() = default;
-Character::Character(const QString &name,
- int level,
- Character::ClassType classType) :
- mName(name),
- mLevel(level),
- mClassType(classType)
+Character::Character(const QString &name, int level, Character::ClassType classType)
+ : mName(name), mLevel(level), mClassType(classType)
{
}
@@ -50,35 +43,41 @@ void Character::setClassType(Character::ClassType classType)
mClassType = classType;
}
-//! [0]
-void Character::read(const QJsonObject &json)
+//! [fromJson]
+Character Character::fromJson(const QJsonObject &json)
{
- if (json.contains("name") && json["name"].isString())
- mName = json["name"].toString();
+ Character result;
+
+ if (const QJsonValue v = json["name"]; v.isString())
+ result.mName = v.toString();
+
+ if (const QJsonValue v = json["level"]; v.isDouble())
+ result.mLevel = v.toInt();
- if (json.contains("level") && json["level"].isDouble())
- mLevel = json["level"].toInt();
+ if (const QJsonValue v = json["classType"]; v.isDouble())
+ result.mClassType = ClassType(v.toInt());
- if (json.contains("classType") && json["classType"].isDouble())
- mClassType = ClassType(json["classType"].toInt());
+ return result;
}
-//! [0]
+//! [fromJson]
-//! [1]
-void Character::write(QJsonObject &json) const
+//! [toJson]
+QJsonObject Character::toJson() const
{
+ QJsonObject json;
json["name"] = mName;
json["level"] = mLevel;
json["classType"] = mClassType;
+ return json;
}
-//! [1]
+//! [toJson]
-void Character::print(int indentation) const
+void Character::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
- QTextStream(stdout) << indent << "Name:\t" << mName << "\n";
- QTextStream(stdout) << indent << "Level:\t" << mLevel << "\n";
+ const QString className = QMetaEnum::fromType<ClassType>().valueToKey(mClassType);
- QString className = QMetaEnum::fromType<ClassType>().valueToKey(mClassType);
- QTextStream(stdout) << indent << "Class:\t" << className << "\n";
+ s << indent << "Name:\t" << mName << "\n"
+ << indent << "Level:\t" << mLevel << "\n"
+ << indent << "Class:\t" << className << "\n";
}
diff --git a/examples/corelib/serialization/savegame/character.h b/examples/corelib/serialization/savegame/character.h
index dfb3837331..0504750320 100644
--- a/examples/corelib/serialization/savegame/character.h
+++ b/examples/corelib/serialization/savegame/character.h
@@ -8,15 +8,15 @@
#include <QObject>
#include <QString>
+QT_FORWARD_DECLARE_CLASS(QTextStream)
+
//! [0]
class Character
{
- Q_GADGET;
+ Q_GADGET
public:
- enum ClassType {
- Warrior, Mage, Archer
- };
+ enum ClassType { Warrior, Mage, Archer };
Q_ENUM(ClassType)
Character();
@@ -31,14 +31,15 @@ public:
ClassType classType() const;
void setClassType(ClassType classType);
- void read(const QJsonObject &json);
- void write(QJsonObject &json) const;
+ static Character fromJson(const QJsonObject &json);
+ QJsonObject toJson() const;
+
+ void print(QTextStream &s, int indentation = 0) const;
- void print(int indentation = 0) const;
private:
QString mName;
- int mLevel;
- ClassType mClassType;
+ int mLevel = 0;
+ ClassType mClassType = Warrior;
};
//! [0]
diff --git a/examples/corelib/serialization/savegame/doc/src/savegame.qdoc b/examples/corelib/serialization/savegame/doc/src/savegame.qdoc
index 0246ae48bb..46fca15b62 100644
--- a/examples/corelib/serialization/savegame/doc/src/savegame.qdoc
+++ b/examples/corelib/serialization/savegame/doc/src/savegame.qdoc
@@ -3,18 +3,19 @@
/*!
\example serialization/savegame
- \title JSON Save Game Example
+ \examplecategory {Data Processing & I/O}
+ \title Saving and Loading a Game
- \brief The JSON Save Game example demonstrates how to save and load a
- small game using QJsonDocument, QJsonObject and QJsonArray.
+ \brief How to save and load a game using Qt's JSON or CBOR classes.
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
- \l {RFC 7049} {CBOR} 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.
+ 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 \l
+ {RFC 7049} {CBOR} format, which is great if you don't want the save file to
+ be easy to read (but see \l {Parsing and displaying CBOR data} for how it \e
+ can be read), 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.
@@ -24,45 +25,83 @@
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.
+ It provides static fromJson() and non-static toJson() functions to
+ serialise itself.
+
+ \note This pattern (fromJson()/toJson()) works because QJsonObjects can be
+ constructed independent of an owning QJsonDocument, and because the data
+ types being (de)serialized here are value types, so can be copied. When
+ serializing to another format — for example XML or QDataStream, which require passing
+ a document-like object — or when the object identity is important (QObject
+ subclasses, for example), other patterns may be more suitable. See the
+ \l{dombookmarks} example for XML, and the implementation of
+ \l QListWidgetItem::read() and \l QListWidgetItem::write()
+ for idiomatic QDataStream serialization. The \c{print()} functions in this example
+ are good examples of QTextStream serialization, even though they, of course, lack
+ the deserialization side.
\snippet serialization/savegame/character.h 0
- Of particular interest to us are the read and write function
+ Of particular interest to us are the fromJson() and toJson() function
implementations:
- \snippet serialization/savegame/character.cpp 0
+ \snippet serialization/savegame/character.cpp fromJson
- 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
- check if the keys are valid before attempting to read them with
- QJsonObject::contains().
+ In the fromJson() function, we construct a local \c result Character object
+ and assign \c{result}'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. In particular, the \c{is...}
+ functions (for example \l QJsonValue::isString(), \l
+ QJsonValue::isDouble()) return \c false for QJsonValue::Undefined, so we
+ can check for existence as well as the correct type in a single lookup.
- \snippet serialization/savegame/character.cpp 1
+ If a value does not exist in the JSON object, or has the wrong type, we
+ don't write to the corresponding \c result member, either, thereby
+ preserving any values the default constructor may have set. This means
+ default values are centrally defined in one location (the default
+ constructor) and need not be repeated in serialisation code
+ (\l{https://en.wikipedia.org/wiki/Don%27t_repeat_yourself}{DRY}).
- 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.
+ Observe the use of
+ \l{https://en.cppreference.com/w/cpp/language/if#If_statements_with_initializer}
+ {C++17 if-with-initializer} to separate scoping and checking of the variable \c v.
+ This means we can keep the variable name short, because its scope is limited.
- Next up is the Level class:
+ Compare that to the naïve approach using \c QJsonObject::contains():
+
+ \badcode
+ if (json.contains("name") && json["name"].isString())
+ result.mName = json["name"].toString();
+ \endcode
+
+ which, beside being less readable, requires a total of three lookups (no,
+ the compiler will \e not optimize these into one), so is three times
+ slower and repeats \c{"name"} three times (violating the DRY principle).
+
+ \snippet serialization/savegame/character.cpp toJson
+
+ In the toJson() function, we do the reverse of the fromJson() function;
+ assign values from the Character object to a new JSON object we then
+ return. As with accessing values, there are two ways to set values on a
+ QJsonObject: \l QJsonObject::operator[]() and \l QJsonObject::insert().
+ Both will override any existing value at the given key.
+
+ \section1 The Level Class
\snippet serialization/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.
+ We want the levels in our game to each each have several NPCs, so we keep a QList
+ of Character objects. We also provide the familiar fromJson() and toJson()
+ functions.
- \snippet serialization/savegame/level.cpp 0
+ \snippet serialization/savegame/level.cpp fromJson
- Containers can be written and read to and from JSON using QJsonArray. In our
+ Containers can be written to and read 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 array.
+ toObject() to get the Character's JSON object. Character::fromJson() can
+ then turn that QJSonObject into a Character object to append to our NPC array.
\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
@@ -70,11 +109,13 @@
element is used as the key to construct the container when reading it back
in.
- \snippet serialization/savegame/level.cpp 1
+ \snippet serialization/savegame/level.cpp toJson
- Again, the write() function is similar to the read() function, except
+ Again, the toJson() function is similar to the fromJson() function, except
reversed.
+ \section1 The Game Class
+
Having established the Character and Level classes, we can move on to
the Game class:
@@ -86,26 +127,43 @@
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().
+ The read() and toJson() functions are used by saveGame() and loadGame().
+
+ \div{class="admonition note"}\b{Note:}
+ Despite \c Game being a value class, we assume that the author wants a game to have
+ identity, much like your main window would have. We therefore don't use a
+ static fromJson() function, which would create a new object, but a read()
+ function we can call on existing objects. There's a 1:1 correspondence
+ between read() and fromJson(), in that one can be implemented in terms of
+ the other:
+
+ \code
+ void read(const QJsonObject &json) { *this = fromJson(json); }
+ static Game fromObject(const QJsonObject &json) { Game g; g.read(json); return g; }
+ \endcode
- \snippet serialization/savegame/game.cpp 0
+ We just use what's more convenient for callers of the functions.
+ \enddiv
+
+ \snippet serialization/savegame/game.cpp newGame
To setup a new game, we create the player and populate the levels and their
NPCs.
- \snippet serialization/savegame/game.cpp 1
+ \snippet serialization/savegame/game.cpp read
- The first thing we do in the read() function is tell the player to read
- itself. We then clear the level array so that calling loadGame() on the
- same Game object twice doesn't result in old levels hanging around.
+ The read() function starts by replacing the player with the
+ one read from JSON. We then clear() the level array so that calling
+ loadGame() on the same Game object twice doesn't result in old levels
+ hanging around.
We then populate the level array by reading each Level from a QJsonArray.
- \snippet serialization/savegame/game.cpp 2
+ \snippet serialization/savegame/game.cpp toJson
- We write the game to JSON similarly to how we write Level.
+ Writing the game to JSON is similar to writing a level.
- \snippet serialization/savegame/game.cpp 3
+ \snippet serialization/savegame/game.cpp loadGame
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,
@@ -119,14 +177,16 @@
After constructing the QJsonDocument, we instruct the Game object to read
itself and then return \c true to indicate success.
- \snippet serialization/savegame/game.cpp 4
+ \snippet serialization/savegame/game.cpp saveGame
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.
+ QJsonObject. To save the game in the format that was specified, we
+ convert the JSON object into either a QJsonDocument for a subsequent
+ QJsonDocument::toJson() call, or a QCborValue for QCborValue::toCbor().
+
+ \section1 Tying It All Together
We are now ready to enter main():
@@ -158,5 +218,5 @@
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}
+ \sa {JSON Support in Qt}, {CBOR Support in Qt}, {Data Input Output}
*/
diff --git a/examples/corelib/serialization/savegame/game.cpp b/examples/corelib/serialization/savegame/game.cpp
index 111fbfc6ea..f99ecb8b51 100644
--- a/examples/corelib/serialization/savegame/game.cpp
+++ b/examples/corelib/serialization/savegame/game.cpp
@@ -11,6 +11,8 @@
#include <QRandomGenerator>
#include <QTextStream>
+using namespace Qt::StringLiterals;
+
Character Game::player() const
{
return mPlayer;
@@ -21,52 +23,45 @@ QList<Level> Game::levels() const
return mLevels;
}
-//! [0]
+//! [newGame]
void Game::newGame()
{
mPlayer = Character();
- mPlayer.setName(QStringLiteral("Hero"));
+ mPlayer.setName("Hero"_L1);
mPlayer.setClassType(Character::Archer);
mPlayer.setLevel(QRandomGenerator::global()->bounded(15, 21));
mLevels.clear();
mLevels.reserve(2);
- Level village(QStringLiteral("Village"));
+ Level village("Village"_L1);
QList<Character> villageNpcs;
villageNpcs.reserve(2);
- villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"),
- QRandomGenerator::global()->bounded(8, 11),
- Character::Warrior));
- villageNpcs.append(Character(QStringLiteral("Terry the Trader"),
- QRandomGenerator::global()->bounded(6, 8),
- Character::Warrior));
+ villageNpcs.append(Character("Barry the Blacksmith"_L1,
+ QRandomGenerator::global()->bounded(8, 11), Character::Warrior));
+ villageNpcs.append(Character("Terry the Trader"_L1,
+ QRandomGenerator::global()->bounded(6, 8), Character::Warrior));
village.setNpcs(villageNpcs);
mLevels.append(village);
- Level dungeon(QStringLiteral("Dungeon"));
+ Level dungeon("Dungeon"_L1);
QList<Character> dungeonNpcs;
dungeonNpcs.reserve(3);
- dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"),
- QRandomGenerator::global()->bounded(18, 26),
- Character::Mage));
- dungeonNpcs.append(Character(QStringLiteral("Eric's Left Minion"),
- QRandomGenerator::global()->bounded(5, 7),
- Character::Warrior));
- dungeonNpcs.append(Character(QStringLiteral("Eric's Right Minion"),
- QRandomGenerator::global()->bounded(4, 9),
- Character::Warrior));
+ dungeonNpcs.append(Character("Eric the Evil"_L1,
+ QRandomGenerator::global()->bounded(18, 26), Character::Mage));
+ dungeonNpcs.append(Character("Eric's Left Minion"_L1,
+ QRandomGenerator::global()->bounded(5, 7), Character::Warrior));
+ dungeonNpcs.append(Character("Eric's Right Minion"_L1,
+ QRandomGenerator::global()->bounded(4, 9), Character::Warrior));
dungeon.setNpcs(dungeonNpcs);
mLevels.append(dungeon);
}
-//! [0]
+//! [newGame]
-//! [3]
+//! [loadGame]
bool Game::loadGame(Game::SaveFormat saveFormat)
{
- QFile loadFile(saveFormat == Json
- ? QStringLiteral("save.json")
- : QStringLiteral("save.dat"));
+ QFile loadFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
@@ -76,85 +71,72 @@ bool Game::loadGame(Game::SaveFormat saveFormat)
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(saveFormat == Json
- ? QJsonDocument::fromJson(saveData)
- : QJsonDocument(QCborValue::fromCbor(saveData).toMap().toJsonObject()));
+ ? QJsonDocument::fromJson(saveData)
+ : QJsonDocument(QCborValue::fromCbor(saveData).toMap().toJsonObject()));
read(loadDoc.object());
- QTextStream(stdout) << "Loaded save for "
- << loadDoc["player"]["name"].toString()
- << " using "
- << (saveFormat != Json ? "CBOR" : "JSON") << "...\n";
+ QTextStream(stdout) << "Loaded save for " << loadDoc["player"]["name"].toString()
+ << " using " << (saveFormat != Json ? "CBOR" : "JSON") << "...\n";
return true;
}
-//! [3]
+//! [loadGame]
-//! [4]
+//! [saveGame]
bool Game::saveGame(Game::SaveFormat saveFormat) const
{
- QFile saveFile(saveFormat == Json
- ? QStringLiteral("save.json")
- : QStringLiteral("save.dat"));
+ QFile saveFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
- QJsonObject gameObject;
- write(gameObject);
- saveFile.write(saveFormat == Json
- ? QJsonDocument(gameObject).toJson()
- : QCborValue::fromJsonValue(gameObject).toCbor());
+ QJsonObject gameObject = toJson();
+ saveFile.write(saveFormat == Json ? QJsonDocument(gameObject).toJson()
+ : QCborValue::fromJsonValue(gameObject).toCbor());
return true;
}
-//! [4]
+//! [saveGame]
-//! [1]
+//! [read]
void Game::read(const QJsonObject &json)
{
- if (json.contains("player") && json["player"].isObject())
- mPlayer.read(json["player"].toObject());
+ if (const QJsonValue v = json["player"]; v.isObject())
+ mPlayer = Character::fromJson(v.toObject());
- if (json.contains("levels") && json["levels"].isArray()) {
- QJsonArray levelArray = json["levels"].toArray();
+ if (const QJsonValue v = json["levels"]; v.isArray()) {
+ const QJsonArray levels = v.toArray();
mLevels.clear();
- mLevels.reserve(levelArray.size());
- for (int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex) {
- QJsonObject levelObject = levelArray[levelIndex].toObject();
- Level level;
- level.read(levelObject);
- mLevels.append(level);
- }
+ mLevels.reserve(levels.size());
+ for (const QJsonValue &level : levels)
+ mLevels.append(Level::fromJson(level.toObject()));
}
}
-//! [1]
+//! [read]
-//! [2]
-void Game::write(QJsonObject &json) const
+//! [toJson]
+QJsonObject Game::toJson() const
{
- QJsonObject playerObject;
- mPlayer.write(playerObject);
- json["player"] = playerObject;
-
- QJsonArray levelArray;
- for (const Level &level : mLevels) {
- QJsonObject levelObject;
- level.write(levelObject);
- levelArray.append(levelObject);
- }
- json["levels"] = levelArray;
+ QJsonObject json;
+ json["player"] = mPlayer.toJson();
+
+ QJsonArray levels;
+ for (const Level &level : mLevels)
+ levels.append(level.toJson());
+ json["levels"] = levels;
+ return json;
}
-//! [2]
+//! [toJson]
-void Game::print(int indentation) const
+void Game::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
- QTextStream(stdout) << indent << "Player\n";
- mPlayer.print(indentation + 1);
+ s << indent << "Player\n";
+ mPlayer.print(s, indentation + 1);
- QTextStream(stdout) << indent << "Levels\n";
- for (Level level : mLevels)
- level.print(indentation + 1);
+ s << indent << "Levels\n";
+ for (const Level &level : mLevels)
+ level.print(s, indentation + 1);
}
diff --git a/examples/corelib/serialization/savegame/game.h b/examples/corelib/serialization/savegame/game.h
index 266e764a80..5ba5952923 100644
--- a/examples/corelib/serialization/savegame/game.h
+++ b/examples/corelib/serialization/savegame/game.h
@@ -4,19 +4,19 @@
#ifndef GAME_H
#define GAME_H
+#include "character.h"
+#include "level.h"
+
#include <QJsonObject>
#include <QList>
-#include "character.h"
-#include "level.h"
+QT_FORWARD_DECLARE_CLASS(QTextStream)
//! [0]
class Game
{
public:
- enum SaveFormat {
- Json, Binary
- };
+ enum SaveFormat { Json, Binary };
Character player() const;
QList<Level> levels() const;
@@ -26,9 +26,10 @@ public:
bool saveGame(SaveFormat saveFormat) const;
void read(const QJsonObject &json);
- void write(QJsonObject &json) const;
+ QJsonObject toJson() const;
+
+ void print(QTextStream &s, int indentation = 0) const;
- void print(int indentation = 0) const;
private:
Character mPlayer;
QList<Level> mLevels;
diff --git a/examples/corelib/serialization/savegame/level.cpp b/examples/corelib/serialization/savegame/level.cpp
index 04edadf7f6..f30d35e57f 100644
--- a/examples/corelib/serialization/savegame/level.cpp
+++ b/examples/corelib/serialization/savegame/level.cpp
@@ -6,9 +6,7 @@
#include <QJsonArray>
#include <QTextStream>
-Level::Level(const QString &name) : mName(name)
-{
-}
+Level::Level(const QString &name) : mName(name) { }
QString Level::name() const
{
@@ -25,46 +23,43 @@ void Level::setNpcs(const QList<Character> &npcs)
mNpcs = npcs;
}
-//! [0]
-void Level::read(const QJsonObject &json)
+//! [fromJson]
+Level Level::fromJson(const QJsonObject &json)
{
- if (json.contains("name") && json["name"].isString())
- mName = json["name"].toString();
+ Level result;
+
+ if (const QJsonValue v = json["name"]; v.isString())
+ result.mName = v.toString();
- if (json.contains("npcs") && json["npcs"].isArray()) {
- QJsonArray npcArray = json["npcs"].toArray();
- mNpcs.clear();
- mNpcs.reserve(npcArray.size());
- for (int npcIndex = 0; npcIndex < npcArray.size(); ++npcIndex) {
- QJsonObject npcObject = npcArray[npcIndex].toObject();
- Character npc;
- npc.read(npcObject);
- mNpcs.append(npc);
- }
+ if (const QJsonValue v = json["npcs"]; v.isArray()) {
+ const QJsonArray npcs = v.toArray();
+ result.mNpcs.reserve(npcs.size());
+ for (const QJsonValue &npc : npcs)
+ result.mNpcs.append(Character::fromJson(npc.toObject()));
}
+
+ return result;
}
-//! [0]
+//! [fromJson]
-//! [1]
-void Level::write(QJsonObject &json) const
+//! [toJson]
+QJsonObject Level::toJson() const
{
+ QJsonObject json;
json["name"] = mName;
QJsonArray npcArray;
- for (const Character &npc : mNpcs) {
- QJsonObject npcObject;
- npc.write(npcObject);
- npcArray.append(npcObject);
- }
+ for (const Character &npc : mNpcs)
+ npcArray.append(npc.toJson());
json["npcs"] = npcArray;
+ return json;
}
-//! [1]
+//! [toJson]
-void Level::print(int indentation) const
+void Level::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
- QTextStream(stdout) << indent << "Name:\t" << mName << "\n";
- QTextStream(stdout) << indent << "NPCs:\n";
+ s << indent << "Name:\t" << mName << "\n" << indent << "NPCs:\n";
for (const Character &character : mNpcs)
- character.print(2);
+ character.print(s, indentation + 1);
}
diff --git a/examples/corelib/serialization/savegame/level.h b/examples/corelib/serialization/savegame/level.h
index 4c0a8aed89..e487e55ae3 100644
--- a/examples/corelib/serialization/savegame/level.h
+++ b/examples/corelib/serialization/savegame/level.h
@@ -4,27 +4,30 @@
#ifndef LEVEL_H
#define LEVEL_H
+#include "character.h"
+
#include <QJsonObject>
#include <QList>
-#include "character.h"
+QT_FORWARD_DECLARE_CLASS(QTextStream)
//! [0]
class Level
{
public:
Level() = default;
- Level(const QString &name);
+ explicit Level(const QString &name);
QString name() const;
QList<Character> npcs() const;
void setNpcs(const QList<Character> &npcs);
- void read(const QJsonObject &json);
- void write(QJsonObject &json) const;
+ static Level fromJson(const QJsonObject &json);
+ QJsonObject toJson() const;
+
+ void print(QTextStream &s, int indentation = 0) const;
- void print(int indentation = 0) const;
private:
QString mName;
QList<Character> mNpcs;
diff --git a/examples/corelib/serialization/savegame/main.cpp b/examples/corelib/serialization/savegame/main.cpp
index c9e713c126..3fc0f3af10 100644
--- a/examples/corelib/serialization/savegame/main.cpp
+++ b/examples/corelib/serialization/savegame/main.cpp
@@ -1,32 +1,37 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include "game.h"
+
#include <QCoreApplication>
+#include <QStringList>
+#include <QString>
#include <QTextStream>
-#include "game.h"
+using namespace Qt::StringLiterals; // for _L1
+
//! [0]
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
- QStringList args = QCoreApplication::arguments();
- bool newGame = true;
- if (args.length() > 1)
- newGame = (args[1].toLower() != QStringLiteral("load"));
- bool json = true;
- if (args.length() > 2)
- json = (args[2].toLower() != QStringLiteral("binary"));
+
+ const QStringList args = QCoreApplication::arguments();
+ const bool newGame
+ = args.size() <= 1 || QString::compare(args[1], "load"_L1, Qt::CaseInsensitive) != 0;
+ const bool json
+ = args.size() <= 2 || QString::compare(args[2], "binary"_L1, Qt::CaseInsensitive) != 0;
Game game;
if (newGame)
game.newGame();
else if (!game.loadGame(json ? Game::Json : Game::Binary))
- return 1;
+ return 1;
// Game is played; changes are made...
//! [0]
//! [1]
- QTextStream(stdout) << "Game ended in the following state:\n";
- game.print();
+ QTextStream s(stdout);
+ s << "Game ended in the following state:\n";
+ game.print(s);
if (!game.saveGame(json ? Game::Json : Game::Binary))
return 1;