Reputation: 2466
The official docs of the json package state:
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)¶
Changed in version 3.6: s can now be of type bytes or bytearray. The input encoding should be UTF-8, UTF-16 or UTF-32.
Changed in version 3.9: The keyword argument encoding has been removed.
What was the purpose of the 'encoding' keyword prior to Python 3.9? Why was the encoding keyword removed? Is json.loads() (3.9+) capable of automatically detecting bytes and bytearray input encoding from UTF-8, UTF-16 and UTF-32?
Reference: https://docs.python.org/3/library/json.html
Upvotes: 3
Views: 2657
Reputation: 46
Is json.loads() (3.9+) capable of automatically detecting bytes and bytearray input encoding from UTF-8, UTF-16 and UTF-32?
Yes -- if you look at the json package source code, json.loads()
calls detect_encoding()
, which, appropriately, detects input encoding. Check out the code for detect_encoding()
here.
Upvotes: 3