14 static void modify(QJsonObject& value, QStringList path,
const QJsonValue& newValue = QJsonValue::Null, QString propertyName =
"")
20 if (path.first() ==
"[root]")
23 for (QStringList::iterator it = path.begin(); it != path.end(); ++it)
25 QString current = *it;
26 if (current.left(1) ==
".")
27 *it = current.mid(1, current.size()-1);
31 modifyValue(value, result, path, newValue, propertyName);
32 else if (newValue != QJsonValue::Null && !propertyName.isEmpty())
33 result[propertyName] = newValue;
39 static QJsonValue create(QJsonValue schema,
bool ignoreRequired =
false)
41 return createValue(schema, ignoreRequired);
46 static QJsonValue createValue(QJsonValue schema,
bool ignoreRequired)
49 QJsonObject obj = schema.toObject();
51 if (obj.find(
"type") != obj.end() && obj.find(
"type").value().isString())
53 QJsonValue ret = QJsonValue::Null;
55 if (obj.find(
"type").value().toString() ==
"object" && ( obj.find(
"required").value().toBool() || ignoreRequired ) )
56 ret = createValue(obj[
"properties"], ignoreRequired);
57 else if (obj.find(
"type").value().toString() ==
"array" && ( obj.find(
"required").value().toBool() || ignoreRequired ) )
61 if (obj.find(
"default") != obj.end())
62 ret = obj.find(
"default").value();
65 ret = createValue(obj[
"items"], ignoreRequired);
67 if (!ret.toObject().isEmpty())
72 else if ( obj.find(
"required").value().toBool() || ignoreRequired )
73 if (obj.find(
"default") != obj.end())
74 ret = obj.find(
"default").value();
80 for (QJsonObject::const_iterator i = obj.begin(); i != obj.end(); ++i)
82 QString attribute = i.key();
83 const QJsonValue & attributeValue = *i;
84 QJsonValue subValue = obj[attribute];
86 if (attributeValue.toObject().find(
"type") != attributeValue.toObject().end())
88 if (attributeValue.toObject().find(
"type").value().toString() ==
"object" && ( attributeValue.toObject().find(
"required").value().toBool() || ignoreRequired ) )
90 if (obj.contains(
"properties"))
91 result[attribute] = createValue(obj[
"properties"], ignoreRequired);
93 result[attribute] = createValue(subValue, ignoreRequired);
95 else if (attributeValue.toObject().find(
"type").value().toString() ==
"array" && ( attributeValue.toObject().find(
"required").value().toBool() || ignoreRequired ) )
99 if (attributeValue.toObject().find(
"default") != attributeValue.toObject().end())
100 result[attribute] = attributeValue.toObject().find(
"default").value();
104 retEmpty = createValue(attributeValue.toObject()[
"items"], ignoreRequired);
106 if (!retEmpty.toObject().isEmpty())
107 array.append(retEmpty);
108 result[attribute] = array;
111 else if ( attributeValue.toObject().find(
"required").value().toBool() || ignoreRequired )
113 if (attributeValue.toObject().find(
"default") != attributeValue.toObject().end())
114 result[attribute] = attributeValue.toObject().find(
"default").value();
116 result[attribute] = QJsonValue::Null;
125 static void modifyValue(QJsonValue source, QJsonObject& target, QStringList path,
const QJsonValue& newValue, QString& property)
127 QJsonObject obj = source.toObject();
131 for (QJsonObject::iterator i = obj.begin(); i != obj.end(); ++i)
133 QString propertyName = i.key();
134 QJsonValue subValue = obj[propertyName];
136 if (subValue.isObject())
140 if (propertyName == path.first())
147 modifyValue(subValue, obj, path, newValue, property);
150 else if (newValue != QJsonValue::Null)
155 if (!subValue.toObject().isEmpty())
156 target[propertyName] = subValue;
160 if (path.first() ==
property && newValue != QJsonValue::Null)
162 target[property] = newValue;
163 property = QString();
166 target[propertyName] = subValue;
170 if (!subValue.toObject().isEmpty())
171 target[propertyName] = subValue;
173 else if (subValue.isArray())
177 if (propertyName == path.first())
184 if ((path.first().left(1) ==
"[") && (path.first().right(1) ==
"]"))
186 arrayLevel = path.first().mid(1, path.first().size()-2).toInt();
191 QJsonArray array = subValue.toArray();
192 QJsonArray json_array;
194 for (QJsonArray::iterator i = array.begin(); i != array.end(); ++i)
199 modifyValue(*i, arr, path, newValue, property);
202 else if (newValue != QJsonValue::Null)
207 if (!subValue.toObject().isEmpty())
208 json_array.append(subValue);
209 else if (newValue != QJsonValue::Null && arrayLevel != -1)
210 json_array.append( (i - array.begin() == arrayLevel) ? subValue : *i );
213 if (!json_array.isEmpty())
214 target[propertyName] = json_array;
215 else if (newValue != QJsonValue::Null && arrayLevel == -1)
216 target[propertyName] = newValue;
220 if (path.first() ==
property && newValue != QJsonValue::Null)
222 target[property] = newValue;
223 property = QString();
226 target[propertyName] = subValue;
230 if (!subValue.toArray().isEmpty())
231 target[propertyName] = subValue;
237 if (propertyName == path.first())
243 if (newValue != QJsonValue::Null && property.isEmpty())
249 target[propertyName] = subValue;
253 if (path.first() ==
property && newValue != QJsonValue::Null)
255 target[property] = newValue;
256 property = QString();
259 target[propertyName] = subValue;
263 target[propertyName] = subValue;
267 else if (newValue != QJsonValue::Null && !property.isEmpty())
269 target[property] = newValue;
270 property = QString();
Definition: QJsonUtils.h:10