def read(self, io: ByteIo) -> Set[T] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: int = io.read_size(self.reserved_byte) out: Set[any] = set() for i in range(0, size): codec: Codec = self.value_codec if codec is None: codec = self.codec_cache.get(io.peek()) out.add(codec.read(io)) return out
def read(self, io: ByteIo) -> List[T] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: bytes or None = io.read_size(self.reserved_byte) out: List[T] = [] for i in range(0, size): out.append(self.codec.read(io)) return out
def read(self, io: ByteIo) -> Dict[T, U] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: int = io.read_size(self.reserved_byte) out: Dict[T, U] = {} for i in range(0, size): k_codec: Codec[T] = self.key_codec if k_codec is None: k_codec = self.codec_cache.get(io.peek()) key: T = k_codec.read(io) v_codec: Codec[U] = self.value_codec if v_codec is None: v_codec = self.codec_cache.get(io.peek()) value: T = v_codec.read(io) out[key] = value return out
def read(self, io: ByteIo) -> List[bytes] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: bytes or None = io.read_size(self.reserved_byte) if size == 0: return [] out: List[bytes] = [] for b in io.read(size): out.append(b.to_bytes(1, byteorder="big", signed=False)) return out
def read(self, io: ByteIo) -> List[str] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: bytes or None = io.read_size(self.reserved_byte) out: List[str] = [] encoded: bytes = io.read(size) string: str = encoded.decode("UTF-8") for char in string: out.append(char) return out
class Deserializer: __codec_cache: CodecCache __reader: ByteIo def __init__(self, codec_cache: CodecCache, reader: BinaryIO): super().__init__() self.__codec_cache = codec_cache self.__reader = ByteIo(reader) def read(self, codec: None or Codec[T] = None) -> T: if codec is not None: return codec.read(self.__reader) found: Codec = self.__codec_cache.get(self.__reader.peek()) return found.read(self.__reader) def close(self) -> None: self.__reader.close()
def read(self, io: ByteIo) -> str or None: size: int or None read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None if self.optimized_from <= read < self.size_1: size = read - self.optimized_from else: size = io.read_size(to_byte(self.size_1)) if size is None: return None if size == 0: return "" raw_bytes: bytes = io.read(size) return str(raw_bytes, "UTF-8")
def read(self, io: ByteIo) -> Deque[T] or None: marker: bytes = io.read() if marker == NoneCodec.NONE_VALUE: return None if marker != self.start: raise TypeError("Start of queue not found") out: Deque[T] = collections.deque() while True: marker = io.peek() if marker == self.end: io.read() return out codec: Codec = self.value_codec if codec is None: codec = self.codec_cache.get(marker) out.append(codec.read(io))
def read(self, io: ByteIo) -> List[bool] or None: read: int = from_byte(io.peek()) if read == from_byte(NoneCodec.NONE_VALUE): return None size: bytes or None = io.read_size(self.reserved_byte) out: List[bool] = [] i: int = 0 value: int = 0 read: int = 8 while i < size: if read == 8: value = int_from_byte(io.read(1)) read = 0 decoded: bool = (value & 0b10000000) != 0 out.append(decoded) i += 1 value = value << 1 read += 1 return out