# HG changeset patch # User Yuya Nishihara # Date 1410956143 -32400 # Node ID 9da0ef3638613c4a55dd9c76a7db945a2a1cf555 # Parent ca85306b3eb225d0d86a4dd90586a279bf9d3322 formatter: extract function that encode values to json string This is the stub for tuple support, which will be used to encode ctx.date() in the same manner as jsonchangeset printer. diff -r ca85306b3eb2 -r 9da0ef363861 mercurial/formatter.py --- a/mercurial/formatter.py Fri Sep 12 21:38:52 2014 -0400 +++ b/mercurial/formatter.py Wed Sep 17 21:15:43 2014 +0900 @@ -88,6 +88,12 @@ baseformatter.end(self) self._ui.write(cPickle.dumps(self._data)) +def _jsonifyobj(v): + if isinstance(v, int): + return '%d' % v + else: + return '"%s"' % encoding.jsonescape(v) + class jsonformatter(baseformatter): def __init__(self, ui, topic, opts): baseformatter.__init__(self, ui, topic, opts) @@ -106,10 +112,7 @@ first = False else: self._ui.write(",\n") - if isinstance(v, int): - self._ui.write(' "%s": %d' % (k, v)) - else: - self._ui.write(' "%s": "%s"' % (k, encoding.jsonescape(v))) + self._ui.write(' "%s": %s' % (k, _jsonifyobj(v))) self._ui.write("\n }") def end(self): baseformatter.end(self)