[PATCH 1 of 7] formatter: extract function that encode values to json string

Yuya Nishihara yuya at tcha.org
Wed Sep 17 10:11:22 CDT 2014


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1410956143 -32400
#      Wed Sep 17 21:15:43 2014 +0900
# Node ID 932109f051e8f29e986ef43b0af19c673eb50a96
# Parent  48791c2bea1ceda4e4f28bc11651e281d636ce1a
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 --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -88,6 +88,12 @@ class pickleformatter(baseformatter):
         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 @@ class jsonformatter(baseformatter):
                 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)


More information about the Mercurial-devel mailing list