From b33aabe6b4daba80287fef944957afea86a443fb Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Fri, 27 Mar 2020 00:04:17 +0100 Subject: [PATCH] Handle serialization of empty strings as keys Fixes #37. --- json5/lib.py | 2 +- tests/lib_test.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/json5/lib.py b/json5/lib.py index d588005..5b2b0ae 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -439,7 +439,7 @@ def _dump_str(obj, ensure_ascii): def _is_ident(k): k = str(k) - if not _is_id_start(k[0]) and k[0] not in (u'$', u'_'): + if not k or not _is_id_start(k[0]) and k[0] not in (u'$', u'_'): return False for ch in k[1:]: if not _is_id_continue(ch) and ch not in (u'$', u'_'): diff --git a/tests/lib_test.py b/tests/lib_test.py index 511f172..9651545 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -422,6 +422,9 @@ def test_supplemental_unicode(self): # we can't test this there. pass + def test_empty_key(self): + self.assertEqual(json5.dumps({'': 'value'}), '{"": "value"}') + if __name__ == '__main__': # pragma: no cover unittest.main()