You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/config_serialization.rst
+34-1Lines changed: 34 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,41 @@ Serialization
5
5
6
6
Flask-session versions below 1.0.0 use pickle serialization (or fallback) for session storage. While not a direct vulnerability, it is a potential security risk. If you are using a version below 1.0.0, it is recommended to upgrade to the latest version as soon as it's available.
7
7
8
-
From 0.7.0 the serializer is msgspec, which is configurable using ``SESSION_SERIALIZATION_FORMAT``. The default format is ``'msgpack'`` which has 30% storage reduction compared to ``'json'``. The ``'json'`` format may be helpful for debugging, easier viewing or compatibility. Switching between the two should be seamless, even for existing sessions.
8
+
From 0.7.0 the serializer is msgspec. The format it uses is configurable with ``SESSION_SERIALIZATION_FORMAT``. The default format is ``'msgpack'`` which has 30% storage reduction compared to ``'json'``. The ``'json'`` format may be helpful for debugging, easier viewing or compatibility. Switching between the two should be seamless, even for existing sessions.
9
9
10
10
All sessions that are accessed or modified while using 0.7.0 will convert to a msgspec format. Once using 1.0.0, any sessions that are still in pickle format will be cleared upon access.
11
11
12
12
The msgspec library has speed and memory advantages over other libraries. However, if you want to use a different library (such as pickle or orjson), you can override the :attr:`session_interface.serializer`.
13
+
14
+
If you encounter a TypeError such as: "Encoding objects of type <type> is unsupported", you may be attempting to serialize an unsupported type. In this case, you can either convert the object to a supported type or use a different serializer.
15
+
16
+
Casting to a supported type:
17
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
+
19
+
.. code-block:: python
20
+
21
+
session["status"] =str(LazyString('done')))
22
+
23
+
24
+
.. note::
25
+
26
+
Flask's flash method uses the session to store messages so you must also pass supported types to the flash method.
27
+
28
+
29
+
For a detailed list of supported types by the msgspec serializer, please refer to the official documentation at `msgspec supported types <https://jcristharif.com/msgspec/supported-types.html>`_.
30
+
31
+
Overriding the serializer:
32
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
+
34
+
.. code-block:: python
35
+
36
+
from flask_session import Session
37
+
import orjson
38
+
39
+
app = Flask(__name__)
40
+
Session(app)
41
+
42
+
# Override the serializer
43
+
app.session_interface.serializer = orjson
44
+
45
+
Any serializer that has a ``dumps`` and ``loads`` method can be used.
0 commit comments