Mercurial > kallithea
annotate rhodecode/lib/ext_json.py @ 2310:83e4447077d3 beta
Improved i18n for the comment count (use of ngettext for pluralisation).
author | Vincent Duvert <vincent@duvert.net> |
---|---|
date | Sun, 20 May 2012 10:47:33 +0200 |
parents | 8a3a1a59a050 |
children | 5c8b1eaafe77 |
rev | line source |
---|---|
2173 | 1 import datetime |
2 import functools | |
3 import decimal | |
4 | |
5 __all__ = ['json', 'simplejson', 'stdjson'] | |
6 | |
7 | |
8 def _is_aware(value): | |
9 """ | |
10 Determines if a given datetime.time is aware. | |
11 | |
12 The logic is described in Python's docs: | |
13 http://docs.python.org/library/datetime.html#datetime.tzinfo | |
14 """ | |
15 return (value.tzinfo is not None | |
16 and value.tzinfo.utcoffset(value) is not None) | |
17 | |
18 | |
19 def _obj_dump(obj): | |
20 """ | |
21 Custom function for dumping objects to JSON, if obj has __json__ attribute | |
22 or method defined it will be used for serialization | |
23 | |
24 :param obj: | |
25 """ | |
26 | |
27 if isinstance(obj, complex): | |
28 return [obj.real, obj.imag] | |
29 # See "Date Time String Format" in the ECMA-262 specification. | |
30 # some code borrowed from django 1.4 | |
31 elif isinstance(obj, datetime.datetime): | |
32 r = obj.isoformat() | |
33 if obj.microsecond: | |
34 r = r[:23] + r[26:] | |
35 if r.endswith('+00:00'): | |
36 r = r[:-6] + 'Z' | |
37 return r | |
38 elif isinstance(obj, datetime.date): | |
39 return obj.isoformat() | |
40 elif isinstance(obj, decimal.Decimal): | |
41 return str(obj) | |
42 elif isinstance(obj, datetime.time): | |
43 if _is_aware(obj): | |
44 raise ValueError("JSON can't represent timezone-aware times.") | |
45 r = obj.isoformat() | |
46 if obj.microsecond: | |
47 r = r[:12] | |
48 return r | |
49 elif isinstance(obj, set): | |
50 return list(obj) | |
51 elif hasattr(obj, '__json__'): | |
52 if callable(obj.__json__): | |
53 return obj.__json__() | |
54 else: | |
55 return obj.__json__ | |
56 else: | |
57 raise NotImplementedError | |
58 | |
59 | |
60 # Import simplejson | |
61 try: | |
62 # import simplejson initially | |
63 import simplejson as _sj | |
64 | |
65 def extended_encode(obj): | |
66 try: | |
67 return _obj_dump(obj) | |
68 except NotImplementedError: | |
69 pass | |
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 | 72 # simplejson |
73 _sj.dumps = functools.partial(_sj.dumps, default=extended_encode, | |
74 use_decimal=False) | |
75 _sj.dump = functools.partial(_sj.dump, default=extended_encode, | |
76 use_decimal=False) | |
77 simplejson = _sj | |
78 | |
79 except ImportError: | |
80 # no simplejson set it to None | |
81 _sj = None | |
82 | |
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 |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
86 import json as _json |
2173 | 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 |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
89 class ExtendedEncoder(_json.JSONEncoder): |
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 |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
95 return _json.JSONEncoder.default(self, obj) |
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 |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
97 _json.dumps = functools.partial(_json.dumps, cls=ExtendedEncoder) |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
98 _json.dump = functools.partial(_json.dump, cls=ExtendedEncoder) |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
99 stdlib = _json |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
100 except ImportError: |
8a3a1a59a050
Fixed simplejson import on python 2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
2174
diff
changeset
|
101 _json = None |
2173 | 102 |
103 # set all available json modules | |
104 simplejson = _sj | |
105 stdjson = _json | |
106 json = _sj if _sj else _json |