From d479a5e24266901ca45d87fbb2e72adcc03b307e Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 7 Feb 2025 23:06:27 +0200 Subject: [PATCH] Implemented JSON serialization Added new iterator and const_iterator objects to UserList and also implemented the serialization function to write to a JSON file --- src/Project-Orion/serialization.cpp | 29 ++++++++++--- src/Project-Orion/serialization.h | 7 +++- src/Project-Orion/user.cpp | 40 ++++++++++++++++++ src/Project-Orion/user.h | 12 ++++++ src/Project-Orion/userlist.h | 63 ++++++++++++++++++++++++++++- 5 files changed, 144 insertions(+), 7 deletions(-) diff --git a/src/Project-Orion/serialization.cpp b/src/Project-Orion/serialization.cpp index d31692f..2341ed4 100644 --- a/src/Project-Orion/serialization.cpp +++ b/src/Project-Orion/serialization.cpp @@ -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 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()); } diff --git a/src/Project-Orion/serialization.h b/src/Project-Orion/serialization.h index 674e88f..f22a540 100644 --- a/src/Project-Orion/serialization.h +++ b/src/Project-Orion/serialization.h @@ -1,9 +1,14 @@ #ifndef SERIALIZATION_H #define SERIALIZATION_H +#define NAME_LABEL "name" +#define DATE_OF_BIRTH_LABEL "date_of_birth" +#define DATE_OF_DEATH_LABEL "date_of_death" +#define DEFAULT_LABEL "default" + #include "userlist.h" UserList loadUserList(const QString &file); -void saveUserList(const UserList &list); +void saveUserList(const UserList &list, const QString &file); #endif // SERIALIZATION_H diff --git a/src/Project-Orion/user.cpp b/src/Project-Orion/user.cpp index bbd7b30..79b8d72 100644 --- a/src/Project-Orion/user.cpp +++ b/src/Project-Orion/user.cpp @@ -3,3 +3,43 @@ User::User(QString name, QDate dateOfBirth, QDate dateOfDeath, bool isItDefault) : name{name}, dateOfBirth{dateOfBirth}, dateOfDeath{dateOfDeath}, isItDefault{isItDefault} {} + +QString User::getName() const +{ + return name; +} + +void User::setName(const QString &newName) +{ + name = newName; +} + +QDate User::getDateOfBirth() const +{ + return dateOfBirth; +} + +void User::setDateOfBirth(const QDate &newDateOfBirth) +{ + dateOfBirth = newDateOfBirth; +} + +QDate User::getDateOfDeath() const +{ + return dateOfDeath; +} + +void User::setDateOfDeath(const QDate &newDateOfDeath) +{ + dateOfDeath = newDateOfDeath; +} + +bool User::getIsItDefault() const +{ + return isItDefault; +} + +void User::setIsItDefault(bool newIsItDefault) +{ + isItDefault = newIsItDefault; +} diff --git a/src/Project-Orion/user.h b/src/Project-Orion/user.h index 5aa0821..a1ab1d5 100644 --- a/src/Project-Orion/user.h +++ b/src/Project-Orion/user.h @@ -7,6 +7,18 @@ class User public: User(QString name, QDate dateOfBirth, QDate dateOfDeath, bool isItDefault = false); + QString getName() const; + void setName(const QString &newName); + + QDate getDateOfBirth() const; + void setDateOfBirth(const QDate &newDateOfBirth); + + QDate getDateOfDeath() const; + void setDateOfDeath(const QDate &newDateOfDeath); + + bool getIsItDefault() const; + void setIsItDefault(bool newIsItDefault); + private: QString name; QDate dateOfBirth; diff --git a/src/Project-Orion/userlist.h b/src/Project-Orion/userlist.h index 6b0c155..9b0e573 100644 --- a/src/Project-Orion/userlist.h +++ b/src/Project-Orion/userlist.h @@ -5,7 +5,7 @@ #include -class UserList: public QObject +class UserList : public QObject { Q_OBJECT public: @@ -14,6 +14,67 @@ public: QVector getList() const; void setList(const QVector &newList); + struct iteratable + { + using iterator_category = std::forward_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = User; + using pointer = User *; // or also value_type* + using reference = User&; // or also value_type& + + iteratable(User *begin) + : ptr{begin} + {} + + reference operator*() const { return *ptr; } + pointer operator->() { return ptr; } + + // Prefix increment + iteratable& operator++() { ptr++; return *this; } + + // Postfix increment + iteratable operator++(int) { iteratable tmp = *this; ++(*this); return tmp; } + + friend bool operator== (const iteratable& a, const iteratable& b) { return a.ptr == b.ptr; }; + friend bool operator!= (const iteratable& a, const iteratable& b) { return a.ptr != b.ptr; }; + + private: + User* ptr; + }; + struct const_interatable + { + using iterator_category = std::forward_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = const User; + using pointer = const User *; // or also value_type* + using reference = const User&; // or also value_type& + + const_interatable(const User *begin) + : ptr{begin} + {} + + reference operator*() const { return *ptr; } + pointer operator->() { return ptr; } + + // Prefix increment + const_interatable& operator++() { ptr++; return *this; } + + // Postfix increment + const_interatable operator++(int) { const_interatable tmp = *this; ++(*this); return tmp; } + + friend bool operator== (const const_interatable& a, const const_interatable& b) { return a.ptr == b.ptr; }; + friend bool operator!= (const const_interatable& a, const const_interatable& b) { return a.ptr != b.ptr; }; + + private: + const User* ptr; + }; + + iteratable begin() { return &*list.begin(); } + iteratable end() { return &*list.end(); } + + const_interatable begin() const { return &*list.begin(); } + const_interatable end() const { return &*list.end(); } + private: QVector list; };