File indexing completed on 2024-05-12 05:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2011 Raphael Kubo da Costa <rakuco@FreeBSD.org>
0003 
0004     SPDX-License-Identifier: BSD-2-Clause
0005 */
0006 
0007 #include "jsonparser.h"
0008 #include "archiveinterface.h"
0009 
0010 #include <QDebug>
0011 #include <QJsonDocument>
0012 
0013 JSONParser::JSONParser()
0014 {
0015 }
0016 
0017 JSONParser::~JSONParser()
0018 {
0019 }
0020 
0021 JSONParser::JSONArchive JSONParser::parse(QIODevice *json)
0022 {
0023     QJsonParseError error;
0024     QJsonDocument jsonDoc = QJsonDocument::fromJson(json->readAll(), &error);
0025 
0026     if (error.error != QJsonParseError::NoError) {
0027         qDebug() << "Parse error: " << error.errorString();
0028         return JSONParser::JSONArchive();
0029     }
0030 
0031     return createJSONArchive(jsonDoc.toVariant());
0032 }
0033 
0034 JSONParser::JSONArchive JSONParser::createJSONArchive(const QVariant &json)
0035 {
0036     JSONParser::JSONArchive archive;
0037 
0038     const auto jsonList = json.toList();
0039     for (const QVariant &entry : jsonList) {
0040         const QVariantMap entryMap = entry.toMap();
0041 
0042         if (!entryMap.contains(QStringLiteral("fullPath"))) {
0043             continue;
0044         }
0045 
0046         Kerfuffle::Archive::Entry *e = new Kerfuffle::Archive::Entry();
0047 
0048         QVariantMap::const_iterator entryIterator = entryMap.constBegin();
0049         for (; entryIterator != entryMap.constEnd(); ++entryIterator) {
0050             const QByteArray key = entryIterator.key().toUtf8();
0051             if (e->property(key.constData()).isValid()) {
0052                 e->setProperty(key.constData(), entryIterator.value());
0053             } else {
0054                 qDebug() << entryIterator.key() << "is not a valid entry key";
0055             }
0056         }
0057 
0058         const QString fullPath = entryMap[QStringLiteral("fullPath")].toString();
0059         archive[fullPath] = e;
0060     }
0061 
0062     return archive;
0063 }