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 #endif
} }
if (file.open(QFile::ReadOnly)) { if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll(); QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return;
QJsonDocument doc(QJsonDocument::fromJson(content)); QJsonDocument doc(QJsonDocument::fromJson(content));
fromJson(doc.array()); fromJson(doc.array());
file.close(); file.close();

View File

@ -82,7 +82,9 @@ void CodeSnippetsManager::loadSnippets()
return; return;
} }
QByteArray json = file.readAll(); QByteArray json = file.readAll().trimmed();
if (json.isEmpty())
return;
QJsonParseError error; QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(json,&error); QJsonDocument doc = QJsonDocument::fromJson(json,&error);
if (error.error != QJsonParseError::NoError) { 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()); qDebug()<<QObject::tr("Can't open file '%1' for read").arg(file.fileName());
return PColorScheme(); return PColorScheme();
} }
QByteArray content = file.readAll(); QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return PColorScheme();
QJsonParseError error; QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(content,&error); QJsonDocument doc = QJsonDocument::fromJson(content,&error);
if (error.error!=QJsonParseError::NoError) { if (error.error!=QJsonParseError::NoError) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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