Skip to content

Instantly share code, notes, and snippets.

@alexzaporozhets
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexzaporozhets/64fc76663258b62b45d3 to your computer and use it in GitHub Desktop.
Save alexzaporozhets/64fc76663258b62b45d3 to your computer and use it in GitHub Desktop.
JSON schema 2 Qt class
struct Authorization
{
struct Company
{
Company()
{
}
Company(const Company &other)
{
operator =(other);
}
Company &operator=(const Company &other)
{
id = other.id;
name = other.name;
role = other.role;
return *this;
}
QString id;
QString name;
QString role;
};
Authorization(const QJsonObject &jsonObj)
{
id = jsonObj["id"].toString();
name = jsonObj["name"].toString();
eMail = jsonObj["eMail"].toString();
QJsonArray::ConstIterator i;
QJsonArray jsonArray;
// You can copy these lines for other lists
jsonArray = jsonObj["companies"].toArray();
for(i = jsonArray.constBegin(); i != jsonArray.constEnd() ; i++) {
Company company;
company.id = (*i).toObject()["id"].toString();
company.name = (*i).toObject()["name"].toString();
company.role = (*i).toObject()["role"].toString();
companies.append(company);
}
// You can copy these lines for other lists
}
Authorization(const Authorization &other)
{
operator =(other);
}
Authorization &operator =(const Authorization &other)
{
id = other.id;
name = other.name;
eMail = other.eMail;
companies = other.companies;
return *this;
}
QJsonObject toJsonObject() const
{
QJsonObject jsonObj;
jsonObj["id"] = id;
jsonObj["name"] = name;
jsonObj["email"] = eMail;
QJsonArray jsonArray;
foreach (Company company, companies) {
QJsonObject jsonObj;
jsonObj["id"] = company.id;
jsonObj["name"] = company.name;
jsonObj["role"] = company.role;
jsonArray.append(jsonObj);
}
jsonObj["companies"] = jsonArray;
jsonArray = QJsonArray();
return jsonObj;
}
QString id;
QString name;
QString eMail;
QVector<Company> companies;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment