1// This is the SIP interface definition for the QJsonArray mapped type.
2//
3// Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4//
5// This file is part of PyQt5.
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
20
21// Note that we assume any iterable can be converted to a QJsonArray.  However,
22// because QJsonValue is an iterable and QJsonObject is implemented as a dict
23// (which is also an iterable), then any overloads that handle one or more of
24// them must be ordered so that QJsonArray is checked last.
25
26%MappedType QJsonArray
27        /TypeHintIn="Iterable[QJsonValue]", TypeHintOut="List[QJsonValue]",
28        TypeHintValue="[]"/
29{
30%TypeHeaderCode
31#include <qjsonarray.h>
32%End
33
34%ConvertFromTypeCode
35    PyObject *l = PyList_New(sipCpp->size());
36
37    if (!l)
38        return 0;
39
40    for (int i = 0; i < sipCpp->size(); ++i)
41    {
42        QJsonValue *t = new QJsonValue(sipCpp->at(i));
43        PyObject *tobj = sipConvertFromNewType(t, sipType_QJsonValue,
44                sipTransferObj);
45
46        if (!tobj)
47        {
48            delete t;
49            Py_DECREF(l);
50
51            return 0;
52        }
53
54        PyList_SetItem(l, i, tobj);
55    }
56
57    return l;
58%End
59
60%ConvertToTypeCode
61    PyObject *iter = PyObject_GetIter(sipPy);
62
63    if (!sipIsErr)
64    {
65        PyErr_Clear();
66        Py_XDECREF(iter);
67
68        return (iter
69#if PY_MAJOR_VERSION < 3
70                && !PyString_Check(sipPy)
71#endif
72                && !PyUnicode_Check(sipPy));
73    }
74
75    if (!iter)
76    {
77        *sipIsErr = 1;
78
79        return 0;
80    }
81
82    QJsonArray *ql = new QJsonArray;
83
84    for (Py_ssize_t i = 0; ; ++i)
85    {
86        PyErr_Clear();
87        PyObject *itm = PyIter_Next(iter);
88
89        if (!itm)
90        {
91            if (PyErr_Occurred())
92            {
93                delete ql;
94                Py_DECREF(iter);
95                *sipIsErr = 1;
96
97                return 0;
98            }
99
100            break;
101        }
102
103        int state;
104        QJsonValue *t = reinterpret_cast<QJsonValue *>(
105                sipForceConvertToType(itm, sipType_QJsonValue, sipTransferObj,
106                        SIP_NOT_NONE, &state, sipIsErr));
107
108        if (*sipIsErr)
109        {
110            PyErr_Format(PyExc_TypeError,
111                    "index %zd has type '%s' but 'QJsonValue' is expected", i,
112                    sipPyTypeName(Py_TYPE(itm)));
113
114            Py_DECREF(itm);
115            delete ql;
116            Py_DECREF(iter);
117
118            return 0;
119        }
120
121        ql->append(*t);
122
123        sipReleaseType(t, sipType_QJsonValue, state);
124        Py_DECREF(itm);
125    }
126
127    Py_DECREF(iter);
128
129    *sipCppPtr = ql;
130
131    return sipGetState(sipTransferObj);
132%End
133};
134