2017-10-17 11:34:37 +02:00
|
|
|
import json
|
2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Iterator
|
2017-10-17 11:34:37 +02:00
|
|
|
|
|
|
|
# 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. &) because they are not expanded
|
|
|
|
within <script> tags.
|
|
|
|
"""
|
|
|
|
|
2017-11-22 10:32:18 +01:00
|
|
|
def encode(self, o: Any) -> str:
|
2017-10-17 11:34:37 +02:00
|
|
|
# Override JSONEncoder.encode because it has hacks for
|
|
|
|
# performance that make things more complicated.
|
|
|
|
chunks = self.iterencode(o, True)
|
|
|
|
return ''.join(chunks)
|
|
|
|
|
2017-11-22 10:32:18 +01:00
|
|
|
def iterencode(self, o: Any, _one_shot: bool=False) -> Iterator[str]:
|
2017-10-17 11:34:37 +02:00
|
|
|
chunks = super().iterencode(o, _one_shot)
|
|
|
|
for chunk in chunks:
|
|
|
|
chunk = chunk.replace('&', '\\u0026')
|
|
|
|
chunk = chunk.replace('<', '\\u003c')
|
|
|
|
chunk = chunk.replace('>', '\\u003e')
|
|
|
|
yield chunk
|