diff options
| author | Tom Christie | 2012-09-03 15:57:43 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-09-03 15:57:43 +0100 |
| commit | 149b00a070fcbfd44feee5b37096081e18356f93 (patch) | |
| tree | 4ce2586f6a15613625a23d80b4624e64a9b94de6 /djangorestframework/parsers.py | |
| parent | 7abef9ac3b3fb20a6cdef5d52c640e5725c93437 (diff) | |
| download | django-rest-framework-149b00a070fcbfd44feee5b37096081e18356f93.tar.bz2 | |
Added the api_view decorator
Diffstat (limited to 'djangorestframework/parsers.py')
| -rw-r--r-- | djangorestframework/parsers.py | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index fc0260fc..96dd81ed 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -23,6 +23,7 @@ from djangorestframework.compat import ETParseError from xml.parsers.expat import ExpatError import datetime import decimal +from io import BytesIO __all__ = ( @@ -63,12 +64,24 @@ class BaseParser(object): """ return media_type_matches(self.media_type, content_type) - def parse(self, stream, **opts): + def parse(self, string_or_stream, **opts): + """ + The main entry point to parsers. This is a light wrapper around + `parse_stream`, that instead handles both string and stream objects. + """ + if isinstance(string_or_stream, basestring): + stream = BytesIO(string_or_stream) + else: + stream = string_or_stream + return self.parse_stream(stream, **opts) + + def parse_stream(self, stream, **opts): """ Given a *stream* to read from, return the deserialized output. - Should return a 2-tuple of (data, files). + Should return parsed data, or a DataAndFiles object consisting of the + parsed data and files. """ - raise NotImplementedError(".parse() Must be overridden to be implemented.") + raise NotImplementedError(".parse_stream() Must be overridden to be implemented.") class JSONParser(BaseParser): @@ -78,7 +91,7 @@ class JSONParser(BaseParser): media_type = 'application/json' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): """ Returns a 2-tuple of `(data, files)`. @@ -98,7 +111,7 @@ class YAMLParser(BaseParser): media_type = 'application/yaml' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): """ Returns a 2-tuple of `(data, files)`. @@ -118,7 +131,7 @@ class PlainTextParser(BaseParser): media_type = 'text/plain' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): """ Returns a 2-tuple of `(data, files)`. @@ -135,7 +148,7 @@ class FormParser(BaseParser): media_type = 'application/x-www-form-urlencoded' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): """ Returns a 2-tuple of `(data, files)`. @@ -153,7 +166,7 @@ class MultiPartParser(BaseParser): media_type = 'multipart/form-data' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): """ Returns a DataAndFiles object. @@ -177,7 +190,7 @@ class XMLParser(BaseParser): media_type = 'application/xml' - def parse(self, stream, **opts): + def parse_stream(self, stream, **opts): try: tree = ET.parse(stream) except (ExpatError, ETParseError, ValueError), exc: |
