Revision fd0351ae 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

  
32 34
try:
33 35
  import json
34 36
except ImportError:
35 37
  # The "json" module was only added in Python 2.6. Earlier versions must use
36 38
  # the separate "simplejson" module.
37 39
  import simplejson as json
40
  _OLD_SIMPLEJSON = True
38 41

  
39 42
import re
43
import logging
40 44

  
41 45
from ganeti import errors
42 46
from ganeti import utils
......
47 51
_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE)
48 52

  
49 53

  
50
def _GetJsonDumpers(_encoder_class=json.JSONEncoder):
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):
51 71
  """Returns two JSON functions to serialize data.
52 72

  
53 73
  @rtype: (callable, callable)

Also available in: Unified diff