Revision a182a3ed

b/daemons/import-export
200 200

  
201 201
    self._data.mtime = time.time()
202 202
    utils.WriteFile(self._path,
203
                    data=serializer.DumpJson(self._data.ToDict(), indent=True),
203
                    data=serializer.DumpJson(self._data.ToDict()),
204 204
                    mode=0400)
205 205

  
206 206

  
b/lib/hypervisor/hv_kvm.py
185 185
    return QmpMessage(data)
186 186

  
187 187
  def __str__(self):
188
    # The protocol expects the JSON object to be sent as a single
189
    # line, hence the need for indent=False.
190
    return serializer.DumpJson(self.data, indent=False)
188
    # The protocol expects the JSON object to be sent as a single line.
189
    return serializer.DumpJson(self.data)
191 190

  
192 191
  def __eq__(self, other):
193 192
    # When comparing two QmpMessages, we are interested in comparing
b/lib/jqueue.py
2248 2248
      assert job.writable, "Can't update read-only job"
2249 2249

  
2250 2250
    filename = self._GetJobPath(job.id)
2251
    data = serializer.DumpJson(job.Serialize(), indent=False)
2251
    data = serializer.DumpJson(job.Serialize())
2252 2252
    logging.debug("Writing job %s to %s", job.id, filename)
2253 2253
    self._UpdateJobQueueFile(filename, data, replicate)
2254 2254

  
b/lib/luxi.py
343 343
    request[KEY_VERSION] = version
344 344

  
345 345
  # Serialize the request
346
  return serializer.DumpJson(request, indent=False)
346
  return serializer.DumpJson(request)
347 347

  
348 348

  
349 349
def CallLuxiMethod(transport_cb, method, args, version=None):
b/lib/rpc.py
442 442
      read_timeout = timeout
443 443

  
444 444
    body = serializer.DumpJson(map(self._encoder,
445
                                   zip(map(compat.snd, argdefs), args)),
446
                               indent=False)
445
                                   zip(map(compat.snd, argdefs), args)))
447 446

  
448 447
    result = self._proc(node_list, procedure, body, read_timeout=read_timeout)
449 448

  
b/lib/serializer.py
42 42
from ganeti import utils
43 43

  
44 44

  
45
_JSON_INDENT = 2
46

  
47 45
_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE)
48 46

  
49 47

  
50
def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
51
  """Returns two JSON functions to serialize data.
52

  
53
  @rtype: (callable, callable)
54
  @return: The function to generate a compact form of JSON and another one to
55
           generate a more readable, indented form of JSON (if supported)
56

  
57
  """
58
  plain_encoder = _encoder_class(sort_keys=True)
59

  
60
  # Check whether the simplejson module supports indentation
61
  try:
62
    indent_encoder = _encoder_class(indent=_JSON_INDENT, sort_keys=True)
63
  except TypeError:
64
    # Indentation not supported
65
    indent_encoder = plain_encoder
66

  
67
  return (plain_encoder.encode, indent_encoder.encode)
68

  
69

  
70
(_DumpJson, _DumpJsonIndent) = _GetJsonDumpers()
71

  
72

  
73
def DumpJson(data, indent=True):
48
def DumpJson(data):
74 49
  """Serialize a given object.
75 50

  
76 51
  @param data: the data to serialize
77
  @param indent: whether to indent output (depends on simplejson version)
78

  
79 52
  @return: the string representation of data
80 53

  
81 54
  """
82
  if indent:
83
    fn = _DumpJsonIndent
84
  else:
85
    fn = _DumpJson
55
  encoded = simplejson.dumps(data)
86 56

  
87
  txt = _RE_EOLSP.sub("", fn(data))
57
  txt = _RE_EOLSP.sub("", encoded)
88 58
  if not txt.endswith("\n"):
89 59
    txt += "\n"
90 60

  
......
112 82
  @return: the string representation of data signed by the hmac key
113 83

  
114 84
  """
115
  txt = DumpJson(data, indent=False)
85
  txt = DumpJson(data)
116 86
  if salt is None:
117 87
    salt = ""
118 88
  signed_dict = {
......
127 97

  
128 98
  signed_dict["hmac"] = utils.Sha1Hmac(key, txt, salt=salt + key_selector)
129 99

  
130
  return DumpJson(signed_dict, indent=False)
100
  return DumpJson(signed_dict)
131 101

  
132 102

  
133 103
def LoadSignedJson(txt, key):
b/lib/server/noded.py
169 169
      logging.exception("Error in RPC call")
170 170
      result = (False, "Error while executing backend function: %s" % str(err))
171 171

  
172
    return serializer.DumpJson(result, indent=False)
172
    return serializer.DumpJson(result)
173 173

  
174 174
  # the new block devices  --------------------------
175 175

  
b/lib/server/rapi.py
79 79
    @return: the body of the message
80 80

  
81 81
    """
82
    return serializer.DumpJson(values, indent=True)
82
    return serializer.DumpJson(values)
83 83

  
84 84

  
85 85
class RemoteApiHttpServer(http.auth.HttpServerRequestAuthentication,
b/test/ganeti.hypervisor.hv_kvm_unittest.py
101 101
    conn.close()
102 102

  
103 103
  def encode_string(self, message):
104
    return (serializer.DumpJson(message, indent=False) +
104
    return (serializer.DumpJson(message) +
105 105
            hv_kvm.QmpConnection._MESSAGE_END_TOKEN)
106 106

  
107 107

  
b/test/ganeti.serializer_unittest.py
52 52
    ]
53 53

  
54 54
  def _TestSerializer(self, dump_fn, load_fn):
55
    for indent in [True, False]:
56
      for data in self._TESTDATA:
57
        self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
58
        self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
55
    for data in self._TESTDATA:
56
      self.failUnless(dump_fn(data).endswith("\n"))
57
      self.assertEqualValues(load_fn(dump_fn(data)), data)
59 58

  
60 59
  def testGeneric(self):
61 60
    self._TestSerializer(serializer.Dump, serializer.Load)

Also available in: Unified diff