/usr/lib/python2.7/dist-packages/chef/utils/json.py is in python-chef 0.2.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from __future__ import absolute_import
import types
try:
import json
except ImportError:
import simplejson as json
def maybe_call(x):
if callable(x):
return x()
return x
class JSONEncoder(json.JSONEncoder):
"""Custom encoder to allow arbitrary classes."""
def default(self, obj):
if hasattr(obj, 'to_dict'):
return maybe_call(obj.to_dict)
elif hasattr(obj, 'to_list'):
return maybe_call(obj.to_list)
elif isinstance(obj, types.GeneratorType):
return list(obj)
return super(JSONEncoder, self).default(obj)
loads = json.loads
dumps = lambda obj, **kwargs: json.dumps(obj, cls=JSONEncoder, **kwargs)
|