serialization.ts 584 B

123456789101112
  1. /** Tag-safe JSON serialization for the model-visible reference envelope. */
  2. /**
  3. * Serialize JSON while preventing source data from spelling an XML-like opening tag.
  4. * @param value - JSON-compatible reference data.
  5. * @returns JSON whose parse result is unchanged and whose data contains no literal `<`.
  6. */
  7. export function stringifyTagSafeJson(value: unknown): string {
  8. const serialized: unknown = JSON.stringify(value)
  9. if (typeof serialized !== 'string') throw new TypeError('session-reference data is not JSON-serializable')
  10. return serialized.replaceAll('<', '\\u003c')
  11. }