| 102 | } |
| 103 | |
| 104 | bool UBTocJsonSerializer::save(const QVersionNumber& version, const QVector<QVariantMap>& toc) |
| 105 | { |
| 106 | QJsonArray list; |
| 107 | |
| 108 | for (const auto entry : toc) |
| 109 | { |
| 110 | list.append(QJsonObject::fromVariantMap(entry)); |
| 111 | } |
| 112 | |
| 113 | QJsonObject document; |
| 114 | document[VERSION] = version.toString(); |
| 115 | document[PAGES] = list; |
| 116 | |
| 117 | QJsonDocument doc{document}; |
| 118 | auto json = doc.toJson(QJsonDocument::Indented); |
| 119 | |
| 120 | // reduce indentation to one space |
| 121 | json.replace(" ", " "); |
| 122 | |
| 123 | QFile file{mPath + "/toc.json"}; |
| 124 | |
| 125 | if (!file.open(QFile::WriteOnly)) |
| 126 | { |
| 127 | qWarning() << "Cannot save TOC: Cannot open file" << file.fileName(); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | file.write(json); |
| 132 | file.close(); |
| 133 | return true; |
| 134 | } |