cpython: e9e1bf9ec2ac (original) (raw)

Mercurial > cpython

changeset 103565:e9e1bf9ec2ac

Issue #17909: Accept binary input in json.loads json.loads (and hence json.load) now support binary input encoded as UTF-8, UTF-16 or UTF-32. Patch by Serhiy Storchaka. [#17909]

Nick Coghlan ncoghlan@gmail.com
date Sat, 10 Sep 2016 20:16:18 +1000
parents cdc91b6ae3b2
children 3ded89cdea11
files Doc/library/json.rst Doc/whatsnew/3.6.rst Lib/json/__init__.py Lib/test/test_json/test_decode.py Lib/test/test_json/test_unicode.py Misc/NEWS
diffstat 6 files changed, 70 insertions(+), 16 deletions(-)[+] [-] Doc/library/json.rst 5 Doc/whatsnew/3.6.rst 8 Lib/json/__init__.py 50 Lib/test/test_json/test_decode.py 4 Lib/test/test_json/test_unicode.py 16 Misc/NEWS 3

line wrap: on

line diff

--- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -268,8 +268,9 @@ Basic Usage .. function:: loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

--- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -680,6 +680,14 @@ restriction that :class:importlib.machi[](#l2.3) :term:path-like object.[](#l2.4) [](#l2.5) [](#l2.6) +json[](#l2.7) +----[](#l2.8) +[](#l2.9) +:func:json.load and :func:json.loads now support binary input. Encoded[](#l2.10) +JSON should be represented using either UTF-8, UTF-16, or UTF-32.[](#l2.11) +(Contributed by Serhiy Storchaka in :issue:17909`.) + + os --

--- a/Lib/json/init.py +++ b/Lib/json/init.py @@ -105,6 +105,7 @@ Using json.tool from the shell to valida from .decoder import JSONDecoder, JSONDecodeError from .encoder import JSONEncoder +import codecs _default_encoder = JSONEncoder( skipkeys=False, @@ -240,6 +241,35 @@ def dumps(obj, *, skipkeys=False, ensure _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None) +def detect_encoding(b):

+

+ + def load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): """Deserialize fp (a .read()-supporting file-like object containing @@ -270,8 +300,8 @@ def load(fp, *, cls=None, object_hook=No def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):

object_hook is an optional function that will be called with the result of any object literal decode (a dict). The return value of @@ -307,12 +337,16 @@ def loads(s, *, encoding=None, cls=None, The encoding argument is ignored and deprecated. """

+ if (cls is None and object_hook is None and parse_int is None and parse_float is None and parse_constant is None and object_pairs_hook is None and not kw):

--- a/Lib/test/test_json/test_decode.py +++ b/Lib/test/test_json/test_decode.py @@ -72,10 +72,8 @@ class TestDecode: def test_invalid_input_type(self): msg = 'the JSON object must be str'

def test_string_with_utf8_bom(self): # see #18958

--- a/Lib/test/test_json/test_unicode.py +++ b/Lib/test/test_json/test_unicode.py @@ -1,3 +1,4 @@ +import codecs from collections import OrderedDict from test.test_json import PyTest, CTest @@ -52,9 +53,18 @@ class TestUnicode: self.assertRaises(TypeError, self.dumps, [b"hi"]) def test_bytes_decode(self):

-

def test_object_pairs_hook_with_unicode(self): s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -135,6 +135,9 @@ Core and Builtins Library ------- +- Issue #17909: json.load and json.loads now support binary input