Print JSON without crashing on Windows terminals with GBK encoding.
(data: Dict[str, Any])
| 659 | |
| 660 | |
| 661 | def emit_json(data: Dict[str, Any]) -> None: |
| 662 | """Print JSON without crashing on Windows terminals with GBK encoding.""" |
| 663 | payload = json.dumps(data, indent=2, ensure_ascii=False) |
| 664 | try: |
| 665 | if hasattr(sys.stdout, "reconfigure"): |
| 666 | sys.stdout.reconfigure(encoding="utf-8", errors="replace") |
| 667 | print(payload) |
| 668 | except UnicodeEncodeError: |
| 669 | sys.stdout.buffer.write(payload.encode("utf-8", errors="replace")) |
| 670 | sys.stdout.buffer.write(b"\n") |
| 671 | |
| 672 | |
| 673 | if __name__ == "__main__": |