# Path: devil/datamapper.py
# class DataMapper(object):
#     """ Base class for all data mappers.
# 
#     This class also implements ``text/plain`` datamapper.
#     """
# 
#     content_type = 'text/plain'
#     charset = 'utf-8'
# 
#     def format(self, response):
#         """ Format the data.
# 
#         In derived classes, it is usually better idea to override
#         ``_format_data()`` than this method.
# 
#         :param response: devil's ``Response`` object or the data
#                          itself. May also be ``None``.
#         :return: django's ``HttpResponse``
# 
#         todo: this shouldn't change the given response. only return the
#         formatted response.
#         """
# 
#         res = self._prepare_response(response)
#         res.content = self._format_data(res.content, self.charset)
#         return self._finalize_response(res)
# 
#     def parse(self, data, charset=None):
#         """ Parse the data.
# 
#         It is usually a better idea to override ``_parse_data()`` than
#         this method in derived classes.
# 
#         :param charset: the charset of the data. Uses datamapper's
#         default (``self.charset``) if not given.
#         :returns:
#         """
# 
#         charset = charset or self.charset
#         return self._parse_data(data, charset)
# 
#     def _decode_data(self, data, charset):
#         """ Decode string data.
# 
#         :returns: unicode string
#         """
# 
#         try:
#             return smart_unicode(data, charset)
#         except UnicodeDecodeError:
#             raise errors.BadRequest('wrong charset')
# 
#     def _encode_data(self, data):
#         """ Encode string data. """
#         return smart_str(data, self.charset)
# 
#     def _format_data(self, data, charset):
#         """ Format the data
# 
#         @param data the data (may be None)
#         """
# 
#         return self._encode_data(data) if data else u''
# 
#     def _parse_data(self, data, charset):
#         """ Parse the data
# 
#         :param data: the data (may be None)
#         """
# 
#         return self._decode_data(data, charset) if data else u''
# 
#     def _prepare_response(self, response):
#         """ Coerce response to devil's Response
# 
#         :param response: either the response data or a ``Response`` object.
#         :returns: ``Response`` object
# 
#         todo: we don't need this anymore as resource already does the coercion
#         """
# 
#         if not isinstance(response, Response):
#             return Response(0, response)
#         return response
# 
#     def _finalize_response(self, response):
#         """ Convert the ``Response`` object into django's ``HttpResponse``
# 
#         :return: django's ``HttpResponse``
#         """
# 
#         res = HttpResponse(content=response.content,
#                            content_type=self._get_content_type())
#         # status_code is set separately to allow zero
#         res.status_code = response.code
#         return res
# 
#     def _get_content_type(self):
#         """ Return Content-Type header with charset info. """
#         return '%s; charset=%s' % (self.content_type, self.charset)
# 
# Path: devil/errors.py
# class HttpStatusCodeError(Exception):
# class BadRequest(HttpStatusCodeError):
# class Unauthorized(HttpStatusCodeError):
# class Forbidden(HttpStatusCodeError):
# class NotFound(HttpStatusCodeError):
# class NotAcceptable(HttpStatusCodeError):
# class MethodNotAllowed(HttpStatusCodeError):
# class Conflict(HttpStatusCodeError):
# class InternalServerError(HttpStatusCodeError):
#     def __init__(self, code, content=None, *args, **kw):
#     def get_code_num(self):
#     def has_code(self, code):
#     def __str__(self):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):


import simplejson as json
from devil.datamapper import DataMapper
from devil import errors
class JsonMapper(DataMapper):
    content_type = 'application/json'

    def __init__(self, use_decimal=False):
        """ Initialize JSON mapper with appropriate use of numbers.

        :param use_decimal: ``True`` if numbers should be converted
                            into ``Decimal``s.
        """

        self.use_decimal = use_decimal

    def _format_data(self, data, charset):
        if data is None or data == '':
            return u''
        else:
            params = {
                'indent': 4,
                'ensure_ascii': False,
                'encoding': charset,
                }
            self._maybe_add_use_decimal(params)
            return json.dumps(data, **params)

    def _parse_data(self, data, charset):
        params = {}
        self._maybe_add_use_decimal(params)
        try:
            return json.loads(data, charset, **params)
        except ValueError, exc:

