Created
January 22, 2018 13:58
-
-
Save NorbertFenk/a134a9378f003f50fabb17d5ccf26d03 to your computer and use it in GitHub Desktop.
Simple Qt based JSON file loading
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
QVariant loadJson(const QString &path) | |
{ | |
QFile file(path); | |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
throw Exception(QStringLiteral("Could not open file")); | |
} | |
QByteArray input = file.readAll(); | |
file.close(); | |
QJsonParseError error = {}; | |
QJsonDocument document = QJsonDocument::fromJson(input, &error); | |
if (error.error != QJsonParseError::NoError) { | |
throw Exception(error.errorString()); | |
} | |
return document.toVariant(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment