示例#1
0
    def _cast_body(cls, body, content_type):
        if not isinstance(body, bytes):
            if is_json_mimetype(content_type):
                return json.dumps(body).encode()

            elif isinstance(body, str):
                return body.encode()

            else:
                return str(body).encode()
        else:
            return body
示例#2
0
    def _cast_body(cls, body, content_type):
        if not isinstance(body, bytes):
            if is_json_mimetype(content_type):
                return json.dumps(body).encode()

            elif isinstance(body, str):
                return body.encode()

            else:
                return str(body).encode()
        else:
            return body
示例#3
0
    def _serialize_data(cls, data, mimetype):
        # TODO: harmonize flask and aiohttp serialization when mimetype=None or mimetype is not JSON
        #       (cases where it might not make sense to jsonify the data)
        if (isinstance(mimetype, str) and is_json_mimetype(mimetype)):
            body = cls.jsonifier.dumps(data)
        elif not (isinstance(data, bytes) or isinstance(data, str)):
            warnings.warn(
                "Implicit (flask) JSON serialization will change in the next major version. "
                "This is triggered because a response body is being serialized as JSON "
                "even though the mimetype is not a JSON type. "
                "This will be replaced by something that is mimetype-specific and may "
                "raise an error instead of silently converting everything to JSON. "
                "Please make sure to specify media/mime types in your specs.",
                FutureWarning  # a Deprecation targeted at application users.
            )
            body = cls.jsonifier.dumps(data)
        else:
            body = data

        return body, mimetype
示例#4
0
    def _jsonify_data(cls, data, mimetype):
        if (isinstance(mimetype, six.string_types) and is_json_mimetype(mimetype)) \
                or not (isinstance(data, six.binary_type) or isinstance(data, six.text_type)):
            return cls.jsonifier.dumps(data)

        return data
示例#5
0
    def _jsonify_data(cls, data, mimetype):
        if (isinstance(mimetype, six.string_types) and is_json_mimetype(mimetype)) \
                or not (isinstance(data, six.binary_type) or isinstance(data, six.text_type)):
            return cls.jsonifier.dumps(data)

        return data