# Path: devil/datamapper.py
# class DataMapper(object):
#     """ Base class for all data mappers.
# 
#     This class also implements ``text/plain`` datamapper.
#     """
# 
#     content_type = 'text/plain'
#     charset = 'utf-8'
# 
#     def format(self, response):
#         """ Format the data.
# 
#         In derived classes, it is usually better idea to override
#         ``_format_data()`` than this method.
# 
#         :param response: devil's ``Response`` object or the data
#                          itself. May also be ``None``.
#         :return: django's ``HttpResponse``
# 
#         todo: this shouldn't change the given response. only return the
#         formatted response.
#         """
# 
#         res = self._prepare_response(response)
#         res.content = self._format_data(res.content, self.charset)
#         return self._finalize_response(res)
# 
#     def parse(self, data, charset=None):
#         """ Parse the data.
# 
#         It is usually a better idea to override ``_parse_data()`` than
#         this method in derived classes.
# 
#         :param charset: the charset of the data. Uses datamapper's
#         default (``self.charset``) if not given.
#         :returns:
#         """
# 
#         charset = charset or self.charset
#         return self._parse_data(data, charset)
# 
#     def _decode_data(self, data, charset):
#         """ Decode string data.
# 
#         :returns: unicode string
#         """
# 
#         try:
#             return smart_unicode(data, charset)
#         except UnicodeDecodeError:
#             raise errors.BadRequest('wrong charset')
# 
#     def _encode_data(self, data):
#         """ Encode string data. """
#         return smart_str(data, self.charset)
# 
#     def _format_data(self, data, charset):
#         """ Format the data
# 
#         @param data the data (may be None)
#         """
# 
#         return self._encode_data(data) if data else u''
# 
#     def _parse_data(self, data, charset):
#         """ Parse the data
# 
#         :param data: the data (may be None)
#         """
# 
#         return self._decode_data(data, charset) if data else u''
# 
#     def _prepare_response(self, response):
#         """ Coerce response to devil's Response
# 
#         :param response: either the response data or a ``Response`` object.
#         :returns: ``Response`` object
# 
#         todo: we don't need this anymore as resource already does the coercion
#         """
# 
#         if not isinstance(response, Response):
#             return Response(0, response)
#         return response
# 
#     def _finalize_response(self, response):
#         """ Convert the ``Response`` object into django's ``HttpResponse``
# 
#         :return: django's ``HttpResponse``
#         """
# 
#         res = HttpResponse(content=response.content,
#                            content_type=self._get_content_type())
#         # status_code is set separately to allow zero
#         res.status_code = response.code
#         return res
# 
#     def _get_content_type(self):
#         """ Return Content-Type header with charset info. """
#         return '%s; charset=%s' % (self.content_type, self.charset)
# 
# Path: devil/errors.py
# class HttpStatusCodeError(Exception):
# class BadRequest(HttpStatusCodeError):
# class Unauthorized(HttpStatusCodeError):
# class Forbidden(HttpStatusCodeError):
# class NotFound(HttpStatusCodeError):
# class NotAcceptable(HttpStatusCodeError):
# class MethodNotAllowed(HttpStatusCodeError):
# class Conflict(HttpStatusCodeError):
# class InternalServerError(HttpStatusCodeError):
#     def __init__(self, code, content=None, *args, **kw):
#     def get_code_num(self):
#     def has_code(self, code):
#     def __str__(self):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):
#     def __init__(self, *args, **kw):


# Path: devil/mappers/jsonmapper.py
import simplejson as json
from devil.datamapper import DataMapper
from devil import errors

class JsonMapper(DataMapper):
    content_type = 'application/json'

    def __init__(self, use_decimal=False):
        """ Initialize JSON mapper with appropriate use of numbers.

        :param use_decimal: ``True`` if numbers should be converted
                            into ``Decimal``s.
        """

        self.use_decimal = use_decimal

    def _format_data(self, data, charset):
        if data is None or data == '':
            return u''
        else:
            params = {
                'indent': 4,
                'ensure_ascii': False,
                'encoding': charset,
                }
            self._maybe_add_use_decimal(params)
            return json.dumps(data, **params)

    def _parse_data(self, data, charset):
        params = {}
        self._maybe_add_use_decimal(params)
        try:
            return json.loads(data, charset, **params)
        except ValueError, exc:

            raise errors.BadRequest('unable to parse data: %s' % (str(exc),))