1919from collections import OrderedDict
2020from datetime import datetime
2121from io import BytesIO
22- from json import JSONEncoder , dumps
23-
24- from ..all import objects
22+ from json import dumps
2523
2624
2725class Object :
2826 all = {}
2927
28+ __slots__ = []
29+
30+ QUALNAME = "Base"
31+
3032 @staticmethod
3133 def read (b : BytesIO , * args ):
3234 return Object .all [int .from_bytes (b .read (4 ), "little" )].read (b , * args )
@@ -35,7 +37,7 @@ def write(self, *args) -> bytes:
3537 pass
3638
3739 def __str__ (self ) -> str :
38- return dumps (self , cls = Encoder , indent = 4 )
40+ return dumps (self , indent = 4 , default = default )
3941
4042 def __bool__ (self ) -> bool :
4143 return True
@@ -62,29 +64,18 @@ def remove_none(obj):
6264 return obj
6365
6466
65- class Encoder (JSONEncoder ):
66- def default (self , o : Object ):
67- try :
68- content = o .__dict__
69- except AttributeError :
70- if isinstance (o , datetime ):
71- return o .strftime ("%d-%b-%Y %H:%M:%S" )
72- else :
73- return repr (o )
74-
75- name = o .__class__ .__name__
76- o = objects .get (getattr (o , "ID" , None ), None )
77-
78- if o is not None :
79- if o .startswith ("pyrogram.client" ):
80- r = remove_none (OrderedDict ([("_" , "pyrogram:" + name )] + [i for i in content .items ()]))
81- r .pop ("_client" , None )
82-
83- return r
84- else :
85- return OrderedDict (
86- [("_" , o .replace ("pyrogram.api.types." , "telegram:" ))]
87- + [i for i in content .items ()]
88- )
67+ def default (o : "Object" ):
68+ try :
69+ content = {i : getattr (o , i ) for i in o .__slots__ }
70+
71+ return remove_none (
72+ OrderedDict (
73+ [("_" , o .QUALNAME )]
74+ + [i for i in content .items ()]
75+ )
76+ )
77+ except AttributeError :
78+ if isinstance (o , datetime ):
79+ return o .strftime ("%d-%b-%Y %H:%M:%S" )
8980 else :
90- return None
81+ return repr ( o )
0 commit comments