def marshal(data, fields, envelope=None):
def make(cls):<EOL><INDENT>if isinstance(cls, type):<EOL><INDENT>return cls()<EOL><DEDENT>return cls<EOL><DEDENT>if isinstance(data, (list, tuple)):<EOL><INDENT>return (OrderedDict([(envelope, [marshal(d, fields) for d in data])])<EOL>if envelope else [marshal(d, fields) for d in data])<EOL><DEDENT>items = ((k, marshal(data, v) if isinstance(v, dict)<EOL>else make(v).output(k, data))<EOL>for k, v in fields.items())<EOL>return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)<EOL>
Takes raw data (in the form of a dict, list, object) and a dict of
    fields to output and filters the data based on those fields.

    :param data: the actual object(s) from which the fields are taken from
    :param fields: a dict of whose keys will make up the final serialized
                   response output
    :param envelope: optional key that will be used to envelop the serialized
                     response


    >>> from flask_restful import fields, marshal
    >>> data = { 'a': 100, 'b': 'foo' }
    >>> mfields = { 'a': fields.Raw }

    >>> marshal(data, mfields)
    OrderedDict([('a', 100)])

    >>> marshal(data, mfields, envelope='data')
    OrderedDict([('data', OrderedDict([('a', 100)]))])