Patchwork [4,of,4] formatter: use templatefilters.json()

login
register
mail settings
Submitter Yuya Nishihara
Date April 2, 2017, 3:28 a.m.
Message ID <6deb06584ae481c751b8.1491103704@mimosa>
Download mbox | patch
Permalink /patch/19904/
State Accepted
Headers show

Comments

Yuya Nishihara - April 2, 2017, 3:28 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491102137 -32400
#      Sun Apr 02 12:02:17 2017 +0900
# Node ID 6deb06584ae481c751b86782e2c8567e4996d04e
# Parent  eba702fdc933ed84d5f53225bfe87ebc40c7831e
formatter: use templatefilters.json()

Now _jsonifyobj() is identical to templatefilters.json(paranoid=False).
David Soria Parra - April 3, 2017, 4:29 p.m.
On Sun, Apr 02, 2017 at 12:28:24PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1491102137 -32400
> #      Sun Apr 02 12:02:17 2017 +0900
> # Node ID 6deb06584ae481c751b86782e2c8567e4996d04e
> # Parent  eba702fdc933ed84d5f53225bfe87ebc40c7831e
> formatter: use templatefilters.json()

This series LGTM for what it's worth.
Augie Fackler - April 3, 2017, 7:04 p.m.
On Sun, Apr 02, 2017 at 12:28:24PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1491102137 -32400
> #      Sun Apr 02 12:02:17 2017 +0900
> # Node ID 6deb06584ae481c751b86782e2c8567e4996d04e
> # Parent  eba702fdc933ed84d5f53225bfe87ebc40c7831e
> formatter: use templatefilters.json()

queued per dsop's review, thanks

>
> Now _jsonifyobj() is identical to templatefilters.json(paranoid=False).
>
> diff --git a/mercurial/formatter.py b/mercurial/formatter.py
> --- a/mercurial/formatter.py
> +++ b/mercurial/formatter.py
> @@ -112,8 +112,8 @@ from .node import (
>  )
>
>  from . import (
> -    encoding,
>      error,
> +    templatefilters,
>      templatekw,
>      templater,
>      util,
> @@ -282,24 +282,6 @@ class pickleformatter(baseformatter):
>          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 @@ class jsonformatter(baseformatter):
>                  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)
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -218,7 +218,7 @@ def indent(text, prefix):
>      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 @@ def json(obj):
>      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())]
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -112,8 +112,8 @@  from .node import (
 )
 
 from . import (
-    encoding,
     error,
+    templatefilters,
     templatekw,
     templater,
     util,
@@ -282,24 +282,6 @@  class pickleformatter(baseformatter):
         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 @@  class jsonformatter(baseformatter):
                 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)
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -218,7 +218,7 @@  def indent(text, prefix):
     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 @@  def json(obj):
     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())]