! determines the relevant part of the full JSON document to be read and its structure. returns \c true if successful, \c false otherwise. */
| 412 | returns \c true if successful, \c false otherwise. |
| 413 | */ |
| 414 | bool JsonFilterPrivate::prepareDocumentToRead() { |
| 415 | PERFTRACE(QStringLiteral("Prepare the JSON document to read")); |
| 416 | |
| 417 | if (modelRows.isEmpty()) |
| 418 | m_preparedDoc = m_doc; |
| 419 | else { |
| 420 | if (modelRows.size() == 1) |
| 421 | m_preparedDoc = m_doc; // root element selected, use the full document |
| 422 | else { |
| 423 | // when running tests there is no ImportFileWidget and JsonOptionsWidget available |
| 424 | // where the model is created and also passed to JsonFilter. So, we need to create |
| 425 | // a model here for in this case. |
| 426 | if (!model) { |
| 427 | model = new QJsonModel(); |
| 428 | model->loadJson(m_doc); |
| 429 | } |
| 430 | |
| 431 | QModelIndex index; |
| 432 | for (auto& it : modelRows) |
| 433 | index = model->index(it, 0, index); |
| 434 | |
| 435 | m_preparedDoc = model->genJsonByIndex(index); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (!m_preparedDoc.isEmpty()) { |
| 440 | if (m_preparedDoc.isArray()) |
| 441 | containerType = JsonFilter::DataContainerType::Array; |
| 442 | else if (m_preparedDoc.isObject()) |
| 443 | containerType = JsonFilter::DataContainerType::Object; |
| 444 | else |
| 445 | return false; |
| 446 | } else |
| 447 | return false; |
| 448 | |
| 449 | int countRows = 0; |
| 450 | int countCols = -1; |
| 451 | QJsonValue firstRow; |
| 452 | QString firstRowName; |
| 453 | importObjectNames = (importObjectNames && (rowType == QJsonValue::Object)); |
| 454 | |
| 455 | switch (containerType) { |
| 456 | case JsonFilter::DataContainerType::Array: { |
| 457 | QJsonArray arr = m_preparedDoc.array(); |
| 458 | int count = arr.count(); |
| 459 | |
| 460 | if (count < startRow) |
| 461 | return false; |
| 462 | |
| 463 | int endRowOffset = (endRow == -1 || endRow > count) ? count : endRow; |
| 464 | firstRow = *(arr.begin() + (startRow - 1)); |
| 465 | for (QJsonArray::iterator it = arr.begin() + (startRow - 1); it != arr.begin() + endRowOffset; ++it) { |
| 466 | if (checkRow(*it, countCols) != 0) |
| 467 | return false; |
| 468 | countRows++; |
| 469 | } |
| 470 | break; |
| 471 | } |