From 12e663a8b0b253970e3bccd373d3d2f3d462f6b6 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Mon, 16 Jul 2012 12:20:44 -0400 Subject: Sync jsonutils from openstack-common. In addition to the following changes from openstack-common, this patch includes some tweak to nova unit tests. timeutils.strtime() will raise an exception if the year in a datetime object is before 1900. Changes from openstack-common: commit ce3071437d1871f77c4d8573cbe5f4ea8c817650 Author: Russell Bryant Date: Mon Jul 16 10:30:25 2012 -0400 Use strtime() in to_primitive() for datetime objs. This patch updates jsonutils.to_primitive() to use timeutils.strtime() to convert a datimetime object to a string instead of just using str(). This ensures that we can easily convert the string back to a datetime using timeutils.parse_strtime(). Required for the nova blueprint no-db-messaging. commit 4c9d439ef24f5afdd74aa9153aa8fc772051e6cb Author: Tim Daly Jr Date: Tue Jun 26 02:48:42 2012 +0000 Add 'filedecoder' method to the jsonutils wrapper module. Fixes bug #1017765 After version 3.3.2, the anyjson library will throw a KeyError if filedecoder isn't present. The filedecoder is just like the decoder except it takes a file instead of a string, like json.load() instead of json.loads(). Change-Id: Ib51f0da8641c035371e09047de9abe3cb83203e9 --- nova/openstack/common/jsonutils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'nova/openstack') diff --git a/nova/openstack/common/jsonutils.py b/nova/openstack/common/jsonutils.py index 752266981..5f6a7edab 100644 --- a/nova/openstack/common/jsonutils.py +++ b/nova/openstack/common/jsonutils.py @@ -39,6 +39,8 @@ import itertools import json import xmlrpclib +from nova.openstack.common import timeutils + def to_primitive(value, convert_instances=False, level=0): """Convert a complex object into primitives. @@ -101,7 +103,7 @@ def to_primitive(value, convert_instances=False, level=0): level=level) return o elif isinstance(value, datetime.datetime): - return str(value) + return timeutils.strtime(value) elif hasattr(value, 'iteritems'): return to_primitive(dict(value.iteritems()), convert_instances=convert_instances, @@ -130,11 +132,15 @@ def loads(s): return json.loads(s) +def load(s): + return json.load(s) + + try: import anyjson except ImportError: pass else: anyjson._modules.append((__name__, 'dumps', TypeError, - 'loads', ValueError)) + 'loads', ValueError, 'load')) anyjson.force_implementation(__name__) -- cgit