zerver: Remove simplejson dependency.

Modified by tabbott to put the third-party code in a new file.

Fixes #6970.
This commit is contained in:
rht
2017-10-17 11:34:37 +02:00
committed by Tim Abbott
parent b4e67fac36
commit e169bb0954
7 changed files with 67 additions and 11 deletions

View File

@@ -0,0 +1,28 @@
import json
# Taken from
# https://github.com/simplejson/simplejson/blob/8edc82afcf6f7512b05fba32baa536fe756bd273/simplejson/encoder.py#L378-L402
# License: MIT
class JSONEncoderForHTML(json.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. &amp;) because they are not expanded
within <script> tags.
"""
def encode(self, o):
# type: (Dict[str, Any]) -> str
# Override JSONEncoder.encode because it has hacks for
# performance that make things more complicated.
chunks = self.iterencode(o, True)
return ''.join(chunks)
def iterencode(self, o, _one_shot=False):
# type: (Dict[str, Any], Optional[bool]) -> Iterator[str]
chunks = super().iterencode(o, _one_shot)
for chunk in chunks:
chunk = chunk.replace('&', '\\u0026')
chunk = chunk.replace('<', '\\u003c')
chunk = chunk.replace('>', '\\u003e')
yield chunk