Files
@ 4c13cedbde93
Branch filter:
Location: kallithea/rhodecode/lib/ext_json.py - annotation
4c13cedbde93
3.1 KiB
text/x-python
Added tag v1.4.0 for changeset 9ae95fdeca18
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 102 103 104 105 106 107 108 109 110 111 | 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 5c8b1eaafe77 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 775a7672d363 85a64b981c07 775a7672d363 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 775a7672d363 775a7672d363 5c8b1eaafe77 775a7672d363 775a7672d363 8a3a1a59a050 8a3a1a59a050 5c8b1eaafe77 775a7672d363 8a3a1a59a050 5c8b1eaafe77 8a3a1a59a050 8a3a1a59a050 8a3a1a59a050 8a3a1a59a050 8a3a1a59a050 5c8b1eaafe77 8a3a1a59a050 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 8a3a1a59a050 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 775a7672d363 775a7672d363 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 5c8b1eaafe77 | import datetime
import functools
import decimal
__all__ = ['json', 'simplejson', 'stdjson']
def _is_aware(value):
"""
Determines if a given datetime.time is aware.
The logic is described in Python's docs:
http://docs.python.org/library/datetime.html#datetime.tzinfo
"""
return (value.tzinfo is not None
and value.tzinfo.utcoffset(value) is not None)
def _obj_dump(obj):
"""
Custom function for dumping objects to JSON, if obj has __json__ attribute
or method defined it will be used for serialization
:param obj:
"""
if isinstance(obj, complex):
return [obj.real, obj.imag]
# See "Date Time String Format" in the ECMA-262 specification.
# some code borrowed from django 1.4
elif isinstance(obj, datetime.datetime):
r = obj.isoformat()
if obj.microsecond:
r = r[:23] + r[26:]
if r.endswith('+00:00'):
r = r[:-6] + 'Z'
return r
elif isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return str(obj)
elif isinstance(obj, datetime.time):
if _is_aware(obj):
raise ValueError("JSON can't represent timezone-aware times.")
r = obj.isoformat()
if obj.microsecond:
r = r[:12]
return r
elif isinstance(obj, set):
return list(obj)
elif hasattr(obj, '__json__'):
if callable(obj.__json__):
return obj.__json__()
else:
return obj.__json__
else:
raise NotImplementedError
# Import simplejson
try:
# import simplejson initially
import simplejson
def extended_encode(obj):
try:
return _obj_dump(obj)
except NotImplementedError:
pass
raise TypeError("%r is not JSON serializable" % (obj,))
# we handle decimals our own it makes unified behavior of json vs
# simplejson
simplejson.dumps = functools.partial(simplejson.dumps,
default=extended_encode,
use_decimal=False)
simplejson.dump = functools.partial(simplejson.dump,
default=extended_encode,
use_decimal=False)
except ImportError:
# no simplejson set it to None
simplejson = None
try:
# simplejson not found try out regular json module
import json
# extended JSON encoder for json
class ExtendedEncoder(json.JSONEncoder):
def default(self, obj):
try:
return _obj_dump(obj)
except NotImplementedError:
pass
return json.JSONEncoder.default(self, obj)
# monkey-patch JSON encoder to use extended version
json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder)
json.dump = functools.partial(json.dump, cls=ExtendedEncoder)
except ImportError:
json = None
stdlib = json
# set all available json modules
if simplejson:
json = simplejson
elif json:
json = json
else:
raise ImportError('Could not find any json modules')
|