Log Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

#include "viewmodel/displayString/util/ScopeTranslationParser.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QFile>
#include <limits>
constexpr int INVALID_SCOPE_ID = std::numeric_limits<int>::min();
/*************************************************************************************************/
ScopeTranslationParser::ScopeTranslationParser(QString configFilePath) :
_configFilePath(configFilePath)
{
}
/*************************************************************************************************/
QMap<int, QString> ScopeTranslationParser::parse() const
{
auto scopes = createJsonObject(readConfigFile())["scopes"];
QMap<int, QString> map;
if(scopes.isArray())
{
for(const auto val : scopes.toArray())
{
auto translation = getScopeTranslationFromJsonObj(val.toObject());
if(translation.has_value())
{
map[translation.value().first] = translation.value().second;
}
}
}
return map;
}
/*************************************************************************************************/
QByteArray ScopeTranslationParser::readConfigFile() const
{
QFile file(_configFilePath);
return file.readAll();
}
/*************************************************************************************************/
QJsonObject ScopeTranslationParser::createJsonObject(const QByteArray& jsonFileContent) const
{
QJsonDocument doc = QJsonDocument::fromJson(jsonFileContent);
return doc.object();
}
/*************************************************************************************************/
std::optional<QPair<int, QString> > ScopeTranslationParser::getScopeTranslationFromJsonObj(
const QJsonObject& obj) const
{
auto id = obj["id"].toInt(INVALID_SCOPE_ID);
auto nameVal = obj["name"];
if(id == INVALID_SCOPE_ID or not nameVal.isString())
{
return std::nullopt;
}
return QPair<int, QString>{id, nameVal.toString()};
}