marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Serializing between JSON-API and regular marshmallow representations

Open jordal opened this issue 8 years ago • 1 comments

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?

jordal avatar Feb 25 '17 09:02 jordal

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.

lycplus avatar Mar 06 '17 03:03 lycplus