Marshmallow: [Question] is it possible to deserialize objects using standard schemas from a .yaml file?

Created on 6 Feb 2019  路  3Comments  路  Source: marshmallow-code/marshmallow

I want to build complex nested objects in a .yaml file that should be deserialized to real python objects, can we do this using the same schemas that I am using for json work? or do I need to do a yaml to json, then json through my schemas to python objects?

question

All 3 comments

You can set render_module to yaml in the schema's Meta class attributes. That will cause loads and dumps to use yaml instead of json.

https://marshmallow.readthedocs.io/en/3.0/api_reference.html#marshmallow.Schema.Meta

@deckar01 any easy way to manage where I need to use the same schemas for both json and yaml? I have a particular use case. define new schemas for each which are essentially duplicates with different render modules?

You would just separate the render operations from the schema operations, then swap the render module as needed.

data = yaml.load(raw_data)
obj = schema.load(data)
...
data = schema.dump(obj)
raw_data = yaml.dump(data)
Was this page helpful?
0 / 5 - 0 ratings