I'd love for Marshmallow to be more modular, for example I'm loading from JSON and serializing to bencode. It would be great if there was a simple way to do this.
It was suggested in https://github.com/marshmallow-code/marshmallow/issues/130 but the solution assumes that the source and destination encoding are the same.
I think you could use two schemas for this. Define a schema MySchema that speaks JSON, then subclass it as MyBencodeSchema and set the encoder to your bencode encoder.
I was about to close this as this is a corner case and the two-schema solution seems reasonable enough.
OTOH, this would be really easy to achieve, all it takes is a load_render_module and a dump_render_module rather than just render_module.
Yeah but also remove the json-specific names of things like json_module
My comment "Done in 3331f83" is misleading. It was a reply to the comment above about "the json-specific names". The feature in the OP is not implemented.
It would be easy to add, but it is also easy to achieve from user side so it is not critical.
Anyone, feel free to reopen if feeling strongly about it.
Ah, I see. OK, for now I'd recommend implementing a custom render class that implements dumps and loads.
class ComboRenderer:
@staticmethod
def loads(s, *args, **kwargs):
return json.loads(s, *args, **kwargs)
@staticmethod
def dumps(obj, *args, **kwargs):
return yaml.dumps(obj, *args, **kwargs)
class MySchema(Schema):
class Meta:
render_module = ComboRenderer
That's much better than my workaround. I find it easy enough not to bother implementing the OP.
Most helpful comment
Ah, I see. OK, for now I'd recommend implementing a custom render class that implements dumps and loads.