TagDict
Tag for 1-item dicts whose only key matches a registered tag.
Internally, the dict key is suffixed with __, and the suffix is removed
when deserializing.
Attributes
| Attribute | Type | Description |
|---|---|---|
| key | string | This attribute stores a string that is used as part of the internal representation of the dictionary key when serializing, and is removed during deserialization. |
Methods
check()
@classmethod
def check(
value: t.Any
) - > bool
Checks if the given value is a dictionary with a single key that corresponds to a registered tag. This method is used to determine if a dictionary should be handled by this tag for serialization/deserialization.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The value to check for compliance with the TagDict structure. |
Returns
| Type | Description |
|---|---|
bool | True if the value is a single-item dictionary whose key is a registered tag, False otherwise. |
to_json()
@classmethod
def to_json(
value: t.Any
) - > t.Any
Converts a Python dictionary into its JSON-serializable representation according to the TagDict format. It appends '__' to the dictionary's key to mark it for internal handling during serialization.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The Python dictionary to be converted to a JSON-compatible format. It is expected to be a single-item dictionary with a tag as its key. |
Returns
| Type | Description |
|---|---|
t.Any | A dictionary where the original key is suffixed with '__' and its value is serialized. |
to_python()
@classmethod
def to_python(
value: t.Any
) - > t.Any
Converts a JSON-deserialized dictionary back into its original Python dictionary form. It removes the '__' suffix from the key, restoring the original tag name.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The dictionary received from JSON deserialization, expected to have a single key suffixed with '__'. |
Returns
| Type | Description |
|---|---|
t.Any | A Python dictionary with the '__' suffix removed from its key, restoring the original tag. |