Browse Source

SettingsComponent: use Utils::OpenJsonDocument

This function strips C-style comments from the JSON string before
parsing it. The next commit relies on this.

Also removes some duplicated code.
Vincent Lang 9 years ago
parent
commit
ab66ef84fb
1 changed files with 11 additions and 19 deletions
  1. 11 19
      src/settings/SettingsComponent.cpp

+ 11 - 19
src/settings/SettingsComponent.cpp

@@ -64,24 +64,6 @@ QVariant SettingsComponent::allValues(const QString& section)
   }
 }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-static QByteArray loadFile(const QString& filename)
-{
-  QLOG_DEBUG() << "Opening:" << filename;
-
-  QFile file(filename);
-  // Checking existence before opening is technically a race condition, but
-  // it looks like Qt doesn't let us distinguish errors on opening.
-  if (!file.exists())
-    return "";
-  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
-  {
-    QLOG_ERROR() << "Could not open" << filename;
-    return "";
-  }
-  return file.readAll();
-}
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 static void writeFile(const QString& filename, const QByteArray& data)
 {
@@ -97,7 +79,17 @@ static void writeFile(const QString& filename, const QByteArray& data)
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 static QJsonObject loadJson(const QString& filename)
 {
-  QJsonDocument json = QJsonDocument::fromJson(loadFile(filename));
+  // Checking existence before opening is technically a race condition, but
+  // it looks like Qt doesn't let us distinguish errors on opening.
+  if (!QFile(filename).exists())
+    return QJsonObject();
+
+  QJsonParseError err;
+  QJsonDocument json = Utils::OpenJsonDocument(filename, &err);
+  if (json.isNull())
+  {
+    QLOG_ERROR() << "Could not open" << filename << "due to" << err.errorString();
+  }
   return json.object();
 }