simplified reading and writing
This commit is contained in:
parent
8f170eb83b
commit
201675f9c4
2 changed files with 27 additions and 28 deletions
|
@ -10,36 +10,14 @@ DataModel::DataModel(QObject *parent)
|
||||||
|
|
||||||
void DataModel::write(QJsonObject &target) const
|
void DataModel::write(QJsonObject &target) const
|
||||||
{
|
{
|
||||||
QJsonObject metaData;
|
write(m_metaData, target, "MetaData");
|
||||||
m_metaData.write(metaData);
|
write(m_verbEnd, target, "VerbEnd");
|
||||||
target["MetaData"] = metaData;
|
write(m_genus, target, "Genus");
|
||||||
|
|
||||||
QJsonObject verbEnd;
|
|
||||||
m_verbEnd.write(verbEnd);
|
|
||||||
target["VerbEnd"] = verbEnd;
|
|
||||||
|
|
||||||
QJsonObject genus;
|
|
||||||
m_genus.write(genus);
|
|
||||||
target["Genus"] = genus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataModel::read(const QJsonObject &source)
|
void DataModel::read(const QJsonObject &source)
|
||||||
{
|
{
|
||||||
const auto &metaData = source["MetaData"];
|
read(m_metaData, source, "MetaData");
|
||||||
if (metaData.isObject())
|
read(m_verbEnd, source, "VerbEnd");
|
||||||
{
|
read(m_genus, source, "Genus");
|
||||||
m_metaData.read(metaData.toObject());
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto &verbEnd = source["VerbEnd"];
|
|
||||||
if (verbEnd.isObject())
|
|
||||||
{
|
|
||||||
m_verbEnd.read(verbEnd.toObject());
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto &genus = source["Genus"];
|
|
||||||
if (genus.isObject())
|
|
||||||
{
|
|
||||||
m_genus.read(genus.toObject());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,4 +20,25 @@ public:
|
||||||
|
|
||||||
void write(QJsonObject &target) const;
|
void write(QJsonObject &target) const;
|
||||||
void read(const QJsonObject &source);
|
void read(const QJsonObject &source);
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <class ModelType>
|
||||||
|
void write(
|
||||||
|
const ModelType &model, QJsonObject &target, const char *name) const
|
||||||
|
{
|
||||||
|
QJsonObject jsonObject;
|
||||||
|
model.write(jsonObject);
|
||||||
|
target[name] = jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class ModelType>
|
||||||
|
void read(
|
||||||
|
ModelType &model, const QJsonObject &source, const char *name) const
|
||||||
|
{
|
||||||
|
const auto &jsonObject = source[name];
|
||||||
|
if (jsonObject.isObject())
|
||||||
|
{
|
||||||
|
model.read(jsonObject.toObject());
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue