diff --git a/srsly/_json_api.py b/srsly/_json_api.py index e755217..5a8a200 100644 --- a/srsly/_json_api.py +++ b/srsly/_json_api.py @@ -34,8 +34,9 @@ def json_loads(data: Union[str, bytes]) -> JSONOutput: data (str / bytes): The data to deserialize. RETURNS: The deserialized Python object. """ - # Avoid transforming the string '-' into the int '0' - if data == "-": + # Avoid transforming a bare '-' into the int '0'. ujson does this for both + # str and bytes input, so the guard must cover bytes too. + if data == "-" or data == b"-": raise ValueError("Expected object or value") return ujson.loads(data) diff --git a/srsly/tests/test_json_api.py b/srsly/tests/test_json_api.py index 85f0164..ff8d8ad 100644 --- a/srsly/tests/test_json_api.py +++ b/srsly/tests/test_json_api.py @@ -206,6 +206,16 @@ def test_json_loads_raises(obj): json_loads(obj) +@pytest.mark.parametrize( + "obj", + [b"-", "-"], +) +def test_json_loads_bare_minus_raises(obj): + # ujson decodes a bare "-" as 0; the guard must cover bytes too. + with pytest.raises(ValueError): + json_loads(obj) + + def test_unsupported_type_error(): with pytest.raises(TypeError, match="is not JSON serializable"): s = json_dumps({1, 2})