formatter: extract function that encode values to json string
authorYuya Nishihara <yuya@tcha.org>
Wed, 17 Sep 2014 21:15:43 +0900
changeset 22474 9da0ef363861
parent 22473 ca85306b3eb2
child 22475 17eeda31e52b
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.
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)