Skip to content

Instantly share code, notes, and snippets.

@mriedem
Created December 22, 2015 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mriedem/6aa2085b1ecbfd78057f to your computer and use it in GitHub Desktop.
Save mriedem/6aa2085b1ecbfd78057f to your computer and use it in GitHub Desktop.
diff --git a/nova/openstack/common/rpc/impl_fake.py b/nova/openstack/common/rpc/impl_fake.py
index 8210671..a8e2e9f 100644
--- a/nova/openstack/common/rpc/impl_fake.py
+++ b/nova/openstack/common/rpc/impl_fake.py
@@ -1,5 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
+# Copyright 2013 IBM Corp.
# Copyright 2011 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -17,6 +18,7 @@
queues. Casts will block, but this is very useful for tests.
"""
+import datetime
import inspect
# NOTE(russellb): We specifically want to use json, not our own jsonutils.
# jsonutils has some extra logic to automatically convert objects to primitive
@@ -32,6 +34,14 @@ from nova.openstack.common.rpc import common as rpc_common
CONSUMERS = {}
+class CustomizedJSONEncoder(json.JSONEncoder):
+ """ customized json encoder for datatime type."""
+ def default(self, obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.strftime('%Y-%m-%dT%H:%M:%S')
+ return json.JSONEncoder.default(self, obj)
+
+
class RpcContext(rpc_common.CommonRpcContext):
def __init__(self, **kwargs):
super(RpcContext, self).__init__(**kwargs)
@@ -128,7 +138,7 @@ def create_connection(conf, new=True):
def check_serialize(msg):
"""Make sure a message intended for rpc can be serialized."""
- json.dumps(msg)
+ json.dumps(msg, cls=CustomizedJSONEncoder)
def multicall(conf, context, topic, msg, timeout=None):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment