Msg format changed to pure json

This commit is contained in:
artem30801@gmail.com
2019-03-28 19:35:56 +03:00
parent aaa2222077
commit 72cbd488ff
2 changed files with 5 additions and 9 deletions

View File

@@ -107,7 +107,7 @@ def recive_message():
def form_message(str_command, dict_arguments):
msg_dict = {str_command: str(dict_arguments).replace(",", '').replace("'", '')[1:-1]}
msg_dict = {str_command: dict_arguments}
msg = json.dumps(msg_dict)
return msg
@@ -118,11 +118,9 @@ def parse_message(msg):
except ValueError:
print("Json string not in correct format")
return None, None
str_command = list(j_message.keys())[0]
dict_arguments = list(j_message.values())[0]
arguments = list(j_message.values())[0].replace(":", '').split()
dict_arguments = dict(zip(arguments[::2], arguments[1::2]))
return str_command, dict_arguments

View File

@@ -307,7 +307,7 @@ class Client:
def form_message(command: str, dict_arguments: dict = None):
if dict_arguments is None:
dict_arguments = {}
msg_dict = {command: str(dict_arguments).replace(",", '').replace("'", '')[1:-1]}
msg_dict = {command: dict_arguments}
msg = json.dumps(msg_dict)
return msg
@@ -315,14 +315,12 @@ class Client:
def parse_message(msg):
try:
j_message = json.loads(msg)
except json.decoder.JSONDecodeError:
except ValueError:
print("Json string not in correct format")
return None, None
str_command = list(j_message.keys())[0]
dict_arguments = list(j_message.values())[0]
arguments = list(j_message.values())[0].replace(":", '').split()
dict_arguments = collections.OrderedDict(zip(arguments[::2], arguments[1::2]))
return str_command, dict_arguments
@requires_connect