Implemented JSON serialization
Added new iterator and const_iterator objects to UserList and also implemented the serialization function to write to a JSON file
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
UserList loadUserList(const QString &file)
|
||||
{
|
||||
QFile doc{std::filesystem::path{file.toStdString()}};
|
||||
doc.open(QFile::ReadOnly);
|
||||
|
||||
QString fileContents = doc.readAll();
|
||||
|
||||
QJsonParseError errors;
|
||||
@ -24,11 +26,11 @@ UserList loadUserList(const QString &file)
|
||||
QJsonArray array = document.array();
|
||||
QVector<User> container;
|
||||
|
||||
for (auto elem: array)
|
||||
for (QJsonArray::const_iterator elem = array.constBegin(); elem != array.constEnd(); ++elem)
|
||||
{
|
||||
auto obj = elem.toObject();
|
||||
User newUser{obj["name"].toString(), QDate::fromString(obj["date_of_birth"].toString()),
|
||||
QDate::fromString(obj["date_of_death"].toString()), obj["default"].toBool()};
|
||||
auto obj = elem->toObject();
|
||||
User newUser{obj[NAME_LABEL].toString(), QDate::fromString(obj[DATE_OF_BIRTH_LABEL].toString()),
|
||||
QDate::fromString(obj[DATE_OF_DEATH_LABEL].toString()), obj[DEFAULT_LABEL].toBool()};
|
||||
|
||||
container.append(newUser);
|
||||
}
|
||||
@ -36,7 +38,24 @@ UserList loadUserList(const QString &file)
|
||||
return container;
|
||||
}
|
||||
|
||||
void saveUserList(const UserList &list)
|
||||
void saveUserList(const UserList &list, const QString &file)
|
||||
{
|
||||
QFile doc{std::filesystem::path{file.toStdString()}};
|
||||
doc.open(QFile::WriteOnly);
|
||||
|
||||
QJsonArray container;
|
||||
|
||||
for(auto &obj: list)
|
||||
{
|
||||
QJsonObject newObject;
|
||||
newObject[NAME_LABEL] = obj.getName();
|
||||
newObject[DATE_OF_BIRTH_LABEL] = obj.getDateOfBirth().toString();
|
||||
newObject[DATE_OF_DEATH_LABEL] = obj.getDateOfDeath().toString();
|
||||
newObject[DEFAULT_LABEL] = obj.getIsItDefault();
|
||||
|
||||
container.append(newObject);
|
||||
}
|
||||
|
||||
QJsonDocument document{container};
|
||||
doc.write(document.toJson());
|
||||
}
|
||||
|
Reference in New Issue
Block a user