forked from rrozek/QtJsonModifyValue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
278 lines (232 loc) · 9.27 KB
/
json.cpp
File metadata and controls
278 lines (232 loc) · 9.27 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "json.h"
Json::Json(bool debugMode, bool strictMode)
{
this->debugMode = debugMode;
this->strictMode = strictMode;
}
void Json::setDebugMode(bool debugMode)
{
this->debugMode = debugMode;
}
void Json::setStrictMode(bool strictMode)
{
this->strictMode = strictMode;
}
QList<QVariant> Json::createPathParts(const QString &path)
{
QRegularExpression rx("([^\\[\\]\\.\\\\]|\\\\\\\\|\\\\\\[|\\\\\\]|\\\\\\.)+|\\.\\."); // regex to find parts with escape sequences
QRegularExpressionMatchIterator it = rx.globalMatch(path);
QList<QVariant> pathParts; // parts of path
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
int prev = match.capturedStart() - 1; // index of char before part
int next = match.capturedEnd(); // index of char after part
bool pathPartIsObject; // if part of path is array or object
if (0 <= prev && next < path.size() && path[prev] == '[' && path[next] == ']') // if part is array
pathPartIsObject = false;
else if (((prev == -1 || path[prev] == ']' || path[prev] == '.') && (next == path.size() || path[next] == '.' || path[next] == '[')) || match.captured() == "..") // if part is object
pathPartIsObject = true;
else { // error in path
if (debugMode) {
qDebug() << "Critical error in path near:" << path.mid(prev + 1, next - prev - 1);
}
return QList<QVariant>() << QVariant();
}
bool ok = true;
if (pathPartIsObject)
pathParts.append(match.captured().replace("..", "").replace("\\\\", "\\").replace("\\[", "[").replace("\\]", "]").replace("\\.", ".")); // insert part into list as object key and remove escape sequences
else
pathParts.append(match.captured().toInt(&ok)); // insert part into list as array index and check if index is valid
if (!ok) { // if array index is invalid
if (debugMode) {
qDebug() << "Requested array index is invalid number:" << path.mid(prev, next - prev + 1);
}
return QList<QVariant>() << QVariant();
}
}
return pathParts;
}
QList<QJsonValue> Json::getSubPathValues(const QJsonValue &val, const QList<QVariant> &pathParts, const bool &strictMode)
{
QList<QJsonValue> values; // all sub values in path
values.append(val); // add root element(value)
for (int i = 0; i < pathParts.size(); ++i) { // expand whole subpath
QVariant part = pathParts.at(i);
QJsonValue lastVal = values.last();
if (part.type() == static_cast<int>(QMetaType::QString))
if (lastVal.isObject()) {
values.append(lastVal.toObject().value(part.toString()));
} else if (lastVal.isUndefined() && !strictMode) {
values.replace(values.size() - 1, QJsonObject());
values.append(values.last().toObject().value(part.toString()));
} else {
if (debugMode) {
qDebug() << "You have requested Json object:" << part.toString();
qDebug() << "But current QJsonValue is not object:" << lastVal;
}
return QList<QJsonValue>();
}
else
if (lastVal.isArray()) {
QJsonArray lastValArr = lastVal.toArray();
for (int j = lastValArr.size(); j < part.toInt(); ++j) {
lastValArr.append(QJsonValue());
}
values.replace(values.size() - 1, lastValArr);
values.append(values.last().toArray().at(part.toInt()));
} else if (lastVal.isUndefined() && !strictMode) {
values.replace(values.size() - 1,QJsonArray());
QJsonArray lastValArr = lastVal.toArray();
for (int j = lastValArr.size(); j < part.toInt(); ++j) {
lastValArr.append(QJsonValue());
}
values.replace(values.size() - 1, lastValArr);
values.append(values.last().toArray().at(part.toInt()));
} else {
if (debugMode) {
qDebug() << "You have requested Json array with index:" << "[" + part.toString() + "]";
qDebug() << "But current QJsonValue is not array:" << lastVal;
}
return QList<QJsonValue>();
}
}
if (strictMode && values.last().isUndefined()) {
if (debugMode) {
if (pathParts.last().type() == static_cast<int>(QMetaType::QString)) {
qDebug() << "You are trying to create new key-value:" << pathParts.last().toString();
qDebug() << "while in strict mode inside of object:" << values.at(values.size() - 2);
} else {
qDebug() << "You are trying to create new array index:" << "[" + pathParts.last().toString() + "]";
qDebug() << "while in strict mode inside of array:" << values.at(values.size() - 2);
}
}
return QList<QJsonValue>();
}
return values;
}
QJsonValue Json::joinSubPathValues(QList<QJsonValue> values, const QList<QVariant> &pathParts)
{
for (int i = pathParts.count() - 1; i >= 0; --i) { // assign back subpath
QJsonValue lastVal = values.last();
values.removeLast();
QVariant part = pathParts.at(i);
if (part.type() == static_cast<int>(QMetaType::QString)) {
QJsonObject updateVal = values.last().toObject();
if (lastVal.isUndefined()) {
updateVal.remove(part.toString());
} else {
updateVal.insert(part.toString(), lastVal);
}
values.removeLast();
values.append(updateVal);
} else {
QJsonArray updateVal = values.last().toArray();
if (lastVal.isUndefined()) {
updateVal.removeAt(part.toInt());
} else {
updateVal.removeAt(part.toInt());
updateVal.insert(part.toInt(), lastVal);
}
values.removeLast();
values.append(updateVal);
}
}
return values.first(); // assign new root element(value)
}
bool Json::create(QJsonDocument *doc, const QString &path, const QJsonValue &newValue)
{
QJsonValue val;
if (doc->isArray())
val = doc->array();
else if (doc->isObject())
val = doc->object();
else
return false;
bool returnValue = create(&val, path, newValue);
if (val.isArray())
doc->setArray(val.toArray());
else if (val.isObject())
doc->setObject(val.toObject());
else
return false;
return returnValue;
}
bool Json::create(QJsonValue *val, const QString &path, const QJsonValue &newValue)
{
QList<QVariant> pathParts = createPathParts(path); // parts of path
if (pathParts.size() && !pathParts.at(0).isValid()) {
return false;
}
QList<QJsonValue> values = getSubPathValues(*val, pathParts, false); // all sub values in path
if (values.isEmpty()) {
return false;
}
values.removeLast(); // remove
values.append(newValue); // and assign new value
*val = joinSubPathValues(values, pathParts); // assign new root element(value)
return true;
}
bool Json::modify(QJsonDocument *doc, const QString &path, const QJsonValue &newValue)
{
QJsonValue val;
if (doc->isArray())
val = doc->array();
else if (doc->isObject())
val = doc->object();
else
return false;
bool returnValue = modify(&val, path, newValue);
if (val.isArray())
doc->setArray(val.toArray());
else if (val.isObject())
doc->setObject(val.toObject());
else
return false;
return returnValue;
}
bool Json::modify(QJsonValue *val, const QString &path, const QJsonValue &newValue)
{
QList<QVariant> pathParts = createPathParts(path); // parts of path
if (pathParts.size() && !pathParts.at(0).isValid()) {
return false;
}
QList<QJsonValue> values = getSubPathValues(*val, pathParts, strictMode); // all sub values in path
if (values.isEmpty()) {
return false;
}
values.removeLast(); // remove
values.append(newValue); // and assign new value
*val = joinSubPathValues(values, pathParts); // assign new root element(value)
return true;
}
QJsonValue Json::get(const QJsonDocument &doc, const QString &path)
{
QJsonValue val;
if (doc.isArray())
val = doc.array();
else if (doc.isObject())
val = doc.object();
else
return QJsonValue(QJsonValue::Undefined);
return get(val, path);
}
QJsonValue Json::get(const QJsonValue &val, const QString &path)
{
QList<QVariant> pathParts = createPathParts(path); // parts of path
if (pathParts.size() && !pathParts.at(0).isValid()) {
return QJsonValue(QJsonValue::Undefined);
}
QList<QJsonValue> values = getSubPathValues(val, pathParts, true); // all sub values in path
if (values.isEmpty()) {
return QJsonValue(QJsonValue::Undefined);
}
return values.last();
}
bool Json::remove(QJsonDocument *doc, const QString &path)
{
return modify(doc, path, QJsonValue(QJsonValue::Undefined));
}
bool Json::remove(QJsonValue *val, const QString &path)
{
return modify(val, path, QJsonValue(QJsonValue::Undefined));
}