Source code for jam.types.base.null

from types import NoneType
from typing import Tuple, Union, Dict, Any
from jam.utils.codec import Codable
from jam.utils.codec.primitives.nulls import NullCodec
from jam.utils.json import JsonSerde


[docs] class Nullable(Codable, JsonSerde): """ Null value implementation. A Null represents the absence of a value. It is encoded as an empty byte sequence. Examples: >>> null = Null() >>> encoded = null.encode() >>> assert encoded == b"" >>> decoded, size = Null.decode_from(encoded) >>> assert decoded == null >>> assert size == 0 """
[docs] def __init__(self): """Initialize Null value.""" super().__init__(codec=NullCodec()) self.value = None
[docs] def get(self) -> None: """ Get the null value. Returns: None """ return None
[docs] def __repr__(self) -> str: """Get string representation.""" return "Null"
[docs] def __eq__(self, other: object) -> bool: """Compare for equality.""" if isinstance(other, Nullable): return True return isinstance(other, NoneType)
[docs] @staticmethod def decode_from( buffer: Union[bytes, bytearray, memoryview], offset: int = 0 ) -> Tuple["Nullable", int]: """ Decode null value from buffer. Args: buffer: Source buffer offset: Starting offset Returns: Tuple of (decoded null value, bytes read) """ _, size = NullCodec.decode_from(buffer, offset) return Null, size
[docs] def to_json(self) -> None: """Convert to JSON representation.""" return None
[docs] @classmethod def from_json(cls, data: Any) -> "Nullable": """Create from JSON representation.""" if data is not None: raise ValueError("Null value must be None") return Nullable()
Null = Nullable()