comparison mercurial/templatefilters.py @ 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 a0f2d83f8083
comparison
equal deleted inserted replaced
31781:47925b63be70 31782:654e9a1c8a6c
216 if i < num_lines - 1 or endswithnewline: 216 if i < num_lines - 1 or endswithnewline:
217 yield '\n' 217 yield '\n'
218 return "".join(indenter()) 218 return "".join(indenter())
219 219
220 @templatefilter('json') 220 @templatefilter('json')
221 def json(obj): 221 def json(obj, paranoid=True):
222 if obj is None: 222 if obj is None:
223 return 'null' 223 return 'null'
224 elif obj is False: 224 elif obj is False:
225 return 'false' 225 return 'false'
226 elif obj is True: 226 elif obj is True:
227 return 'true' 227 return 'true'
228 elif isinstance(obj, (int, long, float)): 228 elif isinstance(obj, (int, long, float)):
229 return str(obj) 229 return str(obj)
230 elif isinstance(obj, str): 230 elif isinstance(obj, str):
231 return '"%s"' % encoding.jsonescape(obj, paranoid=True) 231 return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
232 elif util.safehasattr(obj, 'keys'): 232 elif util.safehasattr(obj, 'keys'):
233 out = ['%s: %s' % (json(k), json(v)) 233 out = ['%s: %s' % (json(k), json(v))
234 for k, v in sorted(obj.iteritems())] 234 for k, v in sorted(obj.iteritems())]
235 return '{' + ', '.join(out) + '}' 235 return '{' + ', '.join(out) + '}'
236 elif util.safehasattr(obj, '__iter__'): 236 elif util.safehasattr(obj, '__iter__'):