| 41 | } |
| 42 | |
| 43 | bool UBTocJsonSerializer::load(QVersionNumber& version, QVector<QVariantMap>& toc) |
| 44 | { |
| 45 | if (!toc.isEmpty()) |
| 46 | { |
| 47 | qWarning() << "Refused to load non-empty TOC"; |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | QFile file{mPath + "/toc.json"}; |
| 52 | |
| 53 | if (!file.open(QFile::ReadOnly)) |
| 54 | { |
| 55 | qDebug() << "Cannot load TOC: Cannot open file" << file.fileName(); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | const auto json = file.readAll(); |
| 60 | file.close(); |
| 61 | QJsonParseError error; |
| 62 | const auto jsonDoc = QJsonDocument::fromJson(json, &error); |
| 63 | |
| 64 | if (error.error != QJsonParseError::NoError) |
| 65 | { |
| 66 | qWarning() << "Parse error reading TOC:" << error.errorString() << "at" << error.offset; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (!jsonDoc.isObject()) |
| 71 | { |
| 72 | qWarning() << "Error reading TOC: JSON document is not an object"; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | version = QVersionNumber::fromString(jsonDoc.object().value(VERSION).toString()); |
| 77 | |
| 78 | const auto pages = jsonDoc.object().value(PAGES); |
| 79 | |
| 80 | if (!pages.isArray()) |
| 81 | { |
| 82 | qWarning() << "Error reading TOC: pages is not an array"; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | int index = 0; |
| 87 | |
| 88 | toc.resize(pages.toArray().size()); |
| 89 | |
| 90 | for (const auto entry : pages.toArray()) |
| 91 | { |
| 92 | if (!entry.isObject()) |
| 93 | { |
| 94 | qWarning() << "Error reading TOC: JSON entry is not an object"; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | toc[index++] = entry.toObject().toVariantMap(); |
| 99 | } |
| 100 | |