项目作者: alexprengere

项目描述 :
Automatic Json export of your Python objects
高级语言: Python
项目地址: git://github.com/alexprengere/jsonmixin.git
创建时间: 2016-09-29T14:18:18Z
项目社区:https://github.com/alexprengere/jsonmixin

开源协议:Apache License 2.0

下载


JsonMixin

Automatic to_json() on your Python objects!

Note that class attributes, class methods, static methods, instance methods and properties will not appear in the json representation, only instance attributes.
This also works whether slots are defined or not.

  1. >>> from jsonmixin import JsonMixin
  2. >>>
  3. >>> class Titi(JsonMixin):
  4. ... clsvars = 'test'
  5. ... def __init__(self, a, b):
  6. ... self.a = a
  7. ... self.b = b
  8. ... def method(self):
  9. ... return self.a
  10. ... @property
  11. ... def prop(self):
  12. ... return self.b
  13. ... @prop.setter
  14. ... def prop(self, b):
  15. ... self.b = b
  16. ... @classmethod
  17. ... def clsmethod(cls):
  18. ... pass
  19. ... @staticmethod
  20. ... def static():
  21. ... pass
  22. >>> l = Titi(1, '2')
  23. >>>
  24. >>> from pprint import pprint
  25. >>> pprint(l.to_json())
  26. {'a': 1, 'b': '2'}