Parses JSON from UTF-8 bytes or a string.
A one-call replacement for JSON.parse(new TextDecoder().decode(bytes)) that also accepts a string directly, and reports a bad payload as a uniform ProtocolError rather than a raw SyntaxError.
JSON.parse(new TextDecoder().decode(bytes))
SyntaxError
The expected shape of the decoded value.
The JSON payload, as UTF-8 bytes or a string.
The parsed value.
If the payload is not valid JSON.
1.0.4
import { decodeJson } from 'edgeport/util';decodeJson<{ ok: boolean }>(new TextEncoder().encode('{"ok":true}')); // { ok: true } Copy
import { decodeJson } from 'edgeport/util';decodeJson<{ ok: boolean }>(new TextEncoder().encode('{"ok":true}')); // { ok: true }
Parses JSON from UTF-8 bytes or a string.
A one-call replacement for
JSON.parse(new TextDecoder().decode(bytes))that also accepts a string directly, and reports a bad payload as a uniform ProtocolError rather than a rawSyntaxError.