Skip to content
Snippets Groups Projects
Commit 7847c64f22ec authored by Bob Ippolito's avatar Bob Ippolito
Browse files

separators, encoding speedup, more test coverage for indent

git-svn-id: http://simplejson.googlecode.com/svn/trunk@35 a4795897-2c25-0410-b006-0d3caba88fa1
parent c480a37b14c8
No related branches found
No related tags found
No related merge requests found
......@@ -90,4 +90,6 @@
implementation (to raise ``TypeError``).
"""
__all__ = ['__init__', 'default', 'encode', 'iterencode']
item_separator = ', '
key_separator = ': '
def __init__(self, skipkeys=False, ensure_ascii=True,
......@@ -93,5 +95,6 @@
def __init__(self, skipkeys=False, ensure_ascii=True,
check_circular=True, allow_nan=True, sort_keys=False, indent=None):
check_circular=True, allow_nan=True, sort_keys=False,
indent=None, separators=None):
"""
Constructor for JSONEncoder, with sensible defaults.
......@@ -117,6 +120,6 @@
sorted by key; this is useful for regression tests to ensure
that JSON serializations can be compared on a day-to-day basis.
If ``indent`` is a non-negative integer, then JSON array
If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
indent level. An indent level of 0 will only insert newlines.
......@@ -121,6 +124,10 @@
elements and object members will be pretty-printed with that
indent level. An indent level of 0 will only insert newlines.
``None`` is the most compact representation.
None is the most compact representation.
If specified, separators should be a (item_separator, key_separator)
tuple. The default is (', ', ': '). To get the most compact JSON
representation you should specify (',', ':') to eliminate whitespace.
"""
self.skipkeys = skipkeys
......@@ -130,5 +137,7 @@
self.sort_keys = sort_keys
self.indent = indent
self.current_indent_level = 0
if separators is not None:
self.item_separator, self.key_separator = separators
def _newline_indent(self):
......@@ -133,7 +142,5 @@
def _newline_indent(self):
if self.indent is None:
return ''
return '\n' + (' ' * (self.indent * self.current_indent_level))
def _iterencode_list(self, lst, markers=None):
......@@ -145,5 +152,7 @@
if markerid in markers:
raise ValueError("Circular reference detected")
markers[markerid] = lst
yield '['
if self.indent is not None:
self.current_indent_level += 1
newline_indent = self._newline_indent()
......@@ -148,8 +157,12 @@
self.current_indent_level += 1
newline_indent = self._newline_indent()
yield '[' + newline_indent
separator = self.item_separator + newline_indent
yield newline_indent
else:
newline_indent = None
separator = self.item_separator
first = True
for value in lst:
if first:
first = False
else:
......@@ -151,8 +164,8 @@
first = True
for value in lst:
if first:
first = False
else:
yield ', ' + newline_indent
yield separator
for chunk in self._iterencode(value, markers):
yield chunk
......@@ -157,3 +170,4 @@
for chunk in self._iterencode(value, markers):
yield chunk
if newline_indent is not None:
self.current_indent_level -= 1
......@@ -159,5 +173,6 @@
self.current_indent_level -= 1
yield self._newline_indent() + ']'
yield self._newline_indent()
yield ']'
if markers is not None:
del markers[markerid]
......@@ -170,5 +185,8 @@
if markerid in markers:
raise ValueError("Circular reference detected")
markers[markerid] = dct
yield '{'
key_separator = self.key_separator
if self.indent is not None:
self.current_indent_level += 1
newline_indent = self._newline_indent()
......@@ -173,6 +191,10 @@
self.current_indent_level += 1
newline_indent = self._newline_indent()
yield '{' + newline_indent
item_separator = self.item_separator + newline_indent
yield newline_indent
else:
newline_indent = None
item_separator = self.item_separator
first = True
if self.ensure_ascii:
encoder = encode_basestring_ascii
......@@ -207,5 +229,5 @@
if first:
first = False
else:
yield ', ' + newline_indent
yield item_separator
yield encoder(key)
......@@ -211,4 +233,4 @@
yield encoder(key)
yield ': '
yield key_separator
for chunk in self._iterencode(value, markers):
yield chunk
......@@ -213,3 +235,4 @@
for chunk in self._iterencode(value, markers):
yield chunk
if newline_indent is not None:
self.current_indent_level -= 1
......@@ -215,5 +238,6 @@
self.current_indent_level -= 1
yield self._newline_indent() + '}'
yield self._newline_indent()
yield '}'
if markers is not None:
del markers[markerid]
......
......@@ -3,7 +3,8 @@
def test_indent():
import simplejson
import textwrap
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
......@@ -6,6 +7,27 @@
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
expect = textwrap.dedent("""\
[
[
"blorpie"
],
[
"whoops"
],
[],
"d-shtaeou",
"d-nthiouh",
"i-vhbjkhnth",
{
"nifty": 87
},
{
"field": "yes",
"morefield": false
}
]""")
d1 = simplejson.dumps(h)
......@@ -10,9 +32,9 @@
d1 = simplejson.dumps(h)
d2 = simplejson.dumps(h, indent=2)
d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
h1 = simplejson.loads(d1)
h2 = simplejson.loads(d2)
assert h1 == h
assert h2 == h
......@@ -13,6 +35,7 @@
h1 = simplejson.loads(d1)
h2 = simplejson.loads(d2)
assert h1 == h
assert h2 == h
assert d2 == expect
def test_separators():
import simplejson
import textwrap
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
expect = textwrap.dedent("""\
[
[
"blorpie"
] ,
[
"whoops"
] ,
[] ,
"d-shtaeou" ,
"d-nthiouh" ,
"i-vhbjkhnth" ,
{
"nifty" : 87
} ,
{
"field" : "yes" ,
"morefield" : false
}
]""")
d1 = simplejson.dumps(h)
d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))
h1 = simplejson.loads(d1)
h2 = simplejson.loads(d2)
assert h1 == h
assert h2 == h
assert d2 == expect
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment