TaggedJSONSerializer Class — flask Architecture
Architecture documentation for the TaggedJSONSerializer class in tag.py from the flask codebase.
Entity Profile
Dependency Diagram
graph TD 6ef56ba4_aac6_67ac_0434_7a801bf6f137["TaggedJSONSerializer"] 5e2eed77_f23f_513d_2d00_d39ce63b14ab["tag.py"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|defined in| 5e2eed77_f23f_513d_2d00_d39ce63b14ab bb2c6a99_7ada_6fdf_fe45_2791c31d0510["__init__()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| bb2c6a99_7ada_6fdf_fe45_2791c31d0510 231a3803_dc71_1586_3edd_5eef3c43f0df["register()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| 231a3803_dc71_1586_3edd_5eef3c43f0df 1c0b69f5_4295_77b6_d206_95b0d9e9c0a0["tag()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| 1c0b69f5_4295_77b6_d206_95b0d9e9c0a0 5e0479cd_0c5f_ecbf_9522_f2277fd8a3fb["untag()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| 5e0479cd_0c5f_ecbf_9522_f2277fd8a3fb 3897b196_2f09_a4be_dc0e_bffd5d28351a["_untag_scan()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| 3897b196_2f09_a4be_dc0e_bffd5d28351a 08d97a25_bf2d_8b1a_4141_99db7d245fd4["dumps()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| 08d97a25_bf2d_8b1a_4141_99db7d245fd4 f576c56b_44d3_0e30_77a6_1155c29edada["loads()"] 6ef56ba4_aac6_67ac_0434_7a801bf6f137 -->|method| f576c56b_44d3_0e30_77a6_1155c29edada
Relationship Graph
Source Code
src/flask/json/tag.py lines 219–327
class TaggedJSONSerializer:
"""Serializer that uses a tag system to compactly represent objects that
are not JSON types. Passed as the intermediate serializer to
:class:`itsdangerous.Serializer`.
The following extra types are supported:
* :class:`dict`
* :class:`tuple`
* :class:`bytes`
* :class:`~markupsafe.Markup`
* :class:`~uuid.UUID`
* :class:`~datetime.datetime`
"""
__slots__ = ("tags", "order")
#: Tag classes to bind when creating the serializer. Other tags can be
#: added later using :meth:`~register`.
default_tags = [
TagDict,
PassDict,
TagTuple,
PassList,
TagBytes,
TagMarkup,
TagUUID,
TagDateTime,
]
def __init__(self) -> None:
self.tags: dict[str, JSONTag] = {}
self.order: list[JSONTag] = []
for cls in self.default_tags:
self.register(cls)
def register(
self,
tag_class: type[JSONTag],
force: bool = False,
index: int | None = None,
) -> None:
"""Register a new tag with this serializer.
:param tag_class: tag class to register. Will be instantiated with this
serializer instance.
:param force: overwrite an existing tag. If false (default), a
:exc:`KeyError` is raised.
:param index: index to insert the new tag in the tag order. Useful when
the new tag is a special case of an existing tag. If ``None``
(default), the tag is appended to the end of the order.
:raise KeyError: if the tag key is already registered and ``force`` is
not true.
"""
tag = tag_class(self)
key = tag.key
if key:
if not force and key in self.tags:
raise KeyError(f"Tag '{key}' is already registered.")
self.tags[key] = tag
if index is None:
self.order.append(tag)
else:
self.order.insert(index, tag)
def tag(self, value: t.Any) -> t.Any:
"""Convert a value to a tagged representation if necessary."""
for tag in self.order:
if tag.check(value):
return tag.tag(value)
return value
def untag(self, value: dict[str, t.Any]) -> t.Any:
"""Convert a tagged representation back to the original type."""
if len(value) != 1:
Domain
Defined In
Source
Frequently Asked Questions
What is the TaggedJSONSerializer class?
TaggedJSONSerializer is a class in the flask codebase, defined in src/flask/json/tag.py.
Where is TaggedJSONSerializer defined?
TaggedJSONSerializer is defined in src/flask/json/tag.py at line 219.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free