<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _serialize_data(self, data):
 """ Turn the data into a JSON API compliant resource object WARN: This function has both side effects & a return. It's complete shit because it mutates data & yet returns a new doc. FIX. :spec: jsonapi.org/format/#document-resource-objects :param data: dict for serializing :return: dict resource in JSON API format """


        rels = {}
        rlink = rid_url(data['rtype'], data['rid'])

        doc = {
            'id': data.pop('rid'),
            'type': data.pop('rtype'),
            'links': {
                'self': rlink,
            },
        }

        for key, val in data['to_many'].items():
            rels.update(self._serialize_to_many(key, val, rlink))
        del data['to_many']

        for key, val in data['to_one'].items():
            rels.update(self._serialize_to_one(key, val, rlink))
        del data['to_one']

        if data:
            doc['attributes'] = data
        if rels:
            doc['relationships'] = rels

        return doc