-
Notifications
You must be signed in to change notification settings - Fork 1k
/
agoraqtjson.cpp
80 lines (73 loc) · 2.65 KB
/
agoraqtjson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "agoraqtjson.h"
#include <QDir>
#include <QFile>
#include <QApplication>
#include <QJsonParseError>
#include <QMessageBox>
#include <QJsonObject>
/////////////////////////////////////////////
/// \brief AgoraQtJson::GetAgoraQtJson
////////////////////////////////////////////
AgoraQtJson::AgoraQtJson(QObject *parent)
: QObject(parent)
, bSuccess(false)
, isArray(false)
{
QString path = QApplication::applicationDirPath();
jsonFilePath = path + QString("/Parameters.json");
QDir jsonDir(path);
if(jsonDir.exists()){
QFile jsonFile(jsonFilePath);
if(jsonFile.open(QIODevice::ReadOnly)){
QByteArray allData = jsonFile.readAll();
jsonFile.close();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if(json_error.error == QJsonParseError::NoError){
if(isArray = jsonDoc.isArray()){
jsParameters = jsonDoc.array();
bSuccess = true;
}
}
}
}
}
AgoraQtJson::~AgoraQtJson()
{
}
bool AgoraQtJson::GetParameters(std::map<std::string, std::string>& mapStringParameters,
std::map<std::string, bool>& mapBoolParameters,
std::map<std::string, int>& mapIntParameters,
std::map<std::string, std::string>& mapObjectParameters)
{
if (!bSuccess)
return false;
if(isArray){
int size = jsParameters.size();
for(int i=0; i<size; ++i){
QJsonValue js = jsParameters[i];
QJsonObject jsObj = js.toObject();
QStringList keys = jsObj.keys();
for(int j =0 ; j<keys.size(); ++j){
QString key = keys[j];
if(jsObj[key].isBool()){
bool b = jsObj[key].toBool();
mapBoolParameters.insert(std::make_pair(key.toUtf8().data(), b));
} else if(jsObj[key].isString()){
QString s = jsObj[key].toString();
mapStringParameters.insert(std::make_pair(key.toUtf8().data(), s.toStdString()));
} else if (jsObj[key].isObject()){
mapObjectParameters.insert(std::make_pair(key.toUtf8().data(), jsObj[key].toString().toUtf8().data() ));
}
else {
int value = jsObj[key].toInt();
mapIntParameters.insert(std::make_pair(key.toUtf8().data(), value));
}
}
}
return true;
}else{
QMessageBox::critical(nullptr, ("AgoraOpenLive"), ("Parameters file format"));
}
return false;
}