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

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 17, 2014, 3:11 p.m.
Message ID <932109f051e8f29e986e.1410966682@mimosa>
Download mbox | patch
Permalink /patch/5844/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 17, 2014, 3:11 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

Patch

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)