Patchwork [3,of,4] json: avoid extra string manipulation of dict keys

login
register
mail settings
Submitter Yuya Nishihara
Date June 9, 2017, 2:42 p.m.
Message ID <cfed8eb80946df2022a9.1497019376@mimosa>
Download mbox | patch
Permalink /patch/21287/
State Accepted
Headers show

Comments

Yuya Nishihara - June 9, 2017, 2:42 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1492922418 -32400
#      Sun Apr 23 13:40:18 2017 +0900
# Node ID cfed8eb80946df2022a945708a9ae38e02768696
# Parent  b919b4f2cf8c55367764d3b2b323e9b64bae6a4e
json: avoid extra string manipulation of dict keys

A key must be string per JSON spec, and that's also true for template dicts.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -234,7 +234,7 @@  def json(obj, paranoid=True):
     elif isinstance(obj, bytes):
         return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
     elif util.safehasattr(obj, 'keys'):
-        out = ['%s: %s' % (json(k), json(v))
+        out = ['"%s": %s' % (encoding.jsonescape(k, paranoid=paranoid), json(v))
                for k, v in sorted(obj.iteritems())]
         return '{' + ', '.join(out) + '}'
     elif util.safehasattr(obj, '__iter__'):