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