Browse Source

Add new isJsonFile method with input of QFile

QGC4.4
DonLakeFlyer 5 years ago
parent
commit
cc0080b00a
  1. 16
      src/JsonHelper.cc
  2. 6
      src/JsonHelper.h

16
src/JsonHelper.cc

@ -225,11 +225,27 @@ bool JsonHelper::isJsonFile(const QByteArray& bytes, QJsonDocument& jsonDoc, QSt
if (parseError.error == QJsonParseError::NoError) { if (parseError.error == QJsonParseError::NoError) {
return true; return true;
} else { } else {
int startPos = qMax(0, parseError.offset - 100);
int length = qMin(bytes.count() - startPos, 200);
qDebug() << QStringLiteral("Json read error '%1'").arg(bytes.mid(startPos, length).constData());
errorString = parseError.errorString(); errorString = parseError.errorString();
return false; return false;
} }
} }
bool JsonHelper::isJsonFile(const QString& fileName, QJsonDocument& jsonDoc, QString& errorString)
{
QFile jsonFile(fileName);
if (!jsonFile.open(QFile::ReadOnly)) {
errorString = tr("File open failed: file:error %1 %2").arg(jsonFile.fileName()).arg(jsonFile.errorString());
return false;
}
QByteArray jsonBytes = jsonFile.readAll();
jsonFile.close();
return isJsonFile(jsonBytes, jsonDoc, errorString);
}
bool JsonHelper::validateInternalQGCJsonFile(const QJsonObject& jsonObject, bool JsonHelper::validateInternalQGCJsonFile(const QJsonObject& jsonObject,
const QString& expectedFileType, const QString& expectedFileType,
int minSupportedVersion, int minSupportedVersion,

6
src/JsonHelper.h

@ -26,6 +26,12 @@ class JsonHelper
Q_DECLARE_TR_FUNCTIONS(JsonHelper) Q_DECLARE_TR_FUNCTIONS(JsonHelper)
public: public:
/// Determines is the specified file is a json file
/// @return true: file is json, false: file is not json
static bool isJsonFile(const QString& fileName, ///< filename
QJsonDocument& jsonDoc, ///< returned json document
QString& errorString); ///< error on parse failure
/// Determines is the specified data is a json file /// Determines is the specified data is a json file
/// @return true: file is json, false: file is not json /// @return true: file is json, false: file is not json
static bool isJsonFile(const QByteArray& bytes, ///< json bytes static bool isJsonFile(const QByteArray& bytes, ///< json bytes

Loading…
Cancel
Save