Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. diff --git i/src/corelib/json/qjsonvalue.cpp w/src/corelib/json/qjsonvalue.cpp
  2. index 4b52014db1..e1b317ca79 100644
  3. --- i/src/corelib/json/qjsonvalue.cpp
  4. +++ w/src/corelib/json/qjsonvalue.cpp
  5. @@ -624,6 +624,31 @@ QJsonObject QJsonValue::toObject() const
  6. return toObject(QJsonObject());
  7. }
  8.  
  9. +QJsonValue QJsonValue::operator [](const QString &key) const
  10. +{
  11. + if (!isObject())
  12. + return QJsonValue(QJsonValue::Undefined);
  13. +
  14. + return toObject()[key];
  15. +}
  16. +
  17. +QJsonValue QJsonValue::operator[](int i) const
  18. +{
  19. + if (!isArray())
  20. + return QJsonValue(QJsonValue::Undefined);
  21. +
  22. + return toArray()[i];
  23. +}
  24. +
  25. +/*
  26. +QJsonValueRef QJsonValue::operator [](const QString &key)
  27. +{
  28. + //return QJsonObjectRef(this, key);
  29. +}*/
  30. +
  31. +
  32. +
  33. +
  34. /*!
  35. Returns \c true if the value is equal to \a other.
  36. */
  37. @@ -712,20 +737,62 @@ void QJsonValue::detach()
  38. However, they are not explicitly documented here.
  39. */
  40.  
  41. +struct QJsonValueRefData
  42. +{
  43. + QJsonValueRef *ref;
  44. + QString key;
  45. + int index;
  46. +};
  47. +
  48. +static const uintptr_t kJsonValueRefPointerTag = 0x1;
  49. +
  50. +QJsonValueRef::QJsonValueRef(QJsonValueRef *ref, const QString &key)
  51. + : a(nullptr), is_object(false), index(0)
  52. +{
  53. + p = quintptr(new QJsonValueRefData{ ref, key, -1 }) | kJsonValueRefPointerTag;
  54. +}
  55. +
  56. +QJsonValueRef::QJsonValueRef(QJsonValueRef *ref, int index)
  57. + : a(nullptr), is_object(false), index(0)
  58. +{
  59. + p = quintptr(new QJsonValueRefData{ ref, QString(), index }) | kJsonValueRefPointerTag;
  60. +}
  61. +
  62. +QJsonValueRef::~QJsonValueRef()
  63. +{
  64. + if (isReference())
  65. + delete reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
  66. +}
  67.  
  68. QJsonValueRef &QJsonValueRef::operator =(const QJsonValue &val)
  69. {
  70. - if (is_object)
  71. + if (isReference()) {
  72. + QJsonValueRefData *data = reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
  73. + qDebug() << __FUNCTION__ << "setting" << data->key << "for" << *data->ref << "to" << val;
  74. +
  75. + QJsonValue refVal = data->ref->toValue();
  76. + if (refVal.isObject()) {
  77. + QJsonObject object = refVal.toObject();
  78. + object[data->key] = val;
  79. + *(data->ref) = object;
  80. + } else {
  81. + QJsonArray array = refVal.toArray();
  82. + array[data->index] = val;
  83. + *(data->ref) = array;
  84. + }
  85. + } else if (is_object) {
  86. o->setValueAt(index, val);
  87. - else
  88. + } else {
  89. a->replace(index, val);
  90. -
  91. + }
  92. return *this;
  93. }
  94.  
  95. QJsonValueRef &QJsonValueRef::operator =(const QJsonValueRef &ref)
  96. {
  97. - if (is_object)
  98. + if (isReference())
  99. + qDebug() << __FUNCTION__ << "unimpl";
  100. + else if (is_object)
  101. o->setValueAt(index, ref);
  102. else
  103. a->replace(index, ref);
  104. @@ -750,11 +817,39 @@ QJsonObject QJsonValueRef::toObject() const
  105.  
  106. QJsonValue QJsonValueRef::toValue() const
  107. {
  108. + if (isReference()) {
  109. + QJsonValueRefData *data = reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
  110. + if (data->index >= 0) // FIXME, better mark
  111. + return data->ref->toValue()[data->index];
  112. + else
  113. + return data->ref->toValue()[data->key];
  114. + }
  115. +
  116. if (!is_object)
  117. return a->at(index);
  118. return o->valueAt(index);
  119. }
  120.  
  121. +QJsonValueRef QJsonValueRef::operator [](const QString &key)
  122. +{
  123. + return QJsonValueRef(this, key);
  124. +}
  125. +
  126. +QJsonValueRef QJsonValueRef::operator [](int i)
  127. +{
  128. + return QJsonValueRef(this, i);
  129. +}
  130. +
  131. +bool QJsonValueRef::isReference() const
  132. +{
  133. + return p & kJsonValueRefPointerTag;
  134. +}
  135. +
  136. +QJsonValueRef *QJsonValueRef::reference() const
  137. +{
  138. +
  139. +}
  140. +
  141. #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
  142. QDebug operator<<(QDebug dbg, const QJsonValue &o)
  143. {
  144. diff --git i/src/corelib/json/qjsonvalue.h w/src/corelib/json/qjsonvalue.h
  145. index a853acaddd..385f7b4855 100644
  146. --- i/src/corelib/json/qjsonvalue.h
  147. +++ w/src/corelib/json/qjsonvalue.h
  148. @@ -60,6 +60,8 @@ namespace QJsonPrivate {
  149. class Entry;
  150. }
  151.  
  152. +class QJsonValueRef;
  153. +
  154. class Q_CORE_EXPORT QJsonValue
  155. {
  156. public:
  157. @@ -114,6 +116,13 @@ public:
  158. QJsonObject toObject() const;
  159. QJsonObject toObject(const QJsonObject &defaultValue) const;
  160.  
  161. + QJsonValue operator[] (const QString &key) const;
  162. + QJsonValue operator[](int i) const;
  163. +
  164. + // QJsonValue operator[] (QLatin1String key) const;
  165. + // QJsonValueRef operator[] (const QString &key);
  166. + //QJsonValueRef operator[] (QLatin1String key);
  167. +
  168. bool operator==(const QJsonValue &other) const;
  169. bool operator!=(const QJsonValue &other) const;
  170.  
  171. @@ -149,6 +158,10 @@ public:
  172. QJsonValueRef(QJsonObject *object, int idx)
  173. : o(object), is_object(true), index(idx) {}
  174.  
  175. + QJsonValueRef(QJsonValueRef *ref, const QString &key);
  176. + QJsonValueRef(QJsonValueRef *ref, int index);
  177. + ~QJsonValueRef();
  178. +
  179. inline operator QJsonValue() const { return toValue(); }
  180. QJsonValueRef &operator = (const QJsonValue &val);
  181. QJsonValueRef &operator = (const QJsonValueRef &val);
  182. @@ -170,6 +183,9 @@ public:
  183. QJsonArray toArray() const;
  184. QJsonObject toObject() const;
  185.  
  186. + QJsonValueRef operator[] (const QString &key);
  187. + QJsonValueRef operator[](int i);
  188. +
  189. // ### Qt 6: Add default values
  190. inline bool toBool(bool defaultValue) const { return toValue().toBool(defaultValue); }
  191. inline int toInt(int defaultValue) const { return toValue().toInt(defaultValue); }
  192. @@ -181,11 +197,15 @@ public:
  193.  
  194. private:
  195. QJsonValue toValue() const;
  196. + bool isReference() const;
  197. + QJsonValueRef *reference() const;
  198.  
  199. union {
  200. QJsonArray *a;
  201. QJsonObject *o;
  202. + quintptr p;
  203. };
  204. +
  205. uint is_object : 1;
  206. uint index : 31;
  207. };
  208. diff --git i/tests/auto/corelib/json/json.qrc w/tests/auto/corelib/json/json.qrc
  209. index eb122a1779..8552c815b4 100644
  210. --- i/tests/auto/corelib/json/json.qrc
  211. +++ w/tests/auto/corelib/json/json.qrc
  212. @@ -3,6 +3,7 @@
  213. <file>bom.json</file>
  214. <file>test2.json</file>
  215. <file>test3.json</file>
  216. + <file>test4.json</file>
  217. <file>test.json</file>
  218. <file>test.bjson</file>
  219. </qresource>
  220. diff --git i/tests/auto/corelib/json/tst_qtjson.cpp w/tests/auto/corelib/json/tst_qtjson.cpp
  221. index b215364f0e..1fa85624f0 100644
  222. --- i/tests/auto/corelib/json/tst_qtjson.cpp
  223. +++ w/tests/auto/corelib/json/tst_qtjson.cpp
  224. @@ -145,6 +145,8 @@ private Q_SLOTS:
  225. void parseErrorOffset_data();
  226. void parseErrorOffset();
  227.  
  228. + void implicitObject();
  229. +
  230. private:
  231. QString testDataDir;
  232. };
  233. @@ -2908,5 +2910,61 @@ void tst_QtJson::parseErrorOffset()
  234. QCOMPARE(error.offset, errorOffset);
  235. }
  236.  
  237. +void tst_QtJson::implicitObject()
  238. +{
  239. + QFile file(testDataDir + "/test4.json");
  240. + file.open(QFile::ReadOnly);
  241. + QByteArray jsonData = file.readAll();
  242. + QVERIFY(!jsonData.isEmpty());
  243. +
  244. + QJsonDocument doc = QJsonDocument::fromJson(jsonData);
  245. + QVERIFY(!doc.isNull());
  246. +
  247. + QJsonObject docobj = doc.object();
  248. +
  249. + QJsonValueRef phoneNumber = docobj["phoneNumber"];
  250. +
  251. + qDebug() << docobj["phoneNumber"][1]["number"];
  252. + /*QJsonArray arr = phoneNumber.toArray();
  253. + QJsonArray array;
  254. + for (int i = 0; i < 10; ++i)
  255. + array.append((double)i);
  256. + phoneNumber = array;
  257. + qDebug() << phoneNumber;
  258. + qDebug() << arr;
  259. +*/
  260. + qDebug() << docobj["address"]["apartment"]["floor"];
  261. + QCOMPARE(docobj["address"]["apartment"]["floor"].toInt(), 4);
  262. +
  263. + qDebug() << docobj["address"]["apaasdasdrtment"]["floor"];
  264. +
  265. + docobj["address"]["apartment"]["floor"] = 42;
  266. +
  267. + qDebug() << docobj;
  268. +
  269. + docobj["address"]["asdasd"]["floor"] = 52;
  270. +
  271. + qDebug() << docobj;
  272. +
  273. + QCOMPARE(docobj["address"]["apartment"]["floor"].toInt(), 42);
  274. +
  275. + //qDebug() << doc["address"]["city"];
  276. +
  277. +// doc["address"]["city"] = "Oslo";
  278. +/*
  279. + QJsonValue addr = doc.object()["address"];
  280. + qDebug() << addr;
  281. +
  282. + QJsonObject obj = addr.toObject();
  283. + obj["postalCode"] = 123;
  284. + qDebug() << obj;
  285. + // doc["address"] = 52;
  286. +*/
  287. +
  288. + //obj["address"] = 42;
  289. +
  290. + // qDebug() << doc;
  291. +}
  292. +
  293. QTEST_MAIN(tst_QtJson)
  294. #include "tst_qtjson.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement