Skip to content

Commit a21a336

Browse files
committed
Moved JSONEncoder to separate file
1 parent 4f18aca commit a21a336

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

flask_jwt_extended/config.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import json
21
from datetime import datetime
32
from datetime import timedelta
43
from datetime import timezone
5-
from typing import Any
64
from typing import Iterable
75
from typing import List
86
from typing import Optional
@@ -11,37 +9,12 @@
119
from typing import Union
1210

1311
from flask import current_app
14-
from flask.json.provider import _default
1512
from jwt.algorithms import requires_cryptography
1613

14+
from flask_jwt_extended.json_encoder import JSONEncoder
1715
from flask_jwt_extended.typing import ExpiresDelta
1816

1917

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-
4518
class _Config(object):
4619
"""
4720
Helper object for accessing and verifying options in this extension. This

flask_jwt_extended/json_encoder.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
from typing import Any
3+
4+
5+
try:
6+
# Flask 2.2 deprecated the flask.json.JSONEncoder (see below), lets recreate
7+
# a class with the same semantics as the old JSONEncoder.
8+
9+
from flask.json.provider import DefaultJSONProvider
10+
11+
class JSONEncoder(json.JSONEncoder):
12+
"""The default JSON encoder. Handles extra types compared to the
13+
built-in :class:`json.JSONEncoder`.
14+
15+
- :class:`datetime.datetime` and :class:`datetime.date` are
16+
serialized to :rfc:`822` strings. This is the same as the HTTP
17+
date format.
18+
- :class:`decimal.Decimal` is serialized to a string.
19+
- :class:`uuid.UUID` is serialized to a string.
20+
- :class:`dataclasses.dataclass` is passed to
21+
:func:`dataclasses.asdict`.
22+
- :class:`~markupsafe.Markup` (or any object with a ``__html__``
23+
method) will call the ``__html__`` method to get a string.
24+
25+
"""
26+
27+
def default(self, o: Any) -> Any:
28+
"""Convert ``o`` to a JSON serializable type. See
29+
:meth:`json.JSONEncoder.default`. Python does not support
30+
overriding how basic types like ``str`` or ``list`` are
31+
serialized, they are handled before this method.
32+
"""
33+
return DefaultJSONProvider.default(o)
34+
35+
except ModuleNotFoundError: # pragma: no cover
36+
from flask.json import JSONEncoder # type: ignore # noqa: F401

flask_jwt_extended/tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from typing import Union
1111

1212
import jwt
13-
from flask.json import JSONEncoder
1413

1514
from flask_jwt_extended.exceptions import CSRFError
1615
from flask_jwt_extended.exceptions import JWTDecodeError
16+
from flask_jwt_extended.json_encoder import JSONEncoder
1717
from flask_jwt_extended.typing import ExpiresDelta
1818

1919

0 commit comments

Comments
 (0)