GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-29863: Add json.COMPACT constant by andrewnester · Pull Request #72 · python/cpython

bpo-29863: Add json.COMPACT constant #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrewnester wants to merge 3 commits into python:master from andrewnester:29540-json-dumps-compact

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-29863: Add json.COMPACT constant #72

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
cc0a1f1
29540 - Added json.COMPACT constant
andrewnester Feb 13, 2017
32c4b13
Updated documentation for json.COMPACT
andrewnester Feb 13, 2017
4315176
Added test to ensure JSONEncoder works with json.COMPACT.
andrewnester Feb 14, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

17 changes: 14 additions & 3 deletions 17 Doc/library/json.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -170,7 +170,7 @@ 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``.
Expand Down Expand Up @@ -283,6 +283,17 @@ 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
---------------------

Expand Down Expand Up @@ -448,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``.
Expand Down
5 changes: 4 additions & 1 deletion 5 Lib/json/__init__.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -99,6 +99,7 @@
__all__ = [
'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
'COMPACT',
]

__author__ = 'Bob Ippolito <[email protected]>'
Expand All @@ -107,6 +108,8 @@
from .encoder import JSONEncoder
import codecs

COMPACT = (',', ':')

_default_encoder = JSONEncoder(
skipkeys=False,
ensure_ascii=True,
Expand Down
9 changes: 9 additions & 0 deletions 9 Lib/test/test_json/test_dump.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ 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"}')

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

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.