annotate rhodecode/lib/ext_json.py @ 2578:d24c70ec9312 beta

Fixed issue with gzipped streams for large git pushes.
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 09 Jul 2012 03:38:18 +0200
parents 5c8b1eaafe77
children 3ae42e10b665
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 import datetime
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 import functools
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 import decimal
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 __all__ = ['json', 'simplejson', 'stdjson']
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 def _is_aware(value):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 """
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 Determines if a given datetime.time is aware.
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 The logic is described in Python's docs:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 http://docs.python.org/library/datetime.html#datetime.tzinfo
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 """
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 return (value.tzinfo is not None
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 and value.tzinfo.utcoffset(value) is not None)
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 def _obj_dump(obj):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 """
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 Custom function for dumping objects to JSON, if obj has __json__ attribute
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 or method defined it will be used for serialization
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 :param obj:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 """
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 if isinstance(obj, complex):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 return [obj.real, obj.imag]
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 # See "Date Time String Format" in the ECMA-262 specification.
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 # some code borrowed from django 1.4
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 elif isinstance(obj, datetime.datetime):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 r = obj.isoformat()
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 if obj.microsecond:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 r = r[:23] + r[26:]
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 if r.endswith('+00:00'):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 r = r[:-6] + 'Z'
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 return r
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 elif isinstance(obj, datetime.date):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 return obj.isoformat()
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 elif isinstance(obj, decimal.Decimal):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 return str(obj)
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 elif isinstance(obj, datetime.time):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 if _is_aware(obj):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 raise ValueError("JSON can't represent timezone-aware times.")
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 r = obj.isoformat()
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 if obj.microsecond:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 r = r[:12]
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 return r
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 elif isinstance(obj, set):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 return list(obj)
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 elif hasattr(obj, '__json__'):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 if callable(obj.__json__):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 return obj.__json__()
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 else:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 return obj.__json__
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 else:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 raise NotImplementedError
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 # Import simplejson
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 try:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 # import simplejson initially
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
63 import simplejson
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 def extended_encode(obj):
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 try:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 return _obj_dump(obj)
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 except NotImplementedError:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 pass
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 raise TypeError("%r is not JSON serializable" % (obj,))
2174
85a64b981c07 ws cleanup, +changelog
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
71 # we handle decimals our own it makes unified behavior of json vs
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 # simplejson
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
73 simplejson.dumps = functools.partial(simplejson.dumps,
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
74 default=extended_encode,
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
75 use_decimal=False)
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
76 simplejson.dump = functools.partial(simplejson.dump,
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
77 default=extended_encode,
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
78 use_decimal=False)
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 except ImportError:
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 # no simplejson set it to None
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
81 simplejson = None
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83
2258
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
84 try:
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
85 # simplejson not found try out regular json module
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
86 import json
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87
2258
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
88 # extended JSON encoder for json
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
89 class ExtendedEncoder(json.JSONEncoder):
2258
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
90 def default(self, obj):
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
91 try:
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
92 return _obj_dump(obj)
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
93 except NotImplementedError:
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
94 pass
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
95 return json.JSONEncoder.default(self, obj)
2258
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
96 # monkey-patch JSON encoder to use extended version
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
97 json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder)
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
98 json.dump = functools.partial(json.dump, cls=ExtendedEncoder)
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
99
2258
8a3a1a59a050 Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 2174
diff changeset
100 except ImportError:
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
101 json = None
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
102
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
103 stdlib = json
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 # set all available json modules
2528
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
106 if simplejson:
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
107 json = simplejson
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
108 elif json:
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
109 json = json
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
110 else:
5c8b1eaafe77 Simplified ext_json thing, for better scope resolution in pydev
Marcin Kuzminski <marcin@python-works.com>
parents: 2258
diff changeset
111 raise ImportError('Could not find any json modules')