-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathJsonUtils.cpp
261 lines (222 loc) · 6.35 KB
/
JsonUtils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//project include
#include <utils/JsonUtils.h>
// util includes
#include <utils/jsonschema/QJsonSchemaChecker.h>
//qt includes
#include <QRegularExpression>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QStringList>
namespace JsonUtils {
QPair<bool, QStringList> readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError)
{
QJsonValue value(obj);
QPair<bool, QStringList> result = readFile(path, value,log, ignError);
obj = value.toObject();
return result;
}
QPair<bool, QStringList> readFile(const QString& path, QJsonValue& obj, Logger* log, bool ignError)
{
QString data;
if(!FileUtils::readFile(path, data, log, ignError))
{
return qMakePair(false, QStringList(QString("Error reading file: %1").arg(path)));
}
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, obj, log);
return parsingResult;
}
bool readSchema(const QString& path, QJsonObject& obj, Logger* log)
{
QJsonObject schema;
if(!readFile(path, schema, log).first)
{
return false;
}
if(!resolveRefs(schema, obj, log))
{
return false;
}
return true;
}
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log)
{
QJsonValue value(obj);
QPair<bool, QStringList> result = JsonUtils::parse(path, data, value, log);
obj = value.toObject();
return result;
}
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonArray& arr, Logger* log)
{
QJsonValue value(arr);
QPair<bool, QStringList> result = JsonUtils::parse(path, data, value, log);
arr = value.toArray();
return result;
}
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonValue& value, Logger* log)
{
QJsonDocument doc;
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, doc, log);
value = doc.object();
return parsingResult;
}
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonDocument& doc, Logger* log)
{
QStringList errorList;
QJsonParseError error;
doc = QJsonDocument::fromJson(data.toUtf8(), &error);
if (error.error != QJsonParseError::NoError)
{
qsizetype errorLine = 1;
qsizetype errorColumn = 1;
qsizetype const lastNewlineIndex = data.lastIndexOf("\n", error.offset - 1);
if (lastNewlineIndex != -1)
{
errorColumn = error.offset - lastNewlineIndex ;
}
errorLine += data.left(error.offset).count('\n');
const QString errorMessage = QString("JSON parse error @(%1): %2, line: %3, column: %4, Data: '%5'")
.arg(path, error.errorString())
.arg(errorLine)
.arg(errorColumn)
.arg(data);
errorList.push_back(errorMessage);
Error(log, "%s", QSTRING_CSTR(errorMessage));
return qMakePair(false, errorList);
}
return qMakePair(true, errorList);
}
QPair<bool, QStringList> validate(const QString& file, const QJsonValue& json, const QString& schemaPath, Logger* log)
{
// get the schema data
QJsonObject schema;
QPair<bool, QStringList> readResult = readFile(schemaPath, schema, log);
if(!readResult.first)
{
return readResult;
}
QPair<bool, QStringList> validationResult = validate(file, json, schema, log);
return validationResult;
}
QPair<bool, QStringList> validate(const QString& file, const QJsonValue& json, const QJsonObject& schema, Logger* log)
{
QStringList errorList;
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schema);
if (!schemaChecker.validate(json).first)
{
const QStringList &errors = schemaChecker.getMessages();
for (const auto& error : errors)
{
QString const errorMessage = QString("JSON parse error @(%1) - %2")
.arg(file, error);
errorList.push_back(errorMessage);
Error(log, "%s", QSTRING_CSTR(errorMessage));
}
return qMakePair(false, errorList);
}
return qMakePair(true, errorList);
}
QPair<bool, QStringList> correct(const QString& file, QJsonValue& json, const QJsonObject& schema, Logger* log)
{
bool wasCorrected {false};
QStringList corrections;
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schema);
if (!schemaChecker.validate(json).first)
{
Warning(log, "Fixing JSON data!");
json = schemaChecker.getAutoCorrectedConfig(json);
wasCorrected = true;
const QStringList &correctionMessages = schemaChecker.getMessages();
for (const auto& correction : correctionMessages)
{
QString const message = QString("JSON fix @(%1) - %2")
.arg(file, correction);
corrections.push_back(message);
Warning(log, "%s", QSTRING_CSTR(message));
}
}
return qMakePair(wasCorrected, corrections);
}
bool write(const QString& filename, const QJsonObject& json, Logger* log)
{
QJsonDocument doc;
doc.setObject(json);
QByteArray const data = doc.toJson(QJsonDocument::Indented);
return FileUtils::writeFile(filename, data, log);
}
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log)
{
for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
{
QString const attribute = i.key();
const QJsonValue & attributeValue = *i;
if (attribute == "$ref" && attributeValue.isString())
{
if(!readSchema(":/" + attributeValue.toString(), obj, log))
{
Error(log,"Error while getting schema ref: %s",QSTRING_CSTR(QString(":/" + attributeValue.toString())));
return false;
}
}
else if (attributeValue.isObject())
{
obj.insert(attribute, resolveRefs(attributeValue.toObject(), obj, log));
}
else
{
obj.insert(attribute, attributeValue);
}
}
return true;
}
QString jsonValueToQString(const QJsonValue &value, QJsonDocument::JsonFormat format)
{
switch (value.type()) {
case QJsonValue::Object:
{
return QJsonDocument(value.toObject()).toJson(format);
}
case QJsonValue::Array:
{
return QJsonDocument(value.toArray()).toJson(format);
}
case QJsonValue::String:
{
return value.toString();
}
case QJsonValue::Double:
{
return QString::number(value.toDouble());
}
case QJsonValue::Bool:
{
return value.toBool() ? "true" : "false";
}
case QJsonValue::Null:
{
return "null";
}
default:
break;
}
return QString();
}
QJsonObject mergeJsonObjects(const QJsonObject &obj1, const QJsonObject &obj2, bool overrideObj1)
{
QJsonObject result = obj1;
for (auto it = obj2.begin(); it != obj2.end(); ++it) {
if (result.contains(it.key())) {
if (overrideObj1)
{
result[it.key()] = it.value();
}
} else {
result[it.key()] = it.value();
}
}
return result;
}
};