changeset 31782:654e9a1c8a6c

formatter: use templatefilters.json() Now _jsonifyobj() is identical to templatefilters.json(paranoid=False).
author Yuya Nishihara <yuya@tcha.org>
date Sun, 02 Apr 2017 12:02:17 +0900
parents 47925b63be70
children 70377de005a0
files mercurial/formatter.py mercurial/templatefilters.py
diffstat 2 files changed, 5 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/formatter.py	Sun Apr 02 11:54:24 2017 +0900
+++ b/mercurial/formatter.py	Sun Apr 02 12:02:17 2017 +0900
@@ -112,8 +112,8 @@
 )
 
 from . import (
-    encoding,
     error,
+    templatefilters,
     templatekw,
     templater,
     util,
@@ -282,24 +282,6 @@
         baseformatter.end(self)
         self._out.write(pickle.dumps(self._data))
 
-def _jsonifyobj(v):
-    if isinstance(v, dict):
-        xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u))
-              for k, u in sorted(v.iteritems())]
-        return '{' + ', '.join(xs) + '}'
-    elif isinstance(v, (list, tuple)):
-        return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
-    elif v is None:
-        return 'null'
-    elif v is True:
-        return 'true'
-    elif v is False:
-        return 'false'
-    elif isinstance(v, (int, long, float)):
-        return str(v)
-    else:
-        return '"%s"' % encoding.jsonescape(v)
-
 class jsonformatter(baseformatter):
     def __init__(self, ui, out, topic, opts):
         baseformatter.__init__(self, ui, topic, opts, _nullconverter)
@@ -319,7 +301,8 @@
                 first = False
             else:
                 self._out.write(",\n")
-            self._out.write('  "%s": %s' % (k, _jsonifyobj(v)))
+            u = templatefilters.json(v, paranoid=False)
+            self._out.write('  "%s": %s' % (k, u))
         self._out.write("\n }")
     def end(self):
         baseformatter.end(self)
--- a/mercurial/templatefilters.py	Sun Apr 02 11:54:24 2017 +0900
+++ b/mercurial/templatefilters.py	Sun Apr 02 12:02:17 2017 +0900
@@ -218,7 +218,7 @@
     return "".join(indenter())
 
 @templatefilter('json')
-def json(obj):
+def json(obj, paranoid=True):
     if obj is None:
         return 'null'
     elif obj is False:
@@ -228,7 +228,7 @@
     elif isinstance(obj, (int, long, float)):
         return str(obj)
     elif isinstance(obj, str):
-        return '"%s"' % encoding.jsonescape(obj, paranoid=True)
+        return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
     elif util.safehasattr(obj, 'keys'):
         out = ['%s: %s' % (json(k), json(v))
                for k, v in sorted(obj.iteritems())]