prevent json file read error

This commit is contained in:
Roy Qu 2024-04-01 19:33:28 +08:00
parent e10b5b1d39
commit a0e3339bf7
11 changed files with 34 additions and 11 deletions

View File

@ -81,7 +81,9 @@ void AutolinkManager::load()
#endif
}
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return;
QJsonDocument doc(QJsonDocument::fromJson(content));
fromJson(doc.array());
file.close();

View File

@ -82,7 +82,9 @@ void CodeSnippetsManager::loadSnippets()
return;
}
QByteArray json = file.readAll();
QByteArray json = file.readAll().trimmed();
if (json.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(json,&error);
if (error.error != QJsonParseError::NoError) {

View File

@ -68,7 +68,9 @@ PColorScheme ColorScheme::load(const QString &filename)
qDebug()<<QObject::tr("Can't open file '%1' for read").arg(file.fileName());
return PColorScheme();
}
QByteArray content = file.readAll();
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return PColorScheme();
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(content,&error);
if (error.error!=QJsonParseError::NoError) {

View File

@ -381,7 +381,9 @@ QList<PIconSet> IconsManager::listIconSets()
pSet->displayName = pSet->name;
QFile infoFile(includeTrailingPathDelimiter(fileInfo.absoluteFilePath())+"info.json");
if (infoFile.exists() && infoFile.open(QFile::ReadOnly)) {
QByteArray content = infoFile.readAll();
QByteArray content = infoFile.readAll().trimmed();
if (content.isEmpty())
return result;
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(content,&error));
if (error.error == QJsonParseError::NoError) {

View File

@ -1928,7 +1928,9 @@ QHash<QString, PProjectEditorLayout> Project::loadLayout()
QFile file(jsonFilename);
if (!file.open(QIODevice::ReadOnly))
return layouts;
QByteArray content = file.readAll();
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return layouts;
QJsonParseError parseError;
QJsonDocument doc(QJsonDocument::fromJson(content,&parseError));
file.close();

View File

@ -46,7 +46,9 @@ void ShortcutManager::load()
return;
}
QByteArray json = file.readAll();
QByteArray json = file.readAll().trimmed();
if (json.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(json,&error);
if (error.error != QJsonParseError::NoError) {

View File

@ -42,7 +42,9 @@ void SymbolUsageManager::load()
tr("Can't open symbol usage file '%1' for read.")
.arg(filename));
}
QByteArray contents = file.readAll();
QByteArray contents = file.readAll().trimmed();
if (contents.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(contents,&error);
if (error.error != QJsonParseError::NoError) {

View File

@ -204,7 +204,9 @@ AppTheme::AppTheme(const QString &filename, ThemeType type, QObject *parent):QOb
.arg(filename));
}
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return;
QJsonObject obj;
switch (type) {

View File

@ -69,7 +69,9 @@ void ToolsManager::load()
return;
}
QByteArray json = file.readAll();
QByteArray json = file.readAll().trimmed();
if (json.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(json,&error);
if (error.error != QJsonParseError::NoError) {

View File

@ -96,8 +96,11 @@ PVisitHistory VisitHistoryManager::doLoad(const QString &filename, qint64 criter
QFile file(filename);
if (!file.open(QFile::ReadOnly))
return pHistory;
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return pHistory;
QJsonParseError error;
QJsonDocument doc=QJsonDocument::fromJson(file.readAll(),&error);
QJsonDocument doc=QJsonDocument::fromJson(content, &error);
if (error.error!=QJsonParseError::NoError)
return pHistory;
bool ok;

View File

@ -174,7 +174,9 @@ void OJProblemSetModel::loadFromFile(const QString &fileName, int& currentIndex)
{
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(content,&error));
if (error.error!=QJsonParseError::NoError) {