JSONTag
Base class for defining type tags for :class:TaggedJSONSerializer.
Attributes
| Attribute | Type | Description |
|---|---|---|
| serializer | [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer) | The TaggedJSONSerializer instance associated with this tagger. |
| key | str = "" | The tag to mark the serialized object with. If empty, this tag is only used as an intermediate step during tagging. |
Constructor
Signature
def JSONTag(
serializer: [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer)
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| serializer | [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer) | The serializer instance to associate with this tagger. |
Signature
def JSONTag(
serializer: [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer)
)
Parameters
| Name | Type | Description |
|---|---|---|
| serializer | [TaggedJSONSerializer](taggedjsonserializer.md?sid=flask_json_tag_taggedjsonserializer) | The serializer instance this tag belongs to, used for recursive serialization. |
Methods
check()
@classmethod
def check(
value: t.Any
) - > bool
Check if the given value should be tagged by this tag.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python object to inspect for type compatibility. |
Returns
| Type | Description |
|---|---|
bool | True if the value matches the type handled by this tag, False otherwise. |
to_json()
@classmethod
def to_json(
value: t.Any
) - > t.Any
Convert the Python object to an object that is a valid JSON type. The tag will be added later.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python object to be converted into a JSON-compatible format. |
Returns
| Type | Description |
|---|---|
t.Any | A JSON-serializable representation of the input value. |
to_python()
@classmethod
def to_python(
value: t.Any
) - > t.Any
Convert the JSON representation back to the correct type. The tag will already be removed.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The JSON-compatible value to be converted back into a Python object. |
Returns
| Type | Description |
|---|---|
t.Any | The reconstructed Python object of the original type. |
tag()
@classmethod
def tag(
value: t.Any
) - > dict[str, t.Any]
Convert the value to a valid JSON type and add the tag structure around it.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python object to be transformed and wrapped in a tag dictionary. |
Returns
| Type | Description |
|---|---|
dict[str, t.Any] | A dictionary containing the tag key mapping to the JSON-serializable value. |