@@ -1,11 +1,63 | |||
|
1 | 1 | #include "AmdaParser.h" |
|
2 | 2 | |
|
3 | 3 | #include <DataSource/DataSourceItem.h> |
|
4 | 4 | |
|
5 | #include <QFile> | |
|
6 | #include <QJsonDocument> | |
|
7 | #include <QJsonObject> | |
|
8 | ||
|
5 | 9 | Q_LOGGING_CATEGORY(LOG_AmdaParser, "AmdaParser") |
|
6 | 10 | |
|
11 | namespace { | |
|
12 | ||
|
13 | // Significant keys of an AMDA's JSON file | |
|
14 | const auto ROOT_KEY = QStringLiteral("dataCenter"); | |
|
15 | ||
|
16 | } // namespace | |
|
17 | ||
|
7 | 18 | std::unique_ptr<DataSourceItem> AmdaParser::readJson(const QString &filePath) noexcept |
|
8 | 19 | { |
|
20 | QFile jsonFile{filePath}; | |
|
21 | ||
|
22 | if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
|
23 | qCCritical(LOG_AmdaParser()) | |
|
24 | << QObject::tr("Can't retrieve data source tree from file %1: %2") | |
|
25 | .arg(filePath, jsonFile.errorString()); | |
|
26 | return nullptr; | |
|
27 | } | |
|
28 | ||
|
29 | auto json = jsonFile.readAll(); | |
|
30 | auto jsonDocument = QJsonDocument::fromJson(json); | |
|
31 | ||
|
32 | // Check preconditions for parsing | |
|
33 | if (!jsonDocument.isObject()) { | |
|
34 | qCCritical(LOG_AmdaParser()) | |
|
35 | << QObject::tr( | |
|
36 | "Can't retrieve data source tree from file %1: the file is malformed (there is " | |
|
37 | "not one and only one root object)") | |
|
38 | .arg(filePath); | |
|
39 | return nullptr; | |
|
40 | } | |
|
41 | ||
|
42 | auto jsonDocumentObject = jsonDocument.object(); | |
|
43 | if (!jsonDocumentObject.contains(ROOT_KEY)) { | |
|
44 | qCCritical(LOG_AmdaParser()) | |
|
45 | << QObject::tr( | |
|
46 | "Can't retrieve data source tree from file %1: the file is malformed (the key " | |
|
47 | "for the root element was not found (%2))") | |
|
48 | .arg(filePath, ROOT_KEY); | |
|
49 | return nullptr; | |
|
50 | } | |
|
51 | ||
|
52 | auto rootValue = jsonDocumentObject.value(ROOT_KEY); | |
|
53 | if (!rootValue.isObject()) { | |
|
54 | qCCritical(LOG_AmdaParser()) | |
|
55 | << QObject::tr( | |
|
56 | "Can't retrieve data source tree from file %1: the file is malformed (the root " | |
|
57 | "element is of the wrong type)") | |
|
58 | .arg(filePath); | |
|
59 | return nullptr; | |
|
60 | } | |
|
9 | 61 | /// @todo ALX |
|
10 | 62 | return nullptr; |
|
11 | 63 | } |
General Comments 0
You need to be logged in to leave comments.
Login now