marshmallow multiplexing schema
Project description
An extension to marshmallow to support schema (de)multiplexing.
marshmallow is a fantastic library for serialization and deserialization of data. For more on that project see its GitHub page or its Documentation.
This library adds a special kind of schema that actually multiplexes other schemas based on object type. When serializing values, it uses get_obj_type() method to get object type name. Then it uses type_schemas name-to-Schema mapping to get schema for that particular object type, serializes object using that schema and adds an extra field with name of object type. Deserialization is reverse.
Installing
$ pip install marshmallow-oneofschema
Example
The code below demonstrates how to set up a polymorphic schema. For the full context check out the tests. Once setup the schema should act like any other schema. If it does not then please file an Issue.
import marshmallow
import marshmallow.fields
from marshmallow_oneofschema import OneOfSchema
class Foo:
def __init__(self, foo):
self.foo = foo
class Bar:
def __init__(self, bar):
self.bar = bar
class FooSchema(marshmallow.Schema):
foo = marshmallow.fields.String(required=True)
@marshmallow.post_load
def make_foo(self, data, **kwargs):
return Foo(**data)
class BarSchema(marshmallow.Schema):
bar = marshmallow.fields.Integer(required=True)
@marshmallow.post_load
def make_bar(self, data, **kwargs):
return Bar(**data)
class MyUberSchema(OneOfSchema):
type_schemas = {"foo": FooSchema, "bar": BarSchema}
def get_obj_type(self, obj):
if isinstance(obj, Foo):
return "foo"
elif isinstance(obj, Bar):
return "bar"
else:
raise Exception("Unknown object type: {}".format(obj.__class__.__name__))
MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
# => [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]
MyUberSchema().load(
[{"type": "foo", "foo": "hello"}, {"type": "bar", "bar": 123}], many=True
)
# => [Foo('hello'), Bar(123)]
By default get_obj_type() returns obj.__class__.__name__, so you can just reuse that to save some typing:
class MyUberSchema(OneOfSchema):
type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
You can customize type field with type_field class property:
class MyUberSchema(OneOfSchema):
type_field = "object_type"
type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
# => [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]
You can use resulting schema everywhere marshmallow.Schema can be used, e.g.
import marshmallow as m
import marshmallow.fields as f
class MyOtherSchema(m.Schema):
items = f.List(f.Nested(MyUberSchema))
License
MIT licensed. See the bundled LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for marshmallow_oneofschema-3.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68b4a57d0281a04ac25d4eb7a4c5865a57090a0a8fd30fd6362c8e833ac6a6d9 |
|
MD5 | 2cb3dfcfc9619db6880265e477ad243c |
|
BLAKE2b-256 | 35758dd134f08375845910d134e50246fdfcab3f1d84ab3284bd09bb15f69be9 |
Hashes for marshmallow_oneofschema-3.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff4cb2a488785ee8edd521a765682c2c80c78b9dc48894124531bdfa1ec9303b |
|
MD5 | 9c72a0c13dd0ee2cff01d6181b58cac7 |
|
BLAKE2b-256 | 5c813ef15337c19d3e3432945aad738081a5f54c16885277c7dff300b5f85b24 |