Add custom content-type/mimetypeΒΆ
Content-types are defined as subclasses of
restfulgrok.contenttype.ContentType
. Take look at its docs, and the
sourcecode of restfulgrok.contenttype.JsonContentType
:
class JsonContentType(ContentType):
"""
JSON content type. Implements both loads and dumps.
"""
mimetype = 'application/json'
extension = 'json'
description = json_description
@classmethod
def dumps(cls, pydata, view=None):
try:
return json.dumps(pydata, indent=2)
except TypeError, e:
raise ContentTypeDumpError(str(e))
except ValueError, e:
raise ContentTypeDumpError(str(e))
@classmethod
def loads(cls, rawdata, view=None):
try:
return json.loads(rawdata)
except TypeError, e:
raise ContentTypeLoadError(str(e))
except ValueError, e:
raise ContentTypeDumpError(str(e))