(d, indent: int | str | None = None, sort_keys: bool = False)
| 293 | |
| 294 | @public |
| 295 | def json_encode(d, indent: int | str | None = None, sort_keys: bool = False) -> str: |
| 296 | if isinstance(d, Nothing): |
| 297 | d = [] |
| 298 | |
| 299 | # Escape < and > so the output is safe inside <script> tags with unescaped $: output. |
| 300 | # </> are valid JSON unicode escapes; all parsers decode them correctly. |
| 301 | return json.dumps(d, indent=indent, sort_keys=sort_keys).replace("<", "\\u003c").replace(">", "\\u003e") |
| 302 | |
| 303 | |
| 304 | @public |