I was playing with the codecs module and realized that there's untapped potential to use them for de/serialization. It's easy enough to register a codec for some case, but very few objects (I think only strings and bytes and their stream cousins) have native encode/decode methods.

It seems to me that obj.encode("json") and str.decode("json"), for example, would be a powerful feature, if it were tied into the native codecs registry, enabling users to simplify a lot of serialization code and implement or tie-in any codec that makes sense.

Right now, if I want to json.dumps a MappingProxyType, I believe I have to pass a custom JSONEncoder to json.dumps explicitly every time I call it. But I think I should be able to register one, and then just call thing.encode('json'). I could call codecs.encode(thing, 'json'), but I think maybe I shouldn't have to import codecs or json into my modules to do this.

What do you think?


In case anyone is interested, here's a simple registration of json as a codec that works today:


import codecs, json


def encode(obj):
try:
size = len(obj)
except TypeError:
size = 1
return json.dumps(obj), size

def decode(obj):
return json.loads(obj), len(obj)
codec_info = codecs.CodecInfo(
name='json',
encode=encode,
decode=decode
)

codecs.register({'json': codec_info}.get)
print(codecs.encode({'a':1}, 'json'))  # etc