| 39 | } |
| 40 | |
| 41 | SerializationResult ChatSerializer::loadFromFile(ChatModel *model, const QString &filePath) |
| 42 | { |
| 43 | QFile file(filePath); |
| 44 | if (!file.open(QIODevice::ReadOnly)) { |
| 45 | return {false, QString("Failed to open file for reading: %1").arg(filePath)}; |
| 46 | } |
| 47 | |
| 48 | QJsonParseError error; |
| 49 | QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error); |
| 50 | if (error.error != QJsonParseError::NoError) { |
| 51 | return {false, QString("JSON parse error: %1").arg(error.errorString())}; |
| 52 | } |
| 53 | |
| 54 | QJsonObject root = doc.object(); |
| 55 | QString version = root["version"].toString(); |
| 56 | |
| 57 | if (!validateVersion(version)) { |
| 58 | return {false, QString("Unsupported version: %1").arg(version)}; |
| 59 | } |
| 60 | |
| 61 | if (!deserializeChat(model, root, filePath)) { |
| 62 | return {false, "Failed to deserialize chat data"}; |
| 63 | } |
| 64 | |
| 65 | return {true, QString()}; |
| 66 | } |
| 67 | |
| 68 | QJsonObject ChatSerializer::serializeMessage( |
| 69 | const ChatModel::Message &message, const QString &chatFilePath) |