Typing Stubs for simplejson
types-simplejson is a stub package providing static type-checking annotations for the 'simplejson' library. It enables type checkers like MyPy and Pyright to analyze code that uses simplejson for JSON serialization and deserialization. This package is part of the broader typeshed project and currently aims to provide accurate annotations for simplejson version 3.20.*.
Warnings
- gotcha The `types-simplejson` package provides *only* type-checking stubs. For actual runtime functionality, you *must* also install the `simplejson` library separately.
- gotcha Do not attempt to import directly from `types_simplejson`. The imports for your code should always reference the `simplejson` library itself.
- gotcha Version compatibility between `types-simplejson` and the runtime `simplejson` library is crucial for accurate type checking. This stub package aims to provide accurate annotations for `simplejson==3.20.*`.
- breaking The `simplejson` library (version 3.19.1) introduced a breaking change by setting the default value of the `allow_nan` parameter in `dumps` and `dump` to `False`.
- gotcha Typeshed stubs, including `types-simplejson`, are built and tested against specific Python versions. This package requires Python >=3.10 and is validated against Python versions 3.10 to 3.14.
Install
-
pip install types-simplejson -
pip install simplejson
Imports
- simplejson
import simplejson
- JSONDecodeError
from simplejson import JSONDecodeError
Quickstart
import simplejson
from typing import Any, Dict, Union
def serialize_and_deserialize(data: Dict[str, Union[str, int, bool]]) -> Dict[str, Any]:
"""
Serializes a dictionary to a JSON string and then deserializes it.
"""
json_string: str = simplejson.dumps(data, indent=2, sort_keys=True)
print(f"Serialized JSON:\n{json_string}")
deserialized_data: Dict[str, Any] = simplejson.loads(json_string)
print(f"Deserialized data: {deserialized_data}")
return deserialized_data
if __name__ == "__main__":
my_data = {"name": "Alice", "age": 30, "is_student": False}
result = serialize_and_deserialize(my_data)
assert result == my_data