Index: Doc/library/json.rst =================================================================== --- Doc/library/json.rst (revision 71408) +++ Doc/library/json.rst (working copy) @@ -137,10 +137,11 @@ ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). - 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. ``None`` (the default) selects the most compact - representation. + If *indent* is a non-negative integer or string, then JSON array elements and + object members will be pretty-printed with that indent level. An indent level + of 0 or ``""`` will only insert newlines. ``None`` (the default) selects the + most compact representation. Using an integer indent indents that many spaces + per level. Using a string indent uses this string for each level. If *separators* is an ``(item_separator, dict_separator)`` tuple, then it will be used instead of the default ``(', ', ': ')`` separators. ``(',', Index: Lib/json/tests/test_indent.py =================================================================== --- Lib/json/tests/test_indent.py (revision 71408) +++ Lib/json/tests/test_indent.py (working copy) @@ -10,32 +10,36 @@ expect = textwrap.dedent("""\ [ - [ - "blorpie" - ], - [ - "whoops" - ], - [], - "d-shtaeou", - "d-nthiouh", - "i-vhbjkhnth", - { - "nifty": 87 - }, - { - "field": "yes", - "morefield": false - } + \t[ + \t\t"blorpie" + \t], + \t[ + \t\t"whoops" + \t], + \t[], + \t"d-shtaeou", + \t"d-nthiouh", + \t"i-vhbjkhnth", + \t{ + \t\t"nifty": 87 + \t}, + \t{ + \t\t"field": "yes", + \t\t"morefield": false + \t} ]""") d1 = json.dumps(h) d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) + d3 = json.dumps(h, indent="\t", sort_keys=True, separators=(',', ': ')) h1 = json.loads(d1) h2 = json.loads(d2) + h3 = json.loads(d3) self.assertEquals(h1, h) self.assertEquals(h2, h) - self.assertEquals(d2, expect) + self.assertEquals(h3, h) + self.assertEquals(d2, expect.replace("\t", " ")) + self.assertEquals(d3, expect) Index: Lib/json/encoder.py =================================================================== --- Lib/json/encoder.py (revision 71408) +++ Lib/json/encoder.py (working copy) @@ -143,9 +143,9 @@ 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 or string, then JSON array elements and object members will be pretty-printed with that - indent level. An indent level of 0 will only insert newlines. + indent level. An indent level of 0 or "" will only insert newlines. None is the most compact representation. If specified, separators should be a (item_separator, key_separator) @@ -175,7 +175,10 @@ self.encoding = encoding def _newline_indent(self): - return '\n' + (' ' * (self.indent * self.current_indent_level)) + if isinstance(self.indent, str): + return '\n' + (self.indent * self.current_indent_level) + else: + return '\n' + (' ' * (self.indent * self.current_indent_level)) def _iterencode_list(self, lst, markers=None): if not lst: