开发者

I need help understanding some pythonic "dictify" / "jsonify" code fragments

I need help understanding this python jsonify/dictify code so I can replicate it:


TYPES = {}

# auto-register all already defined subclasses of CustomObject to the types map
# so they will become seriazible/deseriazible
for N,T in locals().items():
    if isinstance(T, type) and issubclass(T, CustomObject):
        TYPES[N] = T


def CustomTypeDecoder(dct):
    type_name = dct.get('type')
    if type_name:
        cls = TYPES.get(type_name)
        if cls:
            return cls(**dct)
    return dct

def loads(s):
    return json.loads(s, object_hook=CustomTypeDecoder) 


class CustomTypeEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, tuple(TYPES.values())):
   开发者_开发知识库         res = dict(type=obj.__class__.__name__)
            res.update(obj.to_dict())
            return res
        return json.JSONEncoder.default(self, obj)

def dumps(obj):
    return json.dumps(obj, cls=CustomTypeEncoder)


The code that does the hard work is in the class itself. Lines 15 and 27 use the class functionality to convert the class from and to dicts, which are JSON-serializable; the rest is all administrivia code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