diff --git a/plugins/amda/src/AmdaParser.cpp b/plugins/amda/src/AmdaParser.cpp index a4de1f0..f13f3ca 100644 --- a/plugins/amda/src/AmdaParser.cpp +++ b/plugins/amda/src/AmdaParser.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -11,8 +12,65 @@ Q_LOGGING_CATEGORY(LOG_AmdaParser, "AmdaParser") namespace { // Significant keys of an AMDA's JSON file +const auto COMPONENT_KEY = QStringLiteral("component"); +const auto PRODUCT_KEY = QStringLiteral("parameter"); const auto ROOT_KEY = QStringLiteral("dataCenter"); +/// Returns the correct item type according to the key passed in parameter +DataSourceItemType itemType(const QString &key) noexcept +{ + if (key == PRODUCT_KEY) { + return DataSourceItemType::PRODUCT; + } + else if (key == COMPONENT_KEY) { + return DataSourceItemType::COMPONENT; + } + else { + return DataSourceItemType::NODE; + } +} + +/** + * Processes an entry of the JSON file to populate/create data source items + * @param jsonKey the entry's key + * @param jsonValue the entry's value + * @param item the current item for which the entry processing will be applied + * @param appendData flag indicating that the entry is part of an array. In the case of an array of + * values, each value will be concatenated to the others (rather than replacing the others) + */ +void parseEntry(const QString &jsonKey, const QJsonValue &jsonValue, DataSourceItem &item, + bool isArrayEntry = false) +{ + if (jsonValue.isObject()) { + // Case of an object: + // - a new data source item is created and + // - parsing is called recursively to process the new item + // - the new item is then added as a child of the former item + auto object = jsonValue.toObject(); + + auto newItem = std::make_unique(itemType(jsonKey)); + + for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) { + parseEntry(it.key(), it.value(), *newItem); + } + + item.appendChild(std::move(newItem)); + } + else if (jsonValue.isArray()) { + // Case of an array: the item is populated with the arrays' content + auto object = jsonValue.toArray(); + + for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) { + parseEntry(jsonKey, *it, item, true); + } + } + else { + // Case of a simple value: we add a data to the item. If the simple value is a part of an + // array, it is concatenated to the values already existing for this key + item.setData(jsonKey, jsonValue.toVariant(), isArrayEntry); + } +} + } // namespace std::unique_ptr AmdaParser::readJson(const QString &filePath) noexcept @@ -58,6 +116,14 @@ std::unique_ptr AmdaParser::readJson(const QString &filePath) no .arg(filePath); return nullptr; } - /// @todo ALX - return nullptr; + + // Makes the parsing + auto rootObject = rootValue.toObject(); + auto rootItem = std::make_unique(DataSourceItemType::NODE); + + for (auto it = rootObject.constBegin(), end = rootObject.constEnd(); it != end; ++it) { + parseEntry(it.key(), it.value(), *rootItem); + } + + return rootItem; }