qjsonobject.sip 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // This is the SIP interface definition for the QJsonObject mapped type.
  2. //
  3. // Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of PyQt6.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. %MappedType QJsonObject
  20. /TypeHint="Dict[QString, QJsonValue]", TypeHintValue="{}"/
  21. {
  22. %TypeHeaderCode
  23. #include <qjsonobject.h>
  24. %End
  25. %ConvertFromTypeCode
  26. PyObject *d = PyDict_New();
  27. if (!d)
  28. return 0;
  29. QJsonObject::const_iterator it = sipCpp->constBegin();
  30. QJsonObject::const_iterator end = sipCpp->constEnd();
  31. while (it != end)
  32. {
  33. QString *k = new QString(it.key());
  34. PyObject *kobj = sipConvertFromNewType(k, sipType_QString,
  35. sipTransferObj);
  36. if (!kobj)
  37. {
  38. delete k;
  39. Py_DECREF(d);
  40. return 0;
  41. }
  42. QJsonValue *v = new QJsonValue(it.value());
  43. PyObject *vobj = sipConvertFromNewType(v, sipType_QJsonValue,
  44. sipTransferObj);
  45. if (!vobj)
  46. {
  47. delete v;
  48. Py_DECREF(kobj);
  49. Py_DECREF(d);
  50. return 0;
  51. }
  52. int rc = PyDict_SetItem(d, kobj, vobj);
  53. Py_DECREF(vobj);
  54. Py_DECREF(kobj);
  55. if (rc < 0)
  56. {
  57. Py_DECREF(d);
  58. return 0;
  59. }
  60. ++it;
  61. }
  62. return d;
  63. %End
  64. %ConvertToTypeCode
  65. if (!sipIsErr)
  66. return PyDict_Check(sipPy);
  67. QJsonObject *jo = new QJsonObject;
  68. Py_ssize_t pos = 0;
  69. PyObject *kobj, *vobj;
  70. while (PyDict_Next(sipPy, &pos, &kobj, &vobj))
  71. {
  72. int kstate;
  73. QString *k = reinterpret_cast<QString *>(
  74. sipForceConvertToType(kobj, sipType_QString, sipTransferObj,
  75. SIP_NOT_NONE, &kstate, sipIsErr));
  76. if (*sipIsErr)
  77. {
  78. PyErr_Format(PyExc_TypeError,
  79. "a key has type '%s' but 'str' is expected",
  80. sipPyTypeName(Py_TYPE(kobj)));
  81. delete jo;
  82. return 0;
  83. }
  84. int vstate;
  85. QJsonValue *v = reinterpret_cast<QJsonValue *>(
  86. sipForceConvertToType(vobj, sipType_QJsonValue, sipTransferObj,
  87. SIP_NOT_NONE, &vstate, sipIsErr));
  88. if (*sipIsErr)
  89. {
  90. PyErr_Format(PyExc_TypeError,
  91. "a value has type '%s' but 'QJsonValue' is expected",
  92. sipPyTypeName(Py_TYPE(vobj)));
  93. sipReleaseType(k, sipType_QString, kstate);
  94. delete jo;
  95. return 0;
  96. }
  97. jo->insert(*k, *v);
  98. sipReleaseType(v, sipType_QJsonValue, vstate);
  99. sipReleaseType(k, sipType_QString, kstate);
  100. }
  101. *sipCppPtr = jo;
  102. return sipGetState(sipTransferObj);
  103. %End
  104. };