Created
March 4, 2015 09:53
-
-
Save drewpts/f0bf9acda9a322a04ace to your computer and use it in GitHub Desktop.
jsonToQVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class jsonToQVM { | |
typedef std::initializer_list<std::string> ils; | |
private: | |
json11::Json json; | |
std::vector<std::string> keywords; | |
QString write(QVariantMap& qvm, json11::Json j, QList<std::string> k) | |
{ | |
for (const auto& keyword : k) { | |
if (j[keyword].is_object()) //for LPROJ-12 | |
return "Unsupported field in project file\n"; | |
else if (j[keyword].is_null()) | |
qvm.insert(QString(keyword.c_str()), | |
QVariant()); | |
else if (j[keyword].is_number()) | |
qvm.insert(QString(keyword.c_str()), | |
QVariant(j[keyword].number_value())); | |
else if (j[keyword].is_bool()) | |
qvm.insert(QString(keyword.c_str()), | |
QVariant(j[keyword].bool_value())); | |
else if (j[keyword].is_string()) //aggregates fields like author | |
qvm.insert(QString(keyword.c_str()), | |
QVariant(QString(j[keyword]. | |
string_value().c_str()))); | |
else if (j[keyword].is_array() //now aggregates "file"-field | |
&& j[keyword].array_items().at(0).is_string()) { | |
QStringList strings; | |
for (const auto& str : j[keyword].array_items()) | |
strings << str.string_value().c_str(); | |
qvm.insert(QString(keyword.c_str()), | |
QVariant(strings)); | |
} else return "Unknown error\n"; | |
return QString(); | |
} | |
} | |
public: | |
explicit jsonToQVM(const json11::Json& json_, const ils& keywords_) | |
: json(json_), keywords(keywords_) { } | |
QVariantMap& operator()(QVariantMap& writeTo) | |
{ | |
if (!writeTo.isEmpty()) { | |
qWarning() << "Container isn't empty"; | |
return writeTo; | |
} else { | |
QString result; | |
for (const auto& keyword : keywords) | |
result += write(writeTo, json, QList<std::string>() << keyword); | |
if (result.isEmpty()) | |
return writeTo; | |
else { | |
writeTo.clear(); | |
qWarning() << "Error when writing QVariantMap"; | |
return writeTo; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment