python 自定义数据结构_但是Python本身可能需要具有自定义数据结构的json编码

可能是过度杀戮,但我曾经用过一个Mixin类,如下所示:def _default_json_encoder(obj):

""" Default encoder, encountered must have to_dict method to be serialized. """

if hasattr(obj, "to_dict"):

return obj.to_dict()

else:

raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)))

class MixinJSONable(object):

"""

Mixin pattern to add a capability to an object to be jsonable.

If object have to_dict method it will be used to produce the dict, otherwise

MixinJSONable.to_dict will be used.

Only "public" attributes will be dump and instance of self._jsonable tuple.

Attributes could be ignored by passing ignored_keys parameter to to_json method.

Thus _ignored_json_keys (class attribute) will be update and spread over all class using this Mixin.

"""

# _ignored_json_keys = list()

def to_dict(self):

"""

to_dict method to dump public attributes.

"""

self._jsonable = (int, list, str, dict)

_dict = dict()

for attr in dir(self):

value = getattr(self, attr)

if attr.startswith("_") or attr in getattr(MixinJSONable, "_ignored_json_keys", []):

continue

elif isinstance(value, self._jsonable) or value is None or hasattr(value, 'to_dict'):

# to_dict method is used as serialization method.

value = value

else:

continue

_dict[attr] = value

return _dict

def to_json(self, **kw):

"""

Dump object as Json.

Accept the same keys than :func json.dumps:. If ignored_keys (list) is passed,

the keys will not be dumped in the json (filter over all sons)

"""

indent = kw.pop("indent", 4) # use indent key if passed otherwise 4.

_ignored_json_keys = kw.pop("ignored_keys", [])

if _ignored_json_keys:

MixinJSONable._ignored_json_keys = _ignored_json_keys

return json.dumps(self, indent=indent, default=_default_json_encoder, **kw)

class X(MixinJSONable):

pass

x = X()

y = X()

setattr(x,"val",{1:2,3:4})

setattr(y,"val",{1:2,3:x})

y.to_json()

将打印:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值