# jose.py
class BaseObjectSerializer(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Enum):
return obj.value
#: ...... Other object judgment
if isinstance(obj, object):
#: Dict the field #: Ignore fields starting with an underscore return dict([(k, v) for k, v in obj.dict.items() if not k.startswith('_')])
return super(BaseObjectSerializer, self).default(obj)
Specify the encoder class with the to_json () method and do json.dumps:
# jose.py
class BaseObject(object):
_serializer = BaseObjectSerializer
def to_json(self, *args, **kwargs):
kwargs['cls'] = self._serializer #: Custom Serializer
return json.dumps(self, *args, **kwargs)
Only the result of addition is serialized, and holders in the middle are not serialized. ::
from jose import BaseObject
class Message(BaseObject):
def __init__(self, *args, **kwargs):
super(Message, self).__init__(*args, **kwargs)
self.total = 0
self._x =0
self._y =0
def sum(self, x, y):
self._x = x
self._y = y
self.total = self._x + self._y
m = Message()
m.sum(3, 4)
assert m.total == 7
assert m.to_json() == '{"total": 7}'
assert m._x == 3
m2 = Message()
assert m2._x == 0
Recommended Posts