Skip to main content

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.

Attributes​

AttributeTypeDescription
tagsdict[str, [JSONTag](jsontag.md?sid=src_flask_json_tag_jsontag)]A mapping of unique string keys to their corresponding JSONTag instances used for looking up deserialization logic.
orderlist[[JSONTag](jsontag.md?sid=src_flask_json_tag_jsontag)]An ordered list of JSONTag instances used to determine the sequence in which values are checked for tagging during serialization.
default_tagslist[type[[JSONTag](jsontag.md?sid=src_flask_json_tag_jsontag)]] = [TagDict, PassDict, TagTuple, PassList, TagBytes, TagMarkup, TagUUID, TagDateTime]Tag classes to bind when creating the serializer. Other tags can be added later using :meth:~register.

Constructor​

Signature​

def TaggedJSONSerializer() - > None

Methods​


register()​

@classmethod
def register(
tag_class: type[[JSONTag](jsontag.md?sid=src_flask_json_tag_jsontag)],
force: bool = False,
index: int | None = None
) - > None

Register a new tag with this serializer.

Parameters​

NameTypeDescription
tag_classtype[[JSONTag](jsontag.md?sid=src_flask_json_tag_jsontag)]tag class to register. Will be instantiated with this serializer instance.
forcebool = Falseoverwrite an existing tag. If false (default), a KeyError is raised.
index`intNone` = None

Returns​

TypeDescription
None

tag()​

@classmethod
def tag(
value: t.Any
) - > t.Any

Convert a value to a tagged representation if necessary.

Parameters​

NameTypeDescription
valuet.AnyThe Python object to be checked and potentially converted into a tagged format.

Returns​

TypeDescription
t.AnyThe tagged representation of the value, or the original value if no tag matches.

untag()​

@classmethod
def untag(
value: dict[str, t.Any]
) - > t.Any

Convert a tagged representation back to the original type.

Parameters​

NameTypeDescription
valuedict[str, t.Any]A dictionary potentially containing a single key representing a registered tag.

Returns​

TypeDescription
t.AnyThe reconstructed Python object if a valid tag key is found, otherwise the original dictionary.

dumps()​

@classmethod
def dumps(
value: t.Any
) - > str

Tag the value and dump it to a compact JSON string.

Parameters​

NameTypeDescription
valuet.AnyThe Python object to be tagged and serialized into JSON.

Returns​

TypeDescription
strA compact JSON string representation of the tagged value.

loads()​

@classmethod
def loads(
value: str
) - > t.Any

Load data from a JSON string and deserialized any tagged objects.

Parameters​

NameTypeDescription
valuestrThe JSON string to be parsed and untagged.

Returns​

TypeDescription
t.AnyThe original Python object, including reconstructed complex types.