Skip to main content

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

AttributeTypeDescription
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

NameTypeDescription
valuet.AnyThe value to be checked for its type.

Returns

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

NameTypeDescription
valuet.AnyThe bytes object to be converted to a JSON-compatible string.

Returns

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

NameTypeDescription
valuet.AnyThe base64 encoded string to be converted back into a bytes object.

Returns

TypeDescription
t.AnyThe original bytes object decoded from the base64 string.