This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: JSON module in standard library behaves incorrectly on input like a psutil constant
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: joepie91, r.david.murray
Priority: normal Keywords:

Created on 2013-03-07 12:06 by joepie91, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg183653 - (view) Author: Sven Slootweg (joepie91) Date: 2013-03-07 12:06
When faced with a subclass of int like a psutil constant (such as for process status), that actually returns a non-numeric string when used with str(), the JSON module will serialize it as a string without quotes.

An example... Code (Python):

[...] "key": psutil._common.constant(0, "value"), "otherkey": "real string"  [...]

Output (JSON):

[...] "key": value, "otherkey": "real string" [...]

Due to the missing quotes around 'value', the JSON data is corrupted.

The cause appears to be that the JSON module, when faced with an int (or subclass thereof), will use the string representation in the serialized data, which would normally be the integer as a string. This example uses a psutil constant, but this would occur with any int subclass that doesn't return itself as string representation.

I'm not entirely sure how to resolve this issue or whether it can be resolved at all, as all possible solutions appear to have negative side-effects for more common cases. I'd imagine that checking whether the resulting string is numeric, besides being error-prone, would also cause unreasonable overhead during serialization.
msg183664 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-07 14:17
Right, the appropriate thing to do there is to write a custom encoder/decoder to handle those objects.  json only automatically handles types that work like the fundamental types, and this is a good thing, security-wise :)
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61579
2013-03-07 14:17:34r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg183664

resolution: not a bug
stage: resolved
2013-03-07 12:06:14joepie91create