mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Fixed JSON encoding and decoding for lists, added empty() method
The new empty() method retrieves an empty instance of the given object, with all the attributes set to None
This commit is contained in:
parent
fddb3e9aac
commit
b68772aab5
|
@ -117,14 +117,8 @@ class BinaryReader:
|
||||||
# If there was still no luck, give up
|
# If there was still no luck, give up
|
||||||
raise TypeNotFoundError(constructor_id)
|
raise TypeNotFoundError(constructor_id)
|
||||||
|
|
||||||
# Now we need to determine the number of parameters of the class, so we can
|
# Create an empty instance of the class, and read its values
|
||||||
# instantiate it with all of them set to None, and still, no need to write
|
result = clazz.empty()
|
||||||
# the default =None in all the classes, thus forcing the user to provide a real value
|
|
||||||
sig = inspect.signature(clazz.__init__)
|
|
||||||
params = [None] * (len(sig.parameters) - 1) # Subtract 1 (self)
|
|
||||||
result = clazz(*params) # https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists
|
|
||||||
|
|
||||||
# Finally, read the object and return the result
|
|
||||||
result.on_response(self)
|
result.on_response(self)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,17 @@ class TLGenerator:
|
||||||
TLGenerator.write_onsend_code(builder, arg, tlobject.args)
|
TLGenerator.write_onsend_code(builder, arg, tlobject.args)
|
||||||
builder.end_block()
|
builder.end_block()
|
||||||
|
|
||||||
|
# Generate the empty() function, which returns an "empty"
|
||||||
|
# instance, in which all attributes are set to None
|
||||||
|
builder.writeln('@staticmethod')
|
||||||
|
builder.writeln('def empty():')
|
||||||
|
builder.writeln('"""Returns an "empty" instance (all attributes are None)"""')
|
||||||
|
builder.writeln('return {}({})'.format(
|
||||||
|
TLGenerator.get_class_name(tlobject),
|
||||||
|
', '.join('None' for _ in range(len(args)))
|
||||||
|
))
|
||||||
|
builder.end_block()
|
||||||
|
|
||||||
# Write the on_response(self, reader) function
|
# Write the on_response(self, reader) function
|
||||||
builder.writeln('def on_response(self, reader):')
|
builder.writeln('def on_response(self, reader):')
|
||||||
# Do not read constructor's ID, since that's already been read somewhere else
|
# Do not read constructor's ID, since that's already been read somewhere else
|
||||||
|
@ -163,6 +174,10 @@ class TLGenerator:
|
||||||
|
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if TLGenerator.is_tlobject(arg.type):
|
if TLGenerator.is_tlobject(arg.type):
|
||||||
|
if arg.is_vector:
|
||||||
|
builder.writeln("'{0}': [{0}_item.json_encode() "
|
||||||
|
"for {0}_item in self.{0}],".format(arg.name))
|
||||||
|
else:
|
||||||
builder.writeln("'{0}': self.{0}.json_encode(),".format(arg.name))
|
builder.writeln("'{0}': self.{0}.json_encode(),".format(arg.name))
|
||||||
else:
|
else:
|
||||||
builder.writeln("'{0}': self.{0},".format(arg.name))
|
builder.writeln("'{0}': self.{0},".format(arg.name))
|
||||||
|
@ -175,14 +190,15 @@ class TLGenerator:
|
||||||
builder.writeln('@staticmethod')
|
builder.writeln('@staticmethod')
|
||||||
builder.writeln('def json_decode(json_string):')
|
builder.writeln('def json_decode(json_string):')
|
||||||
builder.writeln('# Create an empty instance which will be filled with the JSON values')
|
builder.writeln('# Create an empty instance which will be filled with the JSON values')
|
||||||
builder.writeln('instance = {}({})'.format(
|
builder.writeln('instance = {}.empty()'.format(TLGenerator.get_class_name(tlobject)))
|
||||||
TLGenerator.get_class_name(tlobject),
|
|
||||||
', '.join('None' for _ in range(len(args)))
|
|
||||||
))
|
|
||||||
builder.writeln('dictionary = json.loads(json_string)')
|
builder.writeln('dictionary = json.loads(json_string)')
|
||||||
builder.writeln()
|
builder.writeln()
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if TLGenerator.is_tlobject(arg.type):
|
if TLGenerator.is_tlobject(arg.type):
|
||||||
|
if arg.is_vector:
|
||||||
|
builder.writeln("instance.{0} = [{0}_item.json_decode() "
|
||||||
|
"for {0}_item in dictionary['{0}']]".format(arg.name))
|
||||||
|
else:
|
||||||
builder.writeln("instance.{0} = dictionary['{0}'].json_decode() if '{0}' in dictionary "
|
builder.writeln("instance.{0} = dictionary['{0}'].json_decode() if '{0}' in dictionary "
|
||||||
"and dictionary['{0}'] is not None else None".format(arg.name))
|
"and dictionary['{0}'] is not None else None".format(arg.name))
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user