Merge "rpc: clean JSON serializer, remove strtime() usage"

This commit is contained in:
Jenkins 2015-06-04 08:36:19 +00:00 committed by Gerrit Code Review
commit ea62e2e7a9
2 changed files with 10 additions and 7 deletions

View File

@ -54,20 +54,23 @@ CONF.register_opts(rpc_opts)
class RPCJSONSerializer(wsgi.JSONResponseSerializer): class RPCJSONSerializer(wsgi.JSONResponseSerializer):
def _sanitizer(self, obj): @staticmethod
def to_primitive(_type, _value): def _to_primitive(_type, _value):
return {"_type": _type, "_value": _value} return {"_type": _type, "_value": _value}
def _sanitizer(self, obj):
if isinstance(obj, datetime.datetime): if isinstance(obj, datetime.datetime):
return to_primitive("datetime", timeutils.strtime(obj)) return self._to_primitive("datetime",
obj.isoformat())
return super(RPCJSONSerializer, self)._sanitizer(obj) return super(RPCJSONSerializer, self)._sanitizer(obj)
class RPCJSONDeserializer(wsgi.JSONRequestDeserializer): class RPCJSONDeserializer(wsgi.JSONRequestDeserializer):
def _to_datetime(self, obj): @staticmethod
return timeutils.parse_strtime(obj) def _to_datetime(obj):
return timeutils.normalize_time(timeutils.parse_isotime(obj))
def _sanitizer(self, obj): def _sanitizer(self, obj):
try: try:

View File

@ -260,7 +260,7 @@ class TestRPCJSONSerializer(test_utils.BaseTestCase):
def test_to_json_with_date_format_value(self): def test_to_json_with_date_format_value(self):
fixture = {"date": datetime.datetime(1900, 3, 8, 2)} fixture = {"date": datetime.datetime(1900, 3, 8, 2)}
expected = {"date": {"_value": "1900-03-08T02:00:00.000000", expected = {"date": {"_value": "1900-03-08T02:00:00",
"_type": "datetime"}} "_type": "datetime"}}
actual = rpc.RPCJSONSerializer().to_json(fixture) actual = rpc.RPCJSONSerializer().to_json(fixture)
actual = jsonutils.loads(actual) actual = jsonutils.loads(actual)