Skip to content

Commit

Permalink
untag without object_hook (#5382)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Jan 15, 2024
2 parents c275573 + 700fc7d commit 5a48a0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Unreleased

- Correct type for ``path`` argument to ``send_file``. :issue:`5230`
- Fix a typo in an error message for the ``flask run --key`` option. :pr:`5344`
- Session data is untagged without relying on the built-in ``json.loads``
``object_hook``. This allows other JSON providers that don't implement that.
:issue:`5381`


Version 3.0.0
Expand Down
14 changes: 13 additions & 1 deletion src/flask/json/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,22 @@ def untag(self, value: dict[str, t.Any]) -> t.Any:

return self.tags[key].to_python(value[key])

def _untag_scan(self, value: t.Any) -> t.Any:
if isinstance(value, dict):
# untag each item recursively
value = {k: self._untag_scan(v) for k, v in value.items()}
# untag the dict itself
value = self.untag(value)
elif isinstance(value, list):
# untag each item recursively
value = [self._untag_scan(item) for item in value]

return value

def dumps(self, value: t.Any) -> str:
"""Tag the value and dump it to a compact JSON string."""
return dumps(self.tag(value), separators=(",", ":"))

def loads(self, value: str) -> t.Any:
"""Load data from a JSON string and deserialized any tagged objects."""
return loads(value, object_hook=self.untag)
return self._untag_scan(loads(value))

0 comments on commit 5a48a0f

Please sign in to comment.