Added deserialization from JSON file
This commit is contained in:
@ -19,6 +19,8 @@ qt_add_qml_module(appProject-Orion
|
||||
Main.qml
|
||||
SOURCES user.h user.cpp
|
||||
SOURCES userlist.h userlist.cpp
|
||||
SOURCES serialization.h
|
||||
SOURCES serialization.cpp
|
||||
)
|
||||
|
||||
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
|
||||
|
35
src/Project-Orion/serialization.cpp
Normal file
35
src/Project-Orion/serialization.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "serialization.h"
|
||||
#include <filesystem>
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
UserList&& loadUserList(const QString &file)
|
||||
{
|
||||
QFile doc{std::filesystem::path{file.toStdString()}};
|
||||
QString fileContents = doc.readAll();
|
||||
QJsonDocument document = QJsonDocument::fromJson(fileContents.toUtf8());
|
||||
|
||||
QJsonArray array = document.array();
|
||||
QVector<User> container;
|
||||
|
||||
for (auto elem: array)
|
||||
{
|
||||
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()};
|
||||
|
||||
container.append(newUser);
|
||||
}
|
||||
|
||||
UserList returnObj;
|
||||
returnObj.setList(container);
|
||||
|
||||
return std::move(returnObj);
|
||||
}
|
||||
|
||||
void saveUserList(const UserList &list)
|
||||
{
|
||||
|
||||
}
|
9
src/Project-Orion/serialization.h
Normal file
9
src/Project-Orion/serialization.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef SERIALIZATION_H
|
||||
#define SERIALIZATION_H
|
||||
|
||||
#include "userlist.h"
|
||||
|
||||
UserList &&loadUserList(const QString &file);
|
||||
void saveUserList(const UserList &list);
|
||||
|
||||
#endif // SERIALIZATION_H
|
@ -1,5 +1,5 @@
|
||||
#include "user.h"
|
||||
|
||||
User::User(QString name, QDate dateOfBirth, QDate dateOfDeath, bool isItDefault)
|
||||
: name{name}, dateOfBirth{dateOfBirth}, dateOfDeath{dateOfDeath}
|
||||
: name{name}, dateOfBirth{dateOfBirth}, dateOfDeath{dateOfDeath}, isItDefault{isItDefault}
|
||||
{}
|
||||
|
@ -1,11 +1,13 @@
|
||||
#ifndef USERLIST_H
|
||||
#define USERLIST_H
|
||||
|
||||
#include <QObject>
|
||||
#include "user.h"
|
||||
|
||||
class UserList
|
||||
#include <QObject>
|
||||
|
||||
class UserList: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
UserList();
|
||||
|
||||
|
Reference in New Issue
Block a user