-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadWriteJsonData.cpp
148 lines (131 loc) · 4.55 KB
/
ReadWriteJsonData.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "ReadWriteJsonData.h"
ReadWriteJsonData::ReadWriteJsonData()
{
QFile file("Sample1.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray jsonData = file.readAll();
file.close();
qDebug()<<"Read "<< m_CheckDataType(jsonData);
}
bool ReadWriteJsonData::m_CheckDataType(QByteArray RawData)
{
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(RawData, &error);
if (json_doc.isNull())
{
qDebug() << "Failed to create JSON doc." << error.errorString();
QDomDocument *doc = new QDomDocument;
RawData.replace("<","<");
RawData.replace(">",">");
if (!doc->setContent(RawData))
{
qDebug() <<"QByteArray not set to content. Xml data content has error.";
qDebug()<< "This is neither JSON nor XML data .";
return false;
}
else
{
qDebug() <<"This is XML data. ";
return true;
}
}
else
{
qDebug() <<"This is JSON data. ";
m_ReadJSON_byQJsonObject(json_doc.object());
return true;
}
return false;
}
void ReadWriteJsonData::m_ReadJSON_byQJsonObject(QJsonObject jsonObj)
{
QString strModuleName;
if (jsonObj.isEmpty()) {
qDebug() << "JSON object is empty." ;
}
else
{
strModuleName = jsonObj["MODULE_NAME"].toString();
QString fID = jsonObj["FUNCTION_ID"].toString();
m_Iterator(jsonObj, "FDATA");
qDebug()<<strModuleName <<fID <<"\n MAPPED_DATA "<<Map_FData<<"\n LIST_OF_MAPPED_DATA "<<ListMap_FData;
}
}
void ReadWriteJsonData::m_Iterator(QJsonObject jsonObj, QString TagName)
{
if(jsonObj[TagName].isArray())
{
QJsonArray fdata = jsonObj[TagName].toArray();
ListMap_FData.clear();
QVariantList list = fdata.toVariantList();
for(int i = 0; i<list.size(); i ++)
{
QMap<QString,QVariant> vMap = list.at(i).toMap();
QMap<QString,QString> mapData;
for(QMap<QString,QVariant>::Iterator iter = vMap.begin(); iter != vMap.end(); ++iter)
mapData.insert(iter.key(), iter.value().value<QString>());
ListMap_FData << mapData;
}
}
else
{
QJsonObject fdata_obj= jsonObj[TagName].toObject();
QVariantMap vMap = fdata_obj.toVariantMap();
Map_FData.clear();
for(QVariantMap::const_iterator iter = vMap.begin(); iter != vMap.end(); ++iter)
{
if(iter.value().userType() == QMetaType::QString)
Map_FData.insert(iter.key(),iter.value().value<QString>());
else
{
QJsonObject obj = QJsonObject::fromVariantMap(vMap);
m_Iterator(obj, iter.key());
}
}
//qDebug()<<TagName <<Map_FData;
}
}
void ReadWriteJsonData::m_ListMap_DataWrite(QString mName, int fId, QList<QMap<QString,QString> > lMap)
{
QJsonObject docObject;
docObject.insert("MODULE_NAME", QJsonValue::fromVariant(mName));
docObject.insert("FUNCTION_ID", QJsonValue::fromVariant(QString::number(fId).rightJustified(2,'0')));
QJsonArray listArray;
for(int i=0; i<lMap.size(); i++)
{
QMap<QString, QString> tempMap = lMap[i];
QJsonObject tempObject;
for (QMap<QString, QString>::iterator iter = tempMap.begin(); iter != tempMap.end(); ++iter)
tempObject.insert(iter.key(), iter.value());
listArray.push_back(tempObject);
}
docObject.insert("FDATA", listArray);
QFile file("Sample10.json");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QJsonDocument doc(docObject);
qint64 bytes = file.write(doc.toJson());
qDebug()<<"Write bytes "<<bytes;
}
else
qDebug()<<"unable to write";
}
void ReadWriteJsonData::m_Map_DataWrite(QString mName, int fId, QMap<QString,QString> tempMap)
{
QJsonObject docObject;
docObject.insert("MODULE_NAME", QJsonValue::fromVariant(mName));
docObject.insert("FUNCTION_ID", QJsonValue::fromVariant(QString::number(fId).rightJustified(2,'0')));
QJsonObject tempObject;
for (QMap<QString, QString>::iterator iter = tempMap.begin(); iter != tempMap.end(); ++iter)
tempObject.insert(iter.key(), iter.value());
docObject.insert("FDATA", tempObject);
QFile file("Sample11.json");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QJsonDocument doc(docObject);
qint64 bytes = file.write(doc.toJson());
qDebug()<<"Write bytes "<<bytes;
}
else
qDebug()<<"unable to write";
}