QJsonDocument fails with a confusing error
Anton Yablokov <[email protected]> Wed, 10 Jun 2020 10:35:15 +0300
| Newsgroups | gmane.comp.python.pyqt-pykde |
|---|---|
| Message-ID | <CABrj0DPKXTykf0wE3tKcSrBcM=wY5dV7NYczdbn7NLnimN_8sA@mail.gmail.com> |
--000000000000070abb05a7b5e317
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Dear developers,
Consider the following piece of code:
from PyQt5.QtCore import QJsonDocument
json =3D {
"catalog": [
{
"version": None,
},
]}QJsonDocument(json)
Under Python 3.6+ and PyQt514+ (haven't checked other versions), it results
in the following error at the last line:
TypeError: a value has type 'list' but 'QJsonValue' is expected
QJsonDocument clearly supports lists: QJsonDocument({'a': []}) works fine.
As it turns out, the None value is the reason. Although the docs
<https://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum> clearly show
that QJsonDocument supports null values, None is not supported in
PyQt5: QJsonDocument({'a':
None}) results in
TypeError: a value has type 'NoneType' but 'QJsonValue' is expected.
Is it a bug that None is not translated as null? If not, the error message
should be more informative, at least.
Best regards,
Anton
=F0=9F=8D=8F
--000000000000070abb05a7b5e317
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><p>Dear developers,<br></p><p>Consider the following piece=
of code:</p>
<pre><code><span>from</span><span> </span><span>PyQt5</span><span>.</span><=
span>QtCore</span><span> </span><span>import</span><span> </span><span>QJso=
nDocument</span><span>
json </span><span>=3D</span><span> </span><span>{</span><span>
</span><span>"catalog"</span><span>:</span><span> </span><spa=
n>[</span><span>
</span><span>{</span><span>
</span><span>"version"</span><span>:</span><span> </s=
pan><span>None</span><span>,</span><span>
</span><span>},</span><span>
</span><span>]</span><span>
</span><span>}</span><span>
</span><span>QJsonDocument</span><span>(</span><span>json</span><span>)</sp=
an></code></pre>
<p>Under Python 3.6+ and PyQt514+ (haven't checked other versions), it =
results in the following error at the last line:</p>
<p><code>TypeError: a value has type 'list' but 'QJsonValue'=
; is expected</code></p>
<p><code>QJsonDocument</code> clearly supports lists: <code>QJsonDocument({=
'a': []})</code> works fine.</p><p>As it turns out, the <code>None<=
/code> value is the reason. Although the <a href=3D"https://doc.qt.io/qt-5/=
qjsondocument.html#JsonFormat-enum" rel=3D"nofollow noreferrer" target=3D"_=
blank">docs</a> clearly show that <code>QJsonDocument</code> supports <code=
>null</code> values, <code>None</code> is not supported in PyQt5: <code>QJs=
onDocument({'a': None})</code> results in</p>
<p><code>TypeError: a value has type 'NoneType' but 'QJsonValue=
' is expected</code>.</p><p>Is it a bug that <span style=3D"font-family=
:monospace">None</span> is not translated as <span style=3D"font-family:mon=
ospace">null</span>? If not, the error message should be more informative, =
at least.<br></p><p>Best regards,</p><p>Anton</p><div><div dir=3D"ltr" clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><=
div><div dir=3D"ltr"><div><div dir=3D"ltr"><font size=3D"6">=F0=9F=8D=8F</f=
ont><br></div></div></div></div></div></div></div></div>
--000000000000070abb05a7b5e317--