| 46 | QJsonArray Source::childrenArray() { return m_jsonDoc.object()["children"].toArray(); } |
| 47 | |
| 48 | int main(int argc, char *argv[]) |
| 49 | { |
| 50 | QCoreApplication a(argc, argv); |
| 51 | |
| 52 | QJsonDocument emptyDoc; |
| 53 | Source source; |
| 54 | QJsonDocument jsonDoc = source.jsonDocument(); |
| 55 | QJsonObject jsonObj = jsonDoc.object(); |
| 56 | |
| 57 | QByteArray jsonString = jsonDoc.toJson(QJsonDocument::Indented); |
| 58 | |
| 59 | // Parsing JSON string back to QJsonDocument |
| 60 | QJsonDocument parsedDoc = QJsonDocument::fromJson(jsonString); |
| 61 | // Clear the very long string after use to prevent overly wide Value column in |
| 62 | // KDevelop's Variables tool view, which moves the Type column far to the right. |
| 63 | jsonString.clear(); |
| 64 | |
| 65 | // Check if the parsing was successful |
| 66 | if (!parsedDoc.isNull() && parsedDoc.isObject()) { |
| 67 | QJsonObject parsedObj = parsedDoc.object(); |
| 68 | |
| 69 | // Output parsed JSON object |
| 70 | QString nameStr = "name"; |
| 71 | const auto nameRef = parsedObj[nameStr]; |
| 72 | const QJsonValue name = nameRef; |
| 73 | const auto yearRef = parsedObj["year"]; |
| 74 | const QJsonValue year = yearRef; |
| 75 | const auto ageRef = parsedObj["age"]; |
| 76 | const QJsonValue age = ageRef; |
| 77 | const auto marriedRef = parsedObj["married"]; |
| 78 | const QJsonValue married = marriedRef; |
| 79 | |
| 80 | const auto parsedChildrenRef = parsedObj["children"]; |
| 81 | const QJsonValue parsedChildrenValue = parsedChildrenRef; |
| 82 | QJsonArray parsedChildren = parsedChildrenRef.toArray(); |
| 83 | const QJsonDocument childrenDoc(parsedChildren); |
| 84 | for (const auto &child : parsedChildren) { |
| 85 | QString childName = child.toString(); |
| 86 | } |
| 87 | } else { |
| 88 | qCritical() << "Failed to parse JSON string."; |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
nothing calls this directly
no test coverage detected