/usr/share/pyshared/tg/jsonify.py is in python-turbogears2 2.1.5-2.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | """JSON encoding functions."""
import datetime
import decimal
from simplejson import JSONEncoder
from webob.multidict import MultiDict
class NotExistingImport:
pass
try:
import sqlalchemy
from sqlalchemy.engine.base import ResultProxy, RowProxy
except ImportError:
ResultProxy=NotExistingImport
RowProxy=NotExistingImport
try:
from bson import ObjectId
except ImportError:
ObjectId=NotExistingImport
def is_saobject(obj):
return hasattr(obj, '_sa_class_manager')
class JsonEncodeError(Exception):
"""JSON Encode error"""
class GenericJSON(JSONEncoder):
"""JSON Encoder class"""
def default(self, obj):
if hasattr(obj, '__json__') and callable(obj.__json__):
return obj.__json__()
elif isinstance(obj, (datetime.date, datetime.datetime)):
return str(obj)
elif isinstance(obj, decimal.Decimal):
return float(obj)
elif is_saobject(obj):
props = {}
for key in obj.__dict__:
if not key.startswith('_sa_'):
props[key] = getattr(obj, key)
return props
elif isinstance(obj, ResultProxy):
return dict(rows=list(obj), count=obj.rowcount)
elif isinstance(obj, RowProxy):
return dict(rows=dict(obj), count=1)
elif isinstance(obj, ObjectId):
return str(obj)
elif isinstance(obj, MultiDict):
return obj.mixed()
else:
return JSONEncoder.default(self, obj)
try:
from simplegeneric import generic
_default = GenericJSON()
@generic
def jsonify(obj):
return _default.default(obj)
class GenericFunctionJSON(GenericJSON):
"""Generic Function JSON Encoder class."""
def default(self, obj):
return jsonify(obj)
_instance = GenericFunctionJSON()
except ImportError:
def jsonify(obj):
raise ImportError('simplegeneric is not installed')
_instance = GenericJSON()
# General encoding functions
def encode(obj):
"""Return a JSON string representation of a Python object."""
if isinstance(obj, basestring):
return _instance.encode(obj)
try:
value = obj['test']
except TypeError:
if not hasattr(obj, '__json__') and not is_saobject(obj):
raise JsonEncodeError('Your Encoded object must be dict-like.')
except:
pass
return _instance.encode(obj)
def encode_iter(obj):
"""Encode object, yielding each string representation as available."""
return _instance.iterencode(obj)
|