1

I'm new to Python Marshmallow module and quite like it for serialization and de-serialization. I know that after defining a schema, I can easily serialize a dict or an object to JSON format using schema.dumps method.

However, I wonder how I could serialize an object or a dict to another format than JSON. For example, sometimes I'd like to serialize an object to XML.

I saw this issue in Marshmallow's Github repo. It seems its doable by playing with something called json_module. However, I can't really follow that thread because I'm quite new to Marshmallow.

Logically, schema.dumps seems to be the right method to do that, but from the documentation, I didn't see a possibility of serializing data to any other format than JSON.

I'd appreciate it if anyone could give me an example of converting a dict to an XML string.

Edit: Thanks for the answer using dicttoxml. It's a good module and indeed it solves my problem. However, isn't the "output formatt control" a built-in feature of marshmallow? I though if it were, then the support to controlling output format should be in schema.dumps method? Or did I miss anything?

Zheng Liu
  • 292
  • 2
  • 11
  • 3
    Marshmallow is essentially *format agnostic*. It outputs a structure of Python dictionaries and lists, that can then be serialised to JSON or to any other format of your choosing. – Martijn Pieters Sep 11 '19 at 10:25
  • 2
    So no, Marshmallow does not give you output format control, other than that you can control how object attributes map to key-value pairs in the output dictionaries. The goal is to produce output that is *trivial to serialize*, with whatever serialization method you choose. Usually that's JSON, but can be YAML, XML, CSV or other formats. – Martijn Pieters Sep 11 '19 at 10:26
  • Thanks Martjin. I guess I didn’t understand what the format agnostic really meant when I firstly saw it in marshmallow’s doc. Then what’s marshmallow really good at? For me, what’s left is basically just validation and possibly transformation. So for example, how do you compare marshmallow with cerberus? – Zheng Liu Sep 11 '19 at 11:56
  • 2
    Yes, it is good at exactly that, validation and transformation. It puts data from potentially complex objects (custom classes, etc.) into a structure of lists and dictionaries, and can do the same in reverse. – Martijn Pieters Sep 11 '19 at 12:01
  • I don't know Cerberus, but at first glance it only manages validation. There are more packages in this space, such as [collander](https://pypi.org/project/colander/). – Martijn Pieters Sep 11 '19 at 12:03

1 Answers1

1

As per the answer here. You could always just post process the json to xml using a library. Try dicttoxml

import json
from dicttoxml import dicttoxml
marshmallow_json_string = schema.dumps
marshmallow_dict = json.loads(marshmallow_json_string)
marshmallow_xml = dicttoxml(marshmallow_dict)
Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53
  • That's a very promising answer. Thanks very much. Still, my gut feeling is that it should have been built into marshmallow, isn't it? Maybe my understanding to marshmallow is wrong but I thought it could control input format, do the transformation and control the output format? – Zheng Liu Sep 11 '19 at 05:24
  • 1
    Possibly I'm not too sure, I don't know how users normally use the package. Either way, I think you are on the right track talking about it in github issues for the repo. Developers who know the purpose of the project there can tell you why they did/didn't include it. – Jesse Reza Khorasanee Sep 11 '19 at 05:43
  • 2
    Have a look [at this issue](https://github.com/marshmallow-code/marshmallow/issues/737) where someone is taking in json and outputting yaml. I think that's what you might want but for xml. – Jesse Reza Khorasanee Sep 11 '19 at 05:44
  • 3
    Thanks Jesse. I think the link you provided really helped me. I’ll set this as the answer. Looking back, what I missed was really how to use the render_module method:) – Zheng Liu Sep 11 '19 at 12:00