Serialize ``value`` to a single NDJSON-safe JSON string. Mirrors ``ndjsonSafeStringify`` from the TypeScript implementation.
(value: Any)
| 22 | |
| 23 | |
| 24 | def ndjson_safe_dumps(value: Any) -> str: |
| 25 | """Serialize ``value`` to a single NDJSON-safe JSON string. |
| 26 | |
| 27 | Mirrors ``ndjsonSafeStringify`` from the TypeScript implementation. |
| 28 | """ |
| 29 | |
| 30 | encoded = json.dumps(value, ensure_ascii=False, separators=(",", ":")) |
| 31 | if "\u2028" not in encoded and "\u2029" not in encoded: |
| 32 | return encoded |
| 33 | out_parts: list[str] = [] |
| 34 | for ch in encoded: |
| 35 | replacement = _LINE_SEPARATORS.get(ch) |
| 36 | out_parts.append(replacement if replacement is not None else ch) |
| 37 | return "".join(out_parts) |