Core - v1.3.1
    Preparing search index...

    Class Json

    JSON helpers for untrusted input.

    A request body is whatever the client sent. JSON.parse answers that with an exception, which pushes a try/catch into every route that reads a body; these helpers answer with a value instead, so a handler can decide the status code in one line.

    Pure, vendor-neutral, and previously written identically in two consumers — which is the test this module's own remarks set for belonging here rather than in an application.

    const input = Json.parse<{ symbol?: string }>(await request.text())

    if (!input) {
    return fail('Body must be valid JSON')
    }

    Bayu Dwiyan Satria

    1.0.0

    1.3.0

    Index
    • Parses JSON without throwing.

      Type Parameters

      • T = unknown

        The expected shape. Unverified: this parses, it does not validate. The caller checks the fields it needs on the parsed value.

      Parameters

      • text: string

        The raw text to parse.

      Returns T

      The parsed value, or null when the text is not usable JSON.

      null covers every way the input can fail to be a usable object — malformed syntax, an empty body, or a bare null literal — because none of them is something a handler can act on differently.