simplified reading and writing

pull/12/head
mandlm 2018-05-24 21:07:42 +02:00
parent 8f170eb83b
commit 201675f9c4
2 changed files with 27 additions and 28 deletions

View File

@ -10,36 +10,14 @@ DataModel::DataModel(QObject *parent)
void DataModel::write(QJsonObject &target) const
{
QJsonObject metaData;
m_metaData.write(metaData);
target["MetaData"] = metaData;
QJsonObject verbEnd;
m_verbEnd.write(verbEnd);
target["VerbEnd"] = verbEnd;
QJsonObject genus;
m_genus.write(genus);
target["Genus"] = genus;
write(m_metaData, target, "MetaData");
write(m_verbEnd, target, "VerbEnd");
write(m_genus, target, "Genus");
}
void DataModel::read(const QJsonObject &source)
{
const auto &metaData = source["MetaData"];
if (metaData.isObject())
{
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());
}
read(m_metaData, source, "MetaData");
read(m_verbEnd, source, "VerbEnd");
read(m_genus, source, "Genus");
}

View File

@ -20,4 +20,25 @@ public:
void write(QJsonObject &target) const;
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());
}
}
};