Patchwork [2,of,7] formatter: have jsonformatter accept tuple as value

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

Comments

Yuya Nishihara - Sept. 17, 2014, 3:11 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1410957022 -32400
#      Wed Sep 17 21:30:22 2014 +0900
# Node ID 97d8605b1d91dad17643b7e34a740781c947a8af
# Parent  932109f051e8f29e986ef43b0af19c673eb50a96
formatter: have jsonformatter accept tuple as value

This is necessary for "annotate" to encode ctx.date() in the same manner
as jsonchangeset printer.

It doesn't support list object because keeping mutable object in _item could
be a source of hidden bugs.  Also, I can't think of the use case.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -89,7 +89,9 @@  class pickleformatter(baseformatter):
         self._ui.write(cPickle.dumps(self._data))
 
 def _jsonifyobj(v):
-    if isinstance(v, int):
+    if isinstance(v, tuple):
+        return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
+    elif isinstance(v, int):
         return '%d' % v
     else:
         return '"%s"' % encoding.jsonescape(v)