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.

Title: json.loads should be idempotent when the argument is a dictionary
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: CT Radu, eric.smith, r.david.murray
Priority: normal Keywords:

Created on 2015-06-05 12:21 by CT Radu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg244864 - (view) Author: CT Radu (CT Radu) Date: 2015-06-05 12:21
Currently, json.loads expects a string as input and is expected to return a dictionary or raise an exception.

If the first argument of json.loads is a dictionary
return that dictionary, instead of raising a TypeError as it happens now.

There are some use cases where json.loads would be applied multiple times to the same object. Once the string has been parsed once, it shouldn't be reloaded.
msg244865 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-06-05 12:28
What are the use cases where json.loads would be called on the return from json.loads?

It seems to me a better design would be to wrap such calls with a test, instead of having json.loads do this test. Hiding it in json.loads would mask other programming errors, for instance if you pass an unrelated dict to json.loads.
msg244867 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-05 13:23
Agreed that this is not a good API change.  If this makes sense for a particular program it is easy to write a wrapper, while the current behavior is much more useful for typical programs.  loads is not a "coercion" style call, it is specifically parsing a string, and should fail if passed something it can't parse as a string.  Another argument along these lines is that if you call int() on something, you know you get back an int.  If you call json.loads and it returns a dictionary unmodified, you do *not* know if you have something that is valid json data or not (that is, it might contain un-jsonifiable objects).
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68575
2015-06-05 13:23:30r.david.murraysetstatus: open -> closed

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

resolution: rejected
stage: resolved
2015-06-05 12:28:35eric.smithsetnosy: + eric.smith

messages: + msg244865
versions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5
2015-06-05 12:21:48CT Raducreate