marshmallow-jsonapi
marshmallow-jsonapi copied to clipboard
Serializing between JSON-API and regular marshmallow representations
I have a use case that requires translating an object between JSON-API format (frontend) and vanilla JSON (backend). Since the underlying field structure and field formats are the same, what's the best way to handle this?
I usually avoid multiple inheritance, but I thought it might be possible to subclass a vanilla marshmallow Schema class. Here's an example:
from marshmallow import fields, Schema
from marshmallow_jsonapi import Schema as JSONAPISchema
class FooSchema(Schema):
id = fields.Str()
name = fields.Str()
class FooJSONAPISchema(JSONAPISchema, FooSchema):
class Meta:
type_ = 'foobar'
strict = True
How does that look? Can we do any better?
I have the same problem. I want to integrate marshmallow-mongoengine and marshmallow-jsonapi, it seems using metaclass or inheritance makes it difficult for such thing.