def to_json(self):
        """
        convert the document into a json string and return it
        """
        def _convert_to_python(doc, struct):
            for key in struct:
                if isinstance(struct[key], dict):
                    if doc:  # we don't need to process an empty doc
                        if key in doc:  # we don't care about missing fields
                            _convert_to_python(doc[key], struct[key])
                elif type(struct[key]) is list:
                    if struct[key]:
                        if isinstance(struct[key][0], R):
                            l_objs = []
                            for obj in doc[key]:
                                obj['_collection'] = self.collection.name
                                obj['_database'] = self.db.name
                                l_objs.append(obj)
                            doc[key] = l_objs
                        elif isinstance(struct[key][0], dict):
                            if doc[key]:
                                for obj in doc[key]:
                                    _convert_to_python(obj, struct[key][0])
                else:
                    if isinstance(struct[key], R) and doc[key] is not None:
                        doc[key]['_collection'] = self.collection.name
                        doc[key]['_database'] = self.db.name
        try:
            from json import dumps
        except ImportError:
            from anyjson import serialize as dumps
        except __HOLE__:
            raise ImportError("can't import anyjson. Please install it before continuing.")
        obj = self.to_json_type()
        _convert_to_python(obj, self.structure)
        return unicode(dumps(obj))
dataset/ETHPy150Open namlook/mongokit/mongokit/document.py/Document.to_json