Merge "wsgi: clean JSON serializer"
This commit is contained in:
commit
cafbe9e69d
|
@ -21,9 +21,7 @@ Utility methods for working with WSGI servers
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import datetime
|
|
||||||
import errno
|
import errno
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
@ -679,13 +677,14 @@ class JSONRequestDeserializer(object):
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _sanitizer(self, obj):
|
@staticmethod
|
||||||
|
def _sanitizer(obj):
|
||||||
"""Sanitizer method that will be passed to jsonutils.loads."""
|
"""Sanitizer method that will be passed to jsonutils.loads."""
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def from_json(self, datastring):
|
def from_json(self, datastring):
|
||||||
try:
|
try:
|
||||||
return json.loads(datastring, object_hook=self._sanitizer)
|
return jsonutils.loads(datastring, object_hook=self._sanitizer)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
msg = _('Malformed JSON in request body.')
|
msg = _('Malformed JSON in request body.')
|
||||||
raise webob.exc.HTTPBadRequest(explanation=msg)
|
raise webob.exc.HTTPBadRequest(explanation=msg)
|
||||||
|
@ -701,15 +700,11 @@ class JSONResponseSerializer(object):
|
||||||
|
|
||||||
def _sanitizer(self, obj):
|
def _sanitizer(self, obj):
|
||||||
"""Sanitizer method that will be passed to jsonutils.dumps."""
|
"""Sanitizer method that will be passed to jsonutils.dumps."""
|
||||||
if isinstance(obj, datetime.datetime):
|
|
||||||
return obj.isoformat()
|
|
||||||
if hasattr(obj, "to_dict"):
|
if hasattr(obj, "to_dict"):
|
||||||
return obj.to_dict()
|
return obj.to_dict()
|
||||||
if isinstance(obj, multidict.MultiDict):
|
if isinstance(obj, multidict.MultiDict):
|
||||||
return obj.mixed()
|
return obj.mixed()
|
||||||
if isinstance(obj, set):
|
return jsonutils.to_primitive(obj)
|
||||||
return list(obj)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def to_json(self, data):
|
def to_json(self, data):
|
||||||
return jsonutils.dumps(data, default=self._sanitizer)
|
return jsonutils.dumps(data, default=self._sanitizer)
|
||||||
|
|
|
@ -363,8 +363,8 @@ class JSONResponseSerializerTest(test_utils.BaseTestCase):
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_to_json_with_date_format_value(self):
|
def test_to_json_with_date_format_value(self):
|
||||||
fixture = {"date": datetime.datetime(1, 3, 8, 2)}
|
fixture = {"date": datetime.datetime(1901, 3, 8, 2)}
|
||||||
expected = '{"date": "0001-03-08T02:00:00"}'
|
expected = '{"date": "1901-03-08T02:00:00.000000"}'
|
||||||
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue