-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (161 loc) · 4.09 KB
/
main.cpp
File metadata and controls
182 lines (161 loc) · 4.09 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <QCoreApplication>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
void modifyJsonValue(QJsonValue &destValue, const QString& path, const QJsonValue& newValue);
void modifyJsonValue(QJsonDocument& doc, const QString& path, const QJsonValue& newValue);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string sampleJson = R"({
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"family":
[
[
"Jeff",
"Marika",
"Tony"
],
[
"Steve",
"Sonny",
"Sally"
],
{
"father":
{
"name": "Mike",
"age": 55
},
"mother":
{
"name": "Jennifer",
"age": 49
}
}
]
})";
QJsonDocument doc = QJsonDocument::fromJson(QByteArray::fromStdString(sampleJson));
QJsonObject obj = doc.object();
modifyJsonValue(doc, "firstName", QJsonValue("Natalia"));
modifyJsonValue(doc, "age", 22);
modifyJsonValue(doc, "address.state", "None");
modifyJsonValue(doc, "phoneNumber[0].number", "333 543-3210");
modifyJsonValue(doc, "family[0][2]", "Bill");
modifyJsonValue(doc, "family[1][1]", "Winston");
modifyJsonValue(doc, "family[2].father.age", 56);
QJsonObject obj2 = doc.object();
return a.exec();
}
void modifyJsonValue(QJsonValue& destValue, const QString& path, const QJsonValue& newValue)
{
const int indexOfDot = path.indexOf('.');
const QString dotPropertyName = path.left(indexOfDot);
const QString dotSubPath = indexOfDot > 0 ? path.mid(indexOfDot + 1) : QString();
const int indexOfSquareBracketOpen = path.indexOf('[');
const int indexOfSquareBracketClose = path.indexOf(']');
const int arrayIndex = path.mid(indexOfSquareBracketOpen + 1, indexOfSquareBracketClose - indexOfSquareBracketOpen - 1).toInt();
const QString squareBracketPropertyName = path.left(indexOfSquareBracketOpen);
const QString squareBracketSubPath = indexOfSquareBracketClose > 0 ? (path.mid(indexOfSquareBracketClose + 1)[0] == '.' ? path.mid(indexOfSquareBracketClose + 2) : path.mid(indexOfSquareBracketClose + 1)) : QString();
bool useDot = true;
if (indexOfDot >= 0)
{
if (indexOfSquareBracketOpen >= 0)
{
if (indexOfDot > indexOfSquareBracketOpen)
useDot = false;
else
useDot = true;
}
else
useDot = true;
}
else
{
if (indexOfSquareBracketOpen >= 0)
useDot = false;
else
useDot = true;
}
QString usedPropertyName = useDot ? dotPropertyName : squareBracketPropertyName;
QString usedSubPath = useDot ? dotSubPath : squareBracketSubPath;
QJsonValue subValue;
if (destValue.isArray())
subValue = destValue.toArray()[usedPropertyName.toInt()];
else if (destValue.isObject())
subValue = destValue.toObject()[usedPropertyName];
else
qDebug() << "oh, what should i do now with the following value?! " << destValue;
if (subValue.isArray())
{
QJsonArray arr = subValue.toArray();
QJsonValue arrEntry;
if(! usedSubPath.isEmpty()) {
arrEntry = arr[arrayIndex];
modifyJsonValue(arrEntry,usedSubPath,newValue);
arr[arrayIndex] = arrEntry;
} else {
if(arrayIndex<arr.size()) {
arrEntry = newValue;
arr[arrayIndex] = arrEntry;
}
else {
arr.append(newValue);
}
}
subValue = arr;
} else if (subValue.isObject() && !usedSubPath.isEmpty()) {
modifyJsonValue(subValue,usedSubPath,newValue);
} else {
subValue = newValue;
}
if (destValue.isArray())
{
QJsonArray arr = destValue.toArray();
arr[arrayIndex] = subValue;
destValue = arr;
}
else if (destValue.isObject())
{
QJsonObject obj = destValue.toObject();
obj[usedPropertyName] = subValue;
destValue = obj;
}
else
destValue = newValue;
}
void modifyJsonValue(QJsonDocument& doc, const QString& path, const QJsonValue& newValue)
{
QJsonValue val;
if (doc.isArray())
val = doc.array();
else
val = doc.object();
modifyJsonValue(val,path,newValue);
if (val.isArray())
doc = QJsonDocument(val.toArray());
else
doc = QJsonDocument(val.toObject());
}