1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
5
# Copyright 2011 Justin Santa Barbara
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
21
JSON related utilities.
23
This module provides a few things:
25
1) A handy function for getting an object down to something that can be
26
JSON serialized. See to_primitive().
28
2) Wrappers around loads() and dumps(). The dumps() wrapper will
29
automatically use to_primitive() for you if needed.
31
3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson
42
from glance.openstack.common import timeutils
45
def to_primitive(value, convert_instances=False, level=0):
46
"""Convert a complex object into primitives.
48
Handy for JSON serialization. We can optionally handle instances,
49
but since this is a recursive function, we could have cyclical
52
To handle cyclical data structures we could track the actual objects
53
visited in a set, but not all objects are hashable. Instead we just
54
track the depth of the object inspections and don't go too deep.
56
Therefore, convert_instances=True is lossy ... be aware.
59
nasty = [inspect.ismodule, inspect.isclass, inspect.ismethod,
60
inspect.isfunction, inspect.isgeneratorfunction,
61
inspect.isgenerator, inspect.istraceback, inspect.isframe,
62
inspect.iscode, inspect.isbuiltin, inspect.isroutine,
68
# value of itertools.count doesn't get caught by inspects
69
# above and results in infinite loop when list(value) is called.
70
if type(value) == itertools.count:
73
# FIXME(vish): Workaround for LP bug 852095. Without this workaround,
74
# tests that raise an exception in a mocked method that
75
# has a @wrap_exception with a notifier will fail. If
76
# we up the dependency to 0.5.4 (when it is released) we
77
# can remove this workaround.
78
if getattr(value, '__module__', None) == 'mox':
84
# The try block may not be necessary after the class check above,
85
# but just in case ...
87
# It's not clear why xmlrpclib created their own DateTime type, but
88
# for our purposes, make it a datetime type which is explicitly
90
if isinstance(value, xmlrpclib.DateTime):
91
value = datetime.datetime(*tuple(value.timetuple())[:6])
93
if isinstance(value, (list, tuple)):
96
o.append(to_primitive(v, convert_instances=convert_instances,
99
elif isinstance(value, dict):
101
for k, v in value.iteritems():
102
o[k] = to_primitive(v, convert_instances=convert_instances,
105
elif isinstance(value, datetime.datetime):
106
return timeutils.strtime(value)
107
elif hasattr(value, 'iteritems'):
108
return to_primitive(dict(value.iteritems()),
109
convert_instances=convert_instances,
111
elif hasattr(value, '__iter__'):
112
return to_primitive(list(value),
113
convert_instances=convert_instances,
115
elif convert_instances and hasattr(value, '__dict__'):
116
# Likely an instance of something. Watch for cycles.
117
# Ignore class member vars.
118
return to_primitive(value.__dict__,
119
convert_instances=convert_instances,
124
# Class objects are tricky since they may define something like
125
# __iter__ defined but it isn't callable as list().
126
return unicode(value)
129
def dumps(value, default=to_primitive, **kwargs):
130
return json.dumps(value, default=default, **kwargs)
146
anyjson._modules.append((__name__, 'dumps', TypeError,
147
'loads', ValueError, 'load'))
148
anyjson.force_implementation(__name__)