From cc0a1f1062d7ab455801eb9ffe3acabe748f2c74 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Mon, 13 Feb 2017 18:46:02 +0300 Subject: [PATCH 1/3] 29540 - Added json.COMPACT constant --- Lib/json/__init__.py | 5 ++++- Lib/test/test_json/test_dump.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index b8d5e6cff8cb21..0b7037619dfa2d 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -30,7 +30,7 @@ >>> import json >>> from collections import OrderedDict >>> mydict = OrderedDict([('4', 5), ('6', 7)]) - >>> json.dumps([1,2,3,mydict], separators=(',', ':')) + >>> json.dumps([1,2,3,mydict], separators=json.COMPACT) '[1,2,3,{"4":5,"6":7}]' Pretty printing:: @@ -99,6 +99,7 @@ __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', + 'COMPACT', ] __author__ = 'Bob Ippolito ' @@ -107,6 +108,8 @@ from .encoder import JSONEncoder import codecs +COMPACT = (',', ':') + _default_encoder = JSONEncoder( skipkeys=False, ensure_ascii=True, diff --git a/Lib/test/test_json/test_dump.py b/Lib/test/test_json/test_dump.py index fd0d86b392cee9..3de6a9753210d2 100644 --- a/Lib/test/test_json/test_dump.py +++ b/Lib/test/test_json/test_dump.py @@ -47,6 +47,10 @@ def __lt__(self, o): d[1337] = "true.dat" self.assertEqual(self.dumps(d, sort_keys=True), '{"1337": "true.dat"}') + def test_compact_dump(self): + sio = StringIO() + self.json.dump({'name': 'some name', 'value': 'some value'}, sio, separators=self.json.COMPACT) + self.assertEqual(sio.getvalue(), '{"name":"some name","value":"some value"}') class TestPyDump(TestDump, PyTest): pass From 32c4b13ad5f456f719708849ab3931b1e349d457 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Mon, 13 Feb 2017 22:01:16 +0300 Subject: [PATCH 2/3] Updated documentation for json.COMPACT --- Doc/library/json.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Doc/library/json.rst b/Doc/library/json.rst index 8103c614aaf450..12b2de6ddbf136 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -43,7 +43,7 @@ Encoding basic Python object hierarchies:: Compact encoding:: >>> import json - >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':')) + >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=json.COMPACT) '[1,2,3,{"4":5,"6":7}]' Pretty printing:: @@ -175,6 +175,9 @@ Basic Usage .. versionchanged:: 3.4 Use ``(',', ': ')`` as default if *indent* is not ``None``. + .. versionchanged:: 3.7 + Instead of ``(',', ':')`` constant :data:`json.COMPACT` can be used. + If specified, *default* should be a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` @@ -283,6 +286,16 @@ Basic Usage input encoding should be UTF-8, UTF-16 or UTF-32. +Constants +^^^^^^^^^ + +.. data:: compact + + A constant that can be used as the *separators* argument to emit a compact serialization. + + .. versionadded:: 3.7 + + Encoders and Decoders --------------------- From 431517612f47d53200d0f0dd91badb7c0abefc05 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Tue, 14 Feb 2017 17:02:58 +0300 Subject: [PATCH 3/3] Added test to ensure JSONEncoder works with json.COMPACT. --- Doc/library/json.rst | 12 +++++------- Lib/test/test_json/test_dump.py | 5 +++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Doc/library/json.rst b/Doc/library/json.rst index 12b2de6ddbf136..e4ccc8095ba02f 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -170,14 +170,11 @@ Basic Usage If specified, *separators* should be an ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON representation, - you should specify ``(',', ':')`` to eliminate whitespace. + you should specify :attr:`json.COMPACT` to eliminate whitespace. .. versionchanged:: 3.4 Use ``(',', ': ')`` as default if *indent* is not ``None``. - .. versionchanged:: 3.7 - Instead of ``(',', ':')`` constant :data:`json.COMPACT` can be used. - If specified, *default* should be a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` @@ -289,9 +286,10 @@ Basic Usage Constants ^^^^^^^^^ -.. data:: compact +.. data:: COMPACT - A constant that can be used as the *separators* argument to emit a compact serialization. + A constant that can be used as the *separators* argument + to emit a compact serialization. .. versionadded:: 3.7 @@ -461,7 +459,7 @@ Encoders and Decoders If specified, *separators* should be an ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON representation, - you should specify ``(',', ':')`` to eliminate whitespace. + you should specify :attr:`json.COMPACT` to eliminate whitespace. .. versionchanged:: 3.4 Use ``(',', ': ')`` as default if *indent* is not ``None``. diff --git a/Lib/test/test_json/test_dump.py b/Lib/test/test_json/test_dump.py index 3de6a9753210d2..cdf0de18e612e8 100644 --- a/Lib/test/test_json/test_dump.py +++ b/Lib/test/test_json/test_dump.py @@ -52,6 +52,11 @@ def test_compact_dump(self): self.json.dump({'name': 'some name', 'value': 'some value'}, sio, separators=self.json.COMPACT) self.assertEqual(sio.getvalue(), '{"name":"some name","value":"some value"}') + def test_compact_encode(self): + encoder = self.json.JSONEncoder(separators=self.json.COMPACT) + encoded = encoder.encode({'name': 'some name', 'value': 'some value'}) + self.assertEqual(encoded, '{"name":"some name","value":"some value"}') + class TestPyDump(TestDump, PyTest): pass class TestCDump(TestDump, CTest): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy