-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serializer.cpp
150 lines (128 loc) · 4.35 KB
/
Serializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "Serializer.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QJsonArray>
#include <QJsonObject>
Serializer::Serializer()
{}
void Serializer::loadTxt(const QString &fileName, QVector<QString>* paths){
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
qDebug() << "line: " << line;
}
}
void Serializer::saveTxt(const QString &fileName, const QVector<QString> *paths){
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file);
for(QString path: (*paths)){
out << path;
}
}
QJsonDocument Serializer::loadJson(const QString& fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::ReadOnly);
return QJsonDocument().fromJson(jsonFile.readAll());
}
void Serializer::saveJson(QJsonDocument& document, const QString& fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
void Serializer::modifyJsonValue(QJsonValue& destValue,
const QString& path,
const QJsonValue& newValue)
{
const int indexOfDot = path.indexOf('.');
const QString dotPropertyName = path.left(indexOfDot);
const QString dotSubPath = indexOfDot > 0 ? path.mid(indexOfDot + 1) : QString();
const int indexOfSquareBracketOpen = path.indexOf('[');
const int indexOfSquareBracketClose = path.indexOf(']');
const int arrayIndex = path.mid(indexOfSquareBracketOpen + 1, indexOfSquareBracketClose - indexOfSquareBracketOpen - 1).toInt();
const QString squareBracketPropertyName = path.left(indexOfSquareBracketOpen);
const QString squareBracketSubPath = indexOfSquareBracketClose > 0 ? (path.mid(indexOfSquareBracketClose + 1)[0] == '.' ? path.mid(indexOfSquareBracketClose + 2) : path.mid(indexOfSquareBracketClose + 1)) : QString();
// determine what is first in path. dot or bracket
bool useDot = true;
if (indexOfDot >= 0) // there is a dot in path
{
if (indexOfSquareBracketOpen >= 0) // there is squarebracket in path
{
if (indexOfDot > indexOfSquareBracketOpen)
useDot = false;
else
useDot = true;
}
else
useDot = true;
}
else
{
if (indexOfSquareBracketOpen >= 0)
useDot = false;
else
useDot = true; // acutally, id doesn't matter, both dot and square bracket don't exist
}
QString usedPropertyName = useDot ? dotPropertyName : squareBracketPropertyName;
QString usedSubPath = useDot ? dotSubPath : squareBracketSubPath;
QJsonValue subValue;
if (destValue.isArray())
subValue = destValue.toArray()[usedPropertyName.toInt()];
else if (destValue.isObject())
subValue = destValue.toObject()[usedPropertyName];
else
qDebug() << "oh, what should i do now with the following value?! " << destValue;
if(usedSubPath.isEmpty())
{
subValue = newValue;
}
else
{
if (subValue.isArray())
{
QJsonArray arr = subValue.toArray();
QJsonValue arrEntry = arr[arrayIndex];
modifyJsonValue(arrEntry,usedSubPath,newValue);
arr[arrayIndex] = arrEntry;
subValue = arr;
}
else if (subValue.isObject())
modifyJsonValue(subValue,usedSubPath,newValue);
else
subValue = newValue;
}
if (destValue.isArray())
{
QJsonArray arr = destValue.toArray();
arr[arrayIndex] = subValue;
destValue = arr;
}
else if (destValue.isObject())
{
QJsonObject obj = destValue.toObject();
obj[usedPropertyName] = subValue;
destValue = obj;
}
else
destValue = newValue;
}
void Serializer::modifyJsonValue(QJsonDocument& doc,
const QString& path,
const QJsonValue& newValue)
{
QJsonValue val;
if (doc.isArray())
val = doc.array();
else
val = doc.object();
modifyJsonValue(val,path,newValue);
if (val.isArray())
doc = QJsonDocument(val.toArray());
else
doc = QJsonDocument(val.toObject());
}