def request_dto_computer_key(): dto0 = RequestDto[ComputerKey](data=ComputerKey("5", "40")) json0 = JsonFormatter.serialize(dto0) val0 = JsonFormatter.deserialize(json0, RequestDto[ComputerKey]) print(val0) print(RequestDto.__annotations__["data"]) dto1 = RequestDto[ComputerManager.GetKeysByAuditoriumDto]( data=ComputerManager.GetKeysByAuditoriumDto("37")) json1 = JsonFormatter.serialize(dto1) val1 = JsonFormatter.deserialize( json1, RequestDto[ComputerManager.GetKeysByAuditoriumDto]) print(val1) print(RequestDto.__annotations__["data"])
def request_dto_serialize(): dto = RequestDto[ComputerFlow]("some_method", ComputerFlow.get_random_flow()) json_dto = JsonFormatter.serialize(dto) dto_value = JsonFormatter.deserialize(json_dto, RequestDto[ComputerFlow]) print(dto_value) # repeat json_dto2 = JsonFormatter.serialize(dto_value) dto_value2 = JsonFormatter.deserialize(json_dto2, RequestDto[ComputerFlow]) print(dto_value2 ) # needed for breakpoint to check the result of formatting
def __read_from_config(cls) -> Union[UrlInfo, None]: if os.path.exists(cls.__file_name): with open(cls.__file_name) as file: json = file.read() if len(json) > 0: return JsonFormatter.deserialize(json, UrlInfo) return None
def __read_temp_data(cls) -> ComputerFlow: temp_data: str = "" if os.path.exists(cls.__temp_data_file): with open(cls.__temp_data_file, "r") as file: temp_data = file.read() return JsonFormatter.deserialize( temp_data, ComputerFlow) if temp_data else ComputerFlow()
def read_all(self) -> List[Computer]: if len(self.__cache) == super().count(): return [*self.__cache.values()] read_all_result = super().read_all() computers = [] for name, auditorium, json_data in read_all_result: computer: Computer = JsonFormatter.deserialize(json_data, Computer) self.__cache[ComputerKey(name, auditorium)] = computer computers.append(computer) return computers
def get_request_dto(self, json_request_dto: str, method_name: str, client_address: Tuple[str, int]) -> BaseRequestDto: dto_type = self.__get_request_type(method_name) request_dto = JsonFormatter.deserialize(json_request_dto, dto_type) if not hasattr(request_dto, "data"): raise ServerLogicException( 400, f"Server work only with dto types ! Stop spam !") UrlManager.resolve_client_address(request_dto, client_address) return request_dto
def read(self, key: ComputerKey) -> Union[Computer, None]: computer = self.__cache.get(key) if computer is not None: return computer *_, value = super().read(key) or (None, None) if value is not None: computer = JsonFormatter.deserialize(value, Computer) self.__cache[key] = computer return computer return None
def __read_config_from_file(file_name: str) -> ClientConfig: if os.path.exists(file_name): with open(file_name, "r") as file: file_content = file.read() else: raise Exception(f"Can not open file with Client config (file: {file_name})") if file_content: config = JsonFormatter.deserialize(file_content, ClientConfig) if ConfigManager.__validate_config(config): return config raise Exception(f"Client config in {file_name} is invalid, check the file")
def __read_keys_from_file(cls): if os.path.exists(cls.__file_name): with open(cls.__file_name, "r") as file: keys = file.read() return JsonFormatter.deserialize( keys, ComputerKeysData) if keys else None
def client_config(): c = ClientConfig(None, None) json = JsonFormatter.serialize(c) val = JsonFormatter.deserialize(json, ClientConfig) print(val.__dict__) print(val)
def computer_serialize(): computer = Computer.get_random_computer() json = JsonFormatter.serialize(computer) val = JsonFormatter.deserialize(json, Computer) print(val)
def dict_field_serialize(): some_type = SomeType.get_random_some_type() json = JsonFormatter.serialize(some_type) val = JsonFormatter.deserialize(json, SomeType) print(val)
def request_complex_dto_serialize(): dto = SomeEntity.get_temp_some_entity() json_dto = JsonFormatter.serialize(dto) dto_val = JsonFormatter.deserialize(json_dto, SomeEntity) print(dto_val)
def computer_flow_serialize(): computer_flow = ComputerFlow.get_random_flow() computer_flow_json = JsonFormatter.serialize(computer_flow) val = JsonFormatter.deserialize(computer_flow_json, ComputerFlow) print(val)
def serialize_test(): person: Person = Person(13, "Max") json = JsonFormatter.serialize(person) person_val = JsonFormatter.deserialize(json, Person) print(person_val)
def __read_from_server() -> Union[ClientConfig, None]: json: str = NetworkManager.get("GetClientConfig") return JsonFormatter.deserialize( json, ResponseDto[ClientConfig]).data if json else None