TagBytes
This class handles the serialization and deserialization of bytes objects to and from a JSON-compatible base64 encoded string representation. It checks if a value is a bytes object, encodes it to base64 for JSON output, and decodes it back to bytes from a base64 string.
Attributes
| Attribute | Type | Description |
|---|---|---|
| key | = " b" | This attribute stores the string identifier used to represent byte data in JSON, ensuring consistent serialization and deserialization of binary data. |
Methods
check()
@classmethod
def check(
value: t.Any
) - > bool
Checks if the provided value is an instance of bytes. This method is used to validate if a value conforms to the expected byte type.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The value to be checked for its type. |
Returns
| Type | Description |
|---|---|
bool | True if the value is a bytes object, False otherwise. |
to_json()
@classmethod
def to_json(
value: t.Any
) - > t.Any
Converts a bytes object into a base64 encoded string suitable for JSON serialization. This is used to safely represent binary data within a JSON structure.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The bytes object to be converted to a JSON-compatible string. |
Returns
| Type | Description |
|---|---|
t.Any | A base64 encoded string representation of the bytes object. |
to_python()
@classmethod
def to_python(
value: t.Any
) - > t.Any
Decodes a base64 encoded string back into a bytes object. This method is used to reconstruct binary data from its JSON-serialized string representation.
Parameters
| Name | Type | Description |
|---|---|---|
| value | t.Any | The base64 encoded string to be converted back into a bytes object. |
Returns
| Type | Description |
|---|---|
t.Any | The original bytes object decoded from the base64 string. |