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
| Attribute | Type | Description |
|---|---|---|
| default_tags | list = [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
| Name | Type | Description |
|---|---|---|
| tag_class | type[[JSONTag](jsontag.md?sid=flask_json_tag_jsontag)] | The tag class to register, which will be instantiated with this serializer instance. |
| force | bool = False | Whether to overwrite an existing tag if the key is already registered. |
| index | `int | None` = None |
Returns
| Type | Description |
|---|---|
None | null |
tag()
@classmethod
def tag(
value: t.Any
) - > t.Any
Convert a value to a tagged representation if necessary.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python object to be checked and potentially converted into a tagged format. |
Returns
| Type | Description |
|---|---|
t.Any | The 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
| Name | Type | Description |
|---|---|---|
| value | dict[str, t.Any] | A dictionary potentially containing a single key representing a registered tag. |
Returns
| Type | Description |
|---|---|
t.Any | The 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
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python object to serialize into a tagged JSON string. |
Returns
| Type | Description |
|---|---|
str | A 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
| Name | Type | Description |
|---|---|---|
| value | str | The JSON string to be parsed and untagged. |
Returns
| Type | Description |
|---|---|
t.Any | The deserialized Python object with all tags converted back to their original types. |