|
| 1 | +import json |
1 | 2 | from datetime import datetime |
2 | 3 | from datetime import timedelta |
3 | 4 | from datetime import timezone |
| 5 | +from typing import Any |
4 | 6 | from typing import Iterable |
5 | 7 | from typing import List |
6 | 8 | from typing import Optional |
|
9 | 11 | from typing import Union |
10 | 12 |
|
11 | 13 | from flask import current_app |
12 | | -from flask.json import JSONEncoder |
| 14 | +from flask.json.provider import _default |
13 | 15 | from jwt.algorithms import requires_cryptography |
14 | 16 |
|
15 | 17 | from flask_jwt_extended.typing import ExpiresDelta |
16 | 18 |
|
17 | 19 |
|
| 20 | +class JSONEncoder(json.JSONEncoder): |
| 21 | + """The default JSON encoder. Handles extra types compared to the |
| 22 | + built-in :class:`json.JSONEncoder`. |
| 23 | +
|
| 24 | + - :class:`datetime.datetime` and :class:`datetime.date` are |
| 25 | + serialized to :rfc:`822` strings. This is the same as the HTTP |
| 26 | + date format. |
| 27 | + - :class:`decimal.Decimal` is serialized to a string. |
| 28 | + - :class:`uuid.UUID` is serialized to a string. |
| 29 | + - :class:`dataclasses.dataclass` is passed to |
| 30 | + :func:`dataclasses.asdict`. |
| 31 | + - :class:`~markupsafe.Markup` (or any object with a ``__html__`` |
| 32 | + method) will call the ``__html__`` method to get a string. |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + def default(self, o: Any) -> Any: |
| 37 | + """Convert ``o`` to a JSON serializable type. See |
| 38 | + :meth:`json.JSONEncoder.default`. Python does not support |
| 39 | + overriding how basic types like ``str`` or ``list`` are |
| 40 | + serialized, they are handled before this method. |
| 41 | + """ |
| 42 | + return _default(o) |
| 43 | + |
| 44 | + |
18 | 45 | class _Config(object): |
19 | 46 | """ |
20 | 47 | Helper object for accessing and verifying options in this extension. This |
@@ -284,7 +311,7 @@ def error_msg_key(self) -> str: |
284 | 311 |
|
285 | 312 | @property |
286 | 313 | def json_encoder(self) -> Type[JSONEncoder]: |
287 | | - return current_app.json_encoder |
| 314 | + return JSONEncoder |
288 | 315 |
|
289 | 316 | @property |
290 | 317 | def decode_audience(self) -> Union[str, Iterable[str]]: |
|
0 commit comments