import type { MessageFormat } from "../sendrecv.ts"; export function jsonFormat(): MessageFormat { return { parser: parseJson, formatter: formatJson, }; } function parseJson(data: any): T { const throwUnexpectedFormat = () => { throw new Error("Expected message to be a JSON string"); }; if (typeof data !== "string") { return throwUnexpectedFormat(); } try { return JSON.parse(data); } catch { return throwUnexpectedFormat(); } } function formatJson(message: T): string { return JSON.stringify(message); }