Created
December 29, 2016 16:23
-
-
Save Paulchen-Panther/e157e0081bccdbe123d1237ee1b50104 to your computer and use it in GitHub Desktop.
delete or modify JSON value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
#include <stdlib.h> | |
// QT include | |
#include <QString> | |
#include <QJsonObject> | |
#include <QJsonValue> | |
#include <QJsonArray> | |
class QJsonUtils | |
{ | |
public: | |
static void modifyValue(QJsonObject& value, std::list<std::string> path, const QJsonValue& newValue = QJsonValue::Null) | |
{ | |
QJsonObject result; | |
if (path.front() == "[root]") | |
path.pop_front(); | |
modifyValue(value, result, path, newValue); | |
value = result; | |
} | |
private: | |
static void modifyValue(QJsonValue source, QJsonObject& target, std::list<std::string> path, const QJsonValue& newValue) | |
{ | |
QJsonObject obj = source.toObject(); | |
for (QJsonObject::iterator i = obj.begin(); i != obj.end(); ++i) | |
{ | |
QString propertyName = i.key(); | |
QJsonValue subValue = obj[propertyName]; | |
if (subValue.isObject()) | |
{ | |
if (propertyName.toStdString() == path.front()) | |
{ | |
path.pop_front(); | |
if (!path.empty()) | |
{ | |
QJsonObject obj; | |
modifyValue(subValue, obj, path, newValue); | |
subValue = obj; | |
} | |
else if (newValue != QJsonValue::Null) | |
{ | |
subValue = newValue; | |
} | |
else | |
continue; | |
if (!subValue.toObject().isEmpty()) | |
target[propertyName] = subValue; | |
} | |
else | |
if (!subValue.toObject().isEmpty()) | |
target[propertyName] = subValue; | |
} | |
else if (subValue.isArray()) | |
{ | |
if (propertyName.toStdString() == path.front()) | |
{ | |
path.pop_front(); | |
if ( (path.front().substr(0,1) == "[" ) && ( path.front().substr(path.front().length()-1, 1) == "]") ) | |
path.pop_front(); | |
QJsonArray array = subValue.toArray(); | |
QJsonArray json_array; | |
for (QJsonArray::iterator i = array.begin(); i != array.end(); ++i) | |
{ | |
if (!path.empty()) | |
{ | |
QJsonObject arr; | |
modifyValue(*i, arr, path, newValue); | |
subValue = arr; | |
} | |
else if (newValue != QJsonValue::Null) | |
{ | |
subValue = newValue; | |
} | |
else | |
continue; | |
if (!subValue.toObject().isEmpty()) | |
json_array.append(subValue); | |
} | |
target[propertyName] = json_array; | |
} | |
else | |
if (!subValue.toArray().isEmpty()) | |
target[propertyName] = subValue; | |
} | |
else | |
{ | |
if (propertyName.toStdString() == path.front()) | |
{ | |
path.pop_front(); | |
if (path.empty()) | |
{ | |
if (newValue != QJsonValue::Null) | |
{ | |
subValue = newValue; | |
} | |
else | |
continue; | |
} | |
target[propertyName] = subValue; | |
} | |
else | |
target[propertyName] = subValue; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment