diff options
Diffstat (limited to 'djangorestframework/parsers.py')
| -rw-r--r-- | djangorestframework/parsers.py | 24 | 
1 files changed, 24 insertions, 0 deletions
| diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index 37882984..5f19c521 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -16,15 +16,18 @@ from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser  from django.http.multipartparser import MultiPartParserError  from django.utils import simplejson as json  from djangorestframework import status +from djangorestframework.compat import yaml  from djangorestframework.response import ErrorResponse  from djangorestframework.utils.mediatypes import media_type_matches +  __all__ = (      'BaseParser',      'JSONParser',      'PlainTextParser',      'FormParser',      'MultiPartParser', +    'YAMLParser',  ) @@ -85,6 +88,27 @@ class JSONParser(BaseParser):                                  {'detail': 'JSON parse error - %s' % unicode(exc)}) +if yaml: +    class YAMLParser(BaseParser): +        """ +        Parses YAML-serialized data. +        """ +     +        media_type = 'application/yaml' +     +        def parse(self, stream): +            """ +            Returns a 2-tuple of `(data, files)`. +     +            `data` will be an object which is the parsed content of the response. +            `files` will always be `None`. +            """ +            try: +                return (yaml.safe_load(stream), None) +            except ValueError, exc: +                raise ErrorResponse(status.HTTP_400_BAD_REQUEST, +                                    {'detail': 'YAML parse error - %s' % unicode(exc)}) +  class PlainTextParser(BaseParser):      """ | 
