-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.py
More file actions
28 lines (22 loc) · 795 Bytes
/
json_parser.py
File metadata and controls
28 lines (22 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
from datetime import datetime
from object.user import User
from object.chat import Chat
def user2json(user:User)->str:
dic={'name':user.get_name(), 'id':user.get_id()}
return json.dumps(dic)
def json2user(jstr:str)->User:
dic=json.loads(jstr)
user=User(userid=dic['id'],username=dic['name'])
return user
def time2str(time:datetime):
return time.strftime("%Y%m%d %H:%M:%S")
def str2time(tstr:str):
return datetime.strptime(tstr,"%Y%m%d %H:%M:%S")
def chat2json(chat:Chat)->str:
dic={'user':user2json(chat.user), 'msg':chat.msg, 'time':time2str(chat.time)}
return json.dumps(dic)
def json2chat(jstr:str)->Chat:
dic=json.loads(jstr)
chat=Chat(user=json2user(dic['user']), msg=dic['msg'], time=str2time(dic['time']))
return chat