diff --git a/json5/lib.py b/json5/lib.py index c7273d3..91c2b46 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -335,7 +335,7 @@ def parse( s = s.decode(encoding) if not s: - raise ValueError('Empty strings are not legal JSON5') + return None, 'Empty strings are not legal JSON5', 0 start = start or 0 parser = Parser(s, '', pos=start) ast, err, pos = parser.parse( diff --git a/tests/lib_test.py b/tests/lib_test.py index df83ffd..161928a 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -533,6 +533,14 @@ def check(self, s, obj, err, pos, consume_trailing=True, start=0): self.assertEqual(err, actual_err) self.assertEqual(pos, actual_pos) + def test_parse_empty_input(self): + for source in ('', b''): + with self.subTest(source=source): + self.assertEqual( + json5.parse(source), + (None, 'Empty strings are not legal JSON5', 0), + ) + def test_parse(self): self.check('4', 4, err=None, pos=1) self.check('4 ', 4, err=None, pos=2)