Skip to content

Commit 01e178d

Browse files
committed
v0.12.1 - Fix #94 (bad indentation for custom encoders)
Issue #94 was filed to address a case where if you were using a custom subclass of json5.JSON5Encoder, the default dumps() indentation logic would indent a custom value too much.
1 parent e3439cd commit 01e178d

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ $ git push --tags origin
114114

115115
## Version History / Release Notes
116116

117-
* v0.12.1.dev0 (dev version)
118-
* No changes yet
117+
* v0.12.1 (2025-08-12)
118+
* Fix [#94](https://github.com/dpranke/pyjson5/issues/94), where objects
119+
returned from a custom encoder were not being indented properly.
119120

120121
* v0.12.0 (2025-04-03)
121122
* Roll back pyproject.toml change for licensing so that we can still

‎json5/lib.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def _encode_non_basic_type(self, obj, seen: Set, level: int) -> str:
803803
elif hasattr(obj, '__getitem__') and hasattr(obj, '__iter__'):
804804
s = self._encode_array(obj, seen, level + 1)
805805
else:
806-
s = self.encode(self.default(obj), seen, level + 1, as_key=False)
806+
s = self.encode(self.default(obj), seen, level, as_key=False)
807807
assert s is not None
808808

809809
if self.check_circular:

‎json5/version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '0.12.1.dev0'
15+
__version__ = '0.12.1'
1616

1717
# For backward-compatibility with earlier versions of json5:
1818
VERSION = __version__

‎tests/lib_test.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,24 @@ def __len__(self):
515515

516516
self.assertEqual(json5.dumps(MyDict()), '{a: 1, b: 2}')
517517

518+
def test_custom_encoder(self):
519+
class Test:
520+
pass
521+
522+
class Encoder(json5.JSON5Encoder):
523+
def default(self, obj):
524+
assert isinstance(obj, Test)
525+
return {'foo': 'bar'}
526+
527+
# This tests that the custom value is indented at the same level
528+
# that a regular object would be. This is also testing that
529+
# Encoder.default is only being called when we don't otherwise know
530+
# how to encode something.
531+
self.assertEqual(
532+
'{\n baz: {\n foo: "bar",\n },\n quux: 4,\n}',
533+
json5.dumps({'baz': Test(), 'quux': 4}, indent=4, cls=Encoder),
534+
)
535+
518536
def test_custom_strings(self):
519537
class MyStr(str):
520538
pass

0 commit comments

Comments
 (0)