| 82 | } |
| 83 | |
| 84 | QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent) { |
| 85 | auto* rootItem = new QJsonTreeItem(parent); |
| 86 | // rootItem->setKey("root"); |
| 87 | |
| 88 | if (value.isObject()) { |
| 89 | const auto& object = value.toObject(); |
| 90 | |
| 91 | // determine the size |
| 92 | rootItem->setSize(QJsonDocument(object).toJson(QJsonDocument::Compact).size()); |
| 93 | |
| 94 | // read all children |
| 95 | for (const auto& k : object.keys()) { |
| 96 | const auto& v = object.value(k); |
| 97 | auto* childItem = load(v, rootItem); |
| 98 | childItem->setKey(k); |
| 99 | childItem->setType(v.type()); |
| 100 | rootItem->appendChild(childItem); |
| 101 | } |
| 102 | } else if (value.isArray()) { |
| 103 | const auto& array = value.toArray(); |
| 104 | rootItem->setSize(QJsonDocument(array).toJson(QJsonDocument::Compact).size()); |
| 105 | |
| 106 | int index = 0; |
| 107 | rootItem->reserve(array.count()); |
| 108 | for (const QJsonValue& v : array) { |
| 109 | auto* childItem = load(v, rootItem); |
| 110 | childItem->setKey(QString::number(index)); |
| 111 | childItem->setType(v.type()); |
| 112 | rootItem->appendChild(childItem); |
| 113 | ++index; |
| 114 | } |
| 115 | } else { |
| 116 | const QString& str = value.toVariant().toString(); |
| 117 | rootItem->setValue(str); |
| 118 | rootItem->setType(value.type()); |
| 119 | rootItem->setSize(str.length()); |
| 120 | } |
| 121 | |
| 122 | return rootItem; |
| 123 | } |
| 124 | |
| 125 | //========================================================================= |
| 126 |
nothing calls this directly
no test coverage detected