Serialize and sign ``session`` into a cookie value.
(self, session: Session)
| 196 | # -- serialization --------------------------------------------------- |
| 197 | |
| 198 | def dumps(self, session: Session) -> str: |
| 199 | """Serialize and sign ``session`` into a cookie value.""" |
| 200 | envelope: dict = {"d": dict(session)} |
| 201 | if self.max_age is not None: |
| 202 | envelope["exp"] = int(time.time()) + self.max_age |
| 203 | try: |
| 204 | payload = json.dumps(envelope, separators=(",", ":")).encode("utf-8") |
| 205 | except TypeError as exc: |
| 206 | raise TypeError(f"Session data must be JSON-serializable to fit in a signed cookie: {exc}") from exc |
| 207 | return self._signer.sign(payload) |
| 208 | |
| 209 | def loads(self, raw: str) -> Session: |
| 210 | """Verify and decode a cookie value into a :class:`Session` (empty if invalid/expired).""" |