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
| #include "Json.h"
#include <QDebug> #include <QFile> #include <QTextStream> #include <QRegularExpression> #include <QJsonParseError>
struct JsonPrivate { JsonPrivate(const QString &jsonOrJsonFilePath, bool fromFile);
void remove(QJsonObject &parent, const QString &path); void setValue(QJsonObject &parent, const QString &path, const QJsonValue &newValue); QJsonValue getValue(const QString &path, const QJsonObject &fromNode) const;
QJsonObject root; QJsonDocument doc; bool valid = true; QString errorString; };
JsonPrivate::JsonPrivate(const QString &jsonOrJsonFilePath, bool fromFile) { QByteArray json("{}");
if (fromFile) { QFile file(jsonOrJsonFilePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { json = file.readAll(); } else { valid = false; errorString = QString("Cannot open the file: %1").arg(jsonOrJsonFilePath); qDebug() << errorString; return; } } else { json = jsonOrJsonFilePath.toUtf8(); }
QJsonParseError error; doc = QJsonDocument::fromJson(json, &error);
if (QJsonParseError::NoError == error.error) { root = doc.object(); } else { valid = false; errorString = QString("%1\nOffset: %2").arg(error.errorString()).arg(error.offset); qDebug() << errorString; } }
void JsonPrivate::remove(QJsonObject &parent, const QString &path) { const int indexOfDot = path.indexOf('.'); const QString property = path.left(indexOfDot); const QString restPath = (indexOfDot>0) ? path.mid(indexOfDot+1) : QString();
if(restPath.isEmpty()) { parent.remove(property); } else { QJsonObject child = parent[property].toObject(); remove(child, restPath); parent[property] = child; } }
void JsonPrivate::setValue(QJsonObject &parent, const QString &path, const QJsonValue &newValue) { const int indexOfDot = path.indexOf('.'); const QString property = path.left(indexOfDot); const QString restPath = (indexOfDot>0) ? path.mid(indexOfDot+1) : QString();
QJsonValue fieldValue = parent[property];
if(restPath.isEmpty()) { parent[property] = newValue; } else { QJsonObject child = parent[property].toObject(); setValue(child, restPath, newValue); parent[property] = child; } }
QJsonValue JsonPrivate::getValue(const QString &path, const QJsonObject &fromNode) const {
QJsonObject parent = fromNode.isEmpty() ? root : fromNode; QStringList names = path.split(QRegularExpression("\\."));
int size = names.size(); for (int i = 0; i < size - 1; ++i) { if (parent.isEmpty()) { return QJsonValue(); }
parent = parent.value(names.at(i)).toObject(); }
return parent.value(names.last()); }
Json::Json(const QString &jsonOrJsonFilePath, bool fromFile) : d(new JsonPrivate(jsonOrJsonFilePath, fromFile)) { }
Json::~Json() { delete d; }
bool Json::isValid() const { return d->valid; }
QString Json::errorString() const { return d->errorString; }
int Json::getInt(const QString &path, int def, const QJsonObject &fromNode) const { return getJsonValue(path, fromNode).toInt(def); }
bool Json::getBool(const QString &path, bool def, const QJsonObject &fromNode) const { return getJsonValue(path, fromNode).toBool(def); }
double Json::getDouble(const QString &path, double def, const QJsonObject &fromNode) const { return getJsonValue(path, fromNode).toDouble(def); }
QString Json::getString(const QString &path, const QString &def, const QJsonObject &fromNode) const { return getJsonValue(path, fromNode).toString(def); }
QStringList Json::getStringList(const QString &path, const QJsonObject &fromNode) const { QStringList result; QJsonArray array = getJsonValue(path, fromNode).toArray();
for (QJsonArray::const_iterator iter = array.begin(); iter != array.end(); ++iter) { QJsonValue value = *iter; result << value.toString(); }
return result; }
QJsonArray Json::getJsonArray(const QString &path, const QJsonObject &fromNode) const { if (("." == path || "" == path) && fromNode.isEmpty()) { return d->doc.array(); }
return getJsonValue(path, fromNode).toArray(); }
QJsonObject Json::getJsonObject(const QString &path, const QJsonObject &fromNode) const { return getJsonValue(path, fromNode).toObject(); }
QJsonValue Json::getJsonValue(const QString &path, const QJsonObject &fromNode) const { return d->getValue(path, fromNode); }
void Json::set(const QString &path, const QJsonValue &value) { d->setValue(d->root, path, value); }
void Json::set(const QString &path, const QStringList &strings) { QJsonArray array;
for (const QString &str : strings) { array.append(str); }
d->setValue(d->root, path, array); }
void Json::remove(const QString &path) { d->remove(d->root, path); }
void Json::save(const QString &path, bool pretty) const { QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { return; }
QTextStream out(&file); out << toString(pretty); out.flush(); file.close(); }
QString Json::toString(bool pretty) const { return QJsonDocument(d->root).toJson(pretty ? QJsonDocument::Indented : QJsonDocument::Compact); }
|