provider.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. from __future__ import annotations
  2. import dataclasses
  3. import decimal
  4. import json
  5. import typing as t
  6. import uuid
  7. import weakref
  8. from datetime import date
  9. from werkzeug.http import http_date
  10. from ..globals import request
  11. if t.TYPE_CHECKING: # pragma: no cover
  12. from ..app import Flask
  13. from ..wrappers import Response
  14. class JSONProvider:
  15. """A standard set of JSON operations for an application. Subclasses
  16. of this can be used to customize JSON behavior or use different
  17. JSON libraries.
  18. To implement a provider for a specific library, subclass this base
  19. class and implement at least :meth:`dumps` and :meth:`loads`. All
  20. other methods have default implementations.
  21. To use a different provider, either subclass ``Flask`` and set
  22. :attr:`~flask.Flask.json_provider_class` to a provider class, or set
  23. :attr:`app.json <flask.Flask.json>` to an instance of the class.
  24. :param app: An application instance. This will be stored as a
  25. :class:`weakref.proxy` on the :attr:`_app` attribute.
  26. .. versionadded:: 2.2
  27. """
  28. def __init__(self, app: Flask) -> None:
  29. self._app = weakref.proxy(app)
  30. def dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
  31. """Serialize data as JSON.
  32. :param obj: The data to serialize.
  33. :param kwargs: May be passed to the underlying JSON library.
  34. """
  35. raise NotImplementedError
  36. def dump(self, obj: t.Any, fp: t.IO[str], **kwargs: t.Any) -> None:
  37. """Serialize data as JSON and write to a file.
  38. :param obj: The data to serialize.
  39. :param fp: A file opened for writing text. Should use the UTF-8
  40. encoding to be valid JSON.
  41. :param kwargs: May be passed to the underlying JSON library.
  42. """
  43. fp.write(self.dumps(obj, **kwargs))
  44. def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any:
  45. """Deserialize data as JSON.
  46. :param s: Text or UTF-8 bytes.
  47. :param kwargs: May be passed to the underlying JSON library.
  48. """
  49. raise NotImplementedError
  50. def load(self, fp: t.IO[t.AnyStr], **kwargs: t.Any) -> t.Any:
  51. """Deserialize data as JSON read from a file.
  52. :param fp: A file opened for reading text or UTF-8 bytes.
  53. :param kwargs: May be passed to the underlying JSON library.
  54. """
  55. return self.loads(fp.read(), **kwargs)
  56. def _prepare_response_obj(
  57. self, args: t.Tuple[t.Any, ...], kwargs: t.Dict[str, t.Any]
  58. ) -> t.Any:
  59. if args and kwargs:
  60. raise TypeError("app.json.response() takes either args or kwargs, not both")
  61. if not args and not kwargs:
  62. return None
  63. if len(args) == 1:
  64. return args[0]
  65. return args or kwargs
  66. def response(self, *args: t.Any, **kwargs: t.Any) -> Response:
  67. """Serialize the given arguments as JSON, and return a
  68. :class:`~flask.Response` object with the ``application/json``
  69. mimetype.
  70. The :func:`~flask.json.jsonify` function calls this method for
  71. the current application.
  72. Either positional or keyword arguments can be given, not both.
  73. If no arguments are given, ``None`` is serialized.
  74. :param args: A single value to serialize, or multiple values to
  75. treat as a list to serialize.
  76. :param kwargs: Treat as a dict to serialize.
  77. """
  78. obj = self._prepare_response_obj(args, kwargs)
  79. return self._app.response_class(self.dumps(obj), mimetype="application/json")
  80. def _default(o: t.Any) -> t.Any:
  81. if isinstance(o, date):
  82. return http_date(o)
  83. if isinstance(o, (decimal.Decimal, uuid.UUID)):
  84. return str(o)
  85. if dataclasses and dataclasses.is_dataclass(o):
  86. return dataclasses.asdict(o)
  87. if hasattr(o, "__html__"):
  88. return str(o.__html__())
  89. raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
  90. class DefaultJSONProvider(JSONProvider):
  91. """Provide JSON operations using Python's built-in :mod:`json`
  92. library. Serializes the following additional data types:
  93. - :class:`datetime.datetime` and :class:`datetime.date` are
  94. serialized to :rfc:`822` strings. This is the same as the HTTP
  95. date format.
  96. - :class:`uuid.UUID` is serialized to a string.
  97. - :class:`dataclasses.dataclass` is passed to
  98. :func:`dataclasses.asdict`.
  99. - :class:`~markupsafe.Markup` (or any object with a ``__html__``
  100. method) will call the ``__html__`` method to get a string.
  101. """
  102. default: t.Callable[[t.Any], t.Any] = staticmethod(
  103. _default
  104. ) # type: ignore[assignment]
  105. """Apply this function to any object that :meth:`json.dumps` does
  106. not know how to serialize. It should return a valid JSON type or
  107. raise a ``TypeError``.
  108. """
  109. ensure_ascii = True
  110. """Replace non-ASCII characters with escape sequences. This may be
  111. more compatible with some clients, but can be disabled for better
  112. performance and size.
  113. """
  114. sort_keys = True
  115. """Sort the keys in any serialized dicts. This may be useful for
  116. some caching situations, but can be disabled for better performance.
  117. When enabled, keys must all be strings, they are not converted
  118. before sorting.
  119. """
  120. compact: bool | None = None
  121. """If ``True``, or ``None`` out of debug mode, the :meth:`response`
  122. output will not add indentation, newlines, or spaces. If ``False``,
  123. or ``None`` in debug mode, it will use a non-compact representation.
  124. """
  125. mimetype = "application/json"
  126. """The mimetype set in :meth:`response`."""
  127. def dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
  128. """Serialize data as JSON to a string.
  129. Keyword arguments are passed to :func:`json.dumps`. Sets some
  130. parameter defaults from the :attr:`default`,
  131. :attr:`ensure_ascii`, and :attr:`sort_keys` attributes.
  132. :param obj: The data to serialize.
  133. :param kwargs: Passed to :func:`json.dumps`.
  134. """
  135. cls = self._app._json_encoder
  136. bp = self._app.blueprints.get(request.blueprint) if request else None
  137. if bp is not None and bp._json_encoder is not None:
  138. cls = bp._json_encoder
  139. if cls is not None:
  140. import warnings
  141. warnings.warn(
  142. "Setting 'json_encoder' on the app or a blueprint is"
  143. " deprecated and will be removed in Flask 2.3."
  144. " Customize 'app.json' instead.",
  145. DeprecationWarning,
  146. )
  147. kwargs.setdefault("cls", cls)
  148. if "default" not in cls.__dict__:
  149. kwargs.setdefault("default", self.default)
  150. else:
  151. kwargs.setdefault("default", self.default)
  152. ensure_ascii = self._app.config["JSON_AS_ASCII"]
  153. sort_keys = self._app.config["JSON_SORT_KEYS"]
  154. if ensure_ascii is not None:
  155. import warnings
  156. warnings.warn(
  157. "The 'JSON_AS_ASCII' config key is deprecated and will"
  158. " be removed in Flask 2.3. Set 'app.json.ensure_ascii'"
  159. " instead.",
  160. DeprecationWarning,
  161. )
  162. else:
  163. ensure_ascii = self.ensure_ascii
  164. if sort_keys is not None:
  165. import warnings
  166. warnings.warn(
  167. "The 'JSON_SORT_KEYS' config key is deprecated and will"
  168. " be removed in Flask 2.3. Set 'app.json.sort_keys'"
  169. " instead.",
  170. DeprecationWarning,
  171. )
  172. else:
  173. sort_keys = self.sort_keys
  174. kwargs.setdefault("ensure_ascii", ensure_ascii)
  175. kwargs.setdefault("sort_keys", sort_keys)
  176. return json.dumps(obj, **kwargs)
  177. def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any:
  178. """Deserialize data as JSON from a string or bytes.
  179. :param s: Text or UTF-8 bytes.
  180. :param kwargs: Passed to :func:`json.loads`.
  181. """
  182. cls = self._app._json_decoder
  183. bp = self._app.blueprints.get(request.blueprint) if request else None
  184. if bp is not None and bp._json_decoder is not None:
  185. cls = bp._json_decoder
  186. if cls is not None:
  187. import warnings
  188. warnings.warn(
  189. "Setting 'json_decoder' on the app or a blueprint is"
  190. " deprecated and will be removed in Flask 2.3."
  191. " Customize 'app.json' instead.",
  192. DeprecationWarning,
  193. )
  194. kwargs.setdefault("cls", cls)
  195. return json.loads(s, **kwargs)
  196. def response(self, *args: t.Any, **kwargs: t.Any) -> Response:
  197. """Serialize the given arguments as JSON, and return a
  198. :class:`~flask.Response` object with it. The response mimetype
  199. will be "application/json" and can be changed with
  200. :attr:`mimetype`.
  201. If :attr:`compact` is ``False`` or debug mode is enabled, the
  202. output will be formatted to be easier to read.
  203. Either positional or keyword arguments can be given, not both.
  204. If no arguments are given, ``None`` is serialized.
  205. :param args: A single value to serialize, or multiple values to
  206. treat as a list to serialize.
  207. :param kwargs: Treat as a dict to serialize.
  208. """
  209. obj = self._prepare_response_obj(args, kwargs)
  210. dump_args: t.Dict[str, t.Any] = {}
  211. pretty = self._app.config["JSONIFY_PRETTYPRINT_REGULAR"]
  212. mimetype = self._app.config["JSONIFY_MIMETYPE"]
  213. if pretty is not None:
  214. import warnings
  215. warnings.warn(
  216. "The 'JSONIFY_PRETTYPRINT_REGULAR' config key is"
  217. " deprecated and will be removed in Flask 2.3. Set"
  218. " 'app.json.compact' instead.",
  219. DeprecationWarning,
  220. )
  221. compact: bool | None = not pretty
  222. else:
  223. compact = self.compact
  224. if (compact is None and self._app.debug) or compact is False:
  225. dump_args.setdefault("indent", 2)
  226. else:
  227. dump_args.setdefault("separators", (",", ":"))
  228. if mimetype is not None:
  229. import warnings
  230. warnings.warn(
  231. "The 'JSONIFY_MIMETYPE' config key is deprecated and"
  232. " will be removed in Flask 2.3. Set 'app.json.mimetype'"
  233. " instead.",
  234. DeprecationWarning,
  235. )
  236. else:
  237. mimetype = self.mimetype
  238. return self._app.response_class(
  239. f"{self.dumps(obj, **dump_args)}\n", mimetype=mimetype
  240. )