Mercurial > kallithea
annotate rhodecode/lib/ext_json.py @ 2838:ba09e335f266 beta
updated windows installation with note of 32 bit system used in example
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 13 Sep 2012 21:14:11 +0200 |
parents | 3ae42e10b665 |
children | 83159d73b13b |
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 | |
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 | 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 |
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 | 79 except ImportError: |
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 | 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 |
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 | 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 |
2817
3ae42e10b665
fix ext-json extension issue when exception is raised for non-serializable objects
Marcin Kuzminski <marcin@python-works.com>
parents:
2528
diff
changeset
|
95 raise TypeError("%r is not JSON serializable" % (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 | 104 |
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') |