Revision cdeda3b6 lib/serializer.py
b/lib/serializer.py | ||
---|---|---|
29 | 29 |
# C0103: Invalid name, since pylint doesn't see that Dump points to a |
30 | 30 |
# function and not a constant |
31 | 31 |
|
32 |
_OLD_SIMPLEJSON = False |
|
33 |
|
|
34 |
try: |
|
35 |
import json |
|
36 |
except ImportError: |
|
37 |
# The "json" module was only added in Python 2.6. Earlier versions must use |
|
38 |
# the separate "simplejson" module. |
|
39 |
import simplejson as json |
|
40 |
_OLD_SIMPLEJSON = True |
|
41 |
|
|
32 |
import simplejson |
|
42 | 33 |
import re |
43 |
import logging |
|
44 | 34 |
|
45 | 35 |
from ganeti import errors |
46 | 36 |
from ganeti import utils |
... | ... | |
51 | 41 |
_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE) |
52 | 42 |
|
53 | 43 |
|
54 |
class _CustomJsonEncoder(json.JSONEncoder): |
|
55 |
if __debug__ and not _OLD_SIMPLEJSON: |
|
56 |
try: |
|
57 |
_orig_fn = json.JSONEncoder._iterencode_dict |
|
58 |
except AttributeError: |
|
59 |
raise Exception("Can't override JSONEncoder's '_iterencode_dict'") |
|
60 |
else: |
|
61 |
def _iterencode_dict(self, data, *args, **kwargs): |
|
62 |
for key in data.keys(): |
|
63 |
if not (key is None or isinstance(key, (basestring, bool))): |
|
64 |
raise ValueError("Key '%s' is of disallowed type '%s'" % |
|
65 |
(key, type(key))) |
|
66 |
|
|
67 |
return self._orig_fn(data, *args, **kwargs) |
|
68 |
|
|
69 |
|
|
70 |
def _GetJsonDumpers(_encoder_class=_CustomJsonEncoder): |
|
44 |
def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder): |
|
71 | 45 |
"""Returns two JSON functions to serialize data. |
72 | 46 |
|
73 | 47 |
@rtype: (callable, callable) |
... | ... | |
119 | 93 |
@return: the original data |
120 | 94 |
|
121 | 95 |
""" |
122 |
return json.loads(txt) |
|
96 |
return simplejson.loads(txt)
|
|
123 | 97 |
|
124 | 98 |
|
125 | 99 |
def DumpSignedJson(data, key, salt=None, key_selector=None): |
Also available in: Unified diff