Skip to main content

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

AttributeTypeDescription
keystringThis 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

NameTypeDescription
valuet.AnyThe value to check for compliance with the TagDict structure.

Returns

TypeDescription
boolTrue 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

NameTypeDescription
valuet.AnyThe 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

TypeDescription
t.AnyA 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

NameTypeDescription
valuet.AnyThe dictionary received from JSON deserialization, expected to have a single key suffixed with '__'.

Returns

TypeDescription
t.AnyA Python dictionary with the '__' suffix removed from its key, restoring the original tag.