Jun-25-2020, 07:24 PM
Hey guys,
I am currently learning serialization and deserialization between JSON obj and Python obj, there's the sample code:
Could someone help me out?
Thanks!!
I am currently learning serialization and deserialization between JSON obj and Python obj, there's the sample code:
class Who:
def __init__(self, name, age):
self.name = name
self.age = age
class MyEncoder(json.JSONEncoder):
def default(self, w):
if isinstance(w, Who):
return w.__dict__
else:
return super().default(self, z)
class MyDecoder(json.JSONDecoder):
def __init__(self):
json.JSONDecoder.__init__(self, object_hook=self.decode_who)
def decode_who(self, d):
return Who(**d)
some_man = Who('Jane Doe', 23)
json_str = json.dumps(some_man, cls=MyEncoder)
new_man = json.loads(json_str, cls=MyDecoder)
print(type(new_man))
print(new_man.__dict__)I understood everything except line 23:return Who(**d)specially the double asterisk operator here, I couldn't get the idea of this line will build the python object (type Who)
Could someone help me out?
Thanks!!
