Skip to content

Instantly share code, notes, and snippets.

@NorbertFenk
Created January 22, 2018 13:58
Show Gist options
  • Save NorbertFenk/a134a9378f003f50fabb17d5ccf26d03 to your computer and use it in GitHub Desktop.
Save NorbertFenk/a134a9378f003f50fabb17d5ccf26d03 to your computer and use it in GitHub Desktop.
Simple Qt based JSON file loading
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