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
default_tagslist = [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=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=flask_json_tag_jsontag)]The tag class to register, which will be instantiated with this serializer instance.
forcebool = FalseWhether to overwrite an existing tag if the key is already registered.
index`intNone` = None

Returns​

TypeDescription
Nonenull

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 restored Python object if a valid tag was 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 serialize into a tagged JSON string.

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 deserialized Python object with all tags converted back to their original types.