fork download
  1. # your code goes here
  2.  
  3. import json
  4.  
  5. class Contact:
  6. def __init__(self, first, last):
  7. self.first = first
  8. self.last = last
  9.  
  10. @property
  11. def full_name(self):
  12. return ("{} {}".format(self.first, self.last))
  13.  
  14. class ContactEncoder(json.JSONEncoder):
  15. def default(self, obj):
  16. if isinstance(obj, Contact):
  17. return {"is_contact": 'T'
  18. ,"first": obj.first
  19. ,"last": obj.last
  20. ,"full_name": obj.full_name}
  21. return super().default(obj)
  22.  
  23. if __name__ == "__main__":
  24. c = Contact("Jay", "Loophole")
  25. print(json.dumps(c.__dict__))
  26. print(json.dumps(c, cls=ContactEncoder))
Success #stdin #stdout 0.01s 10040KB
stdin
Standard input is empty
stdout
{"first": "Jay", "last": "Loophole"}
{"is_contact": "T", "first": "Jay", "last": "Loophole", "full_name": "Jay Loophole"}