From e3a981fc2c58e9603445822bf73d7fe0b22a0f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20C=C3=A2mara?= Date: Thu, 2 May 2019 16:09:10 +0100 Subject: [PATCH 1/2] Fixes #2182 possibly Fixes #2190 --- gluon/serializers.py | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/gluon/serializers.py b/gluon/serializers.py index 440b8657..a72bc216 100644 --- a/gluon/serializers.py +++ b/gluon/serializers.py @@ -119,13 +119,43 @@ def xml(value, encoding='UTF-8', key='document', quote=True): return ('' % encoding) + str(xml_rec(value, key, quote)) -def json(value, default=custom_json, indent=None, sort_keys=False): - value = json_parser.dumps(value, default=default, sort_keys=sort_keys, indent=indent) - # replace JavaScript incompatible spacing - # http://timelessrepo.com/json-isnt-a-javascript-subset - # PY3 FIXME - # return value.replace(ur'\u2028', '\\u2028').replace(ur'\2029', '\\u2029') - return value +class JSONEncoderForHTML(json_parser.JSONEncoder): + """An encoder that produces JSON safe to embed in HTML. + To embed JSON content in, say, a script tag on a web page, the + characters &, < and > should be escaped. They cannot be escaped + with the usual entities (e.g. &) because they are not expanded + within