_json.py 918 B

12345678910111213141516171819202122232425262728293031323334
  1. import json as _json
  2. import typing as _t
  3. from types import ModuleType
  4. class _CompactJSON:
  5. """Wrapper around json module that strips whitespace."""
  6. @staticmethod
  7. def loads(payload: _t.Union[str, bytes]) -> _t.Any:
  8. return _json.loads(payload)
  9. @staticmethod
  10. def dumps(obj: _t.Any, **kwargs: _t.Any) -> str:
  11. kwargs.setdefault("ensure_ascii", False)
  12. kwargs.setdefault("separators", (",", ":"))
  13. return _json.dumps(obj, **kwargs)
  14. class DeprecatedJSON(ModuleType):
  15. def __getattribute__(self, item: str) -> _t.Any:
  16. import warnings
  17. warnings.warn(
  18. "Importing 'itsdangerous.json' is deprecated and will be"
  19. " removed in ItsDangerous 2.1. Use Python's 'json' module"
  20. " instead.",
  21. DeprecationWarning,
  22. stacklevel=2,
  23. )
  24. return getattr(_json, item)
  25. json = DeprecatedJSON("json")