dotnet/aspnetcore · error · Error

Invalid input for JSON hub protocol. Expected a string.

Error message

Invalid input for JSON hub protocol. Expected a string.

What it means

Thrown by JsonHubProtocol.parseMessages when `input` is not a `string`. The IHubProtocol interface technically allows ArrayBuffer, but the JSON protocol implementation only operates on Text transfer format, so any non-string input (ArrayBuffer, Uint8Array, number) is rejected with a descriptive message before parsing.

Solutions

  1. Use the MessagePackHubProtocol when you need binary transfer.
  2. If you must feed bytes to JSON, decode to a string first (TextDecoder) before calling parseMessages.
  3. Ensure the negotiated transfer format matches the protocol (Text for JSON, Binary for MessagePack).

Example fix

// before
protocol.parseMessages(uint8Array, logger);
// after - JSON requires text
protocol.parseMessages(new TextDecoder().decode(uint8Array), logger);
Defensive patterns

Strategy: type-guard

Validate before calling

function ensureStringInput(input: unknown): string {
  if (typeof input !== "string") {
    if (input instanceof ArrayBuffer) return new TextDecoder().decode(input);
    throw new TypeError("JSON protocol requires a string");
  }
  return input;
}

Type guard

function isStringInput(i: unknown): i is string {
  return typeof i === "string";
}

Prevention

When it happens

Trigger: Calling JsonHubProtocol.parseMessages with binary data, or wiring the JSON protocol against a connection whose transport delivers ArrayBuffer chunks (e.g. a binary WebSockets frame).

Common situations: Configuring the connection with JsonHubProtocol while the WebSocket transport negotiated binary transfer format, or feeding raw byte frames into the protocol manually (e.g. in tests).

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/c65b6e9400697642. Report an issue: GitHub.

Appendix: source

Thrown at src/SignalR/clients/ts/signalr/src/JsonHubProtocol.ts:31

export class JsonHubProtocol implements IHubProtocol {

    /** @inheritDoc */
    public readonly name: string = JSON_HUB_PROTOCOL_NAME;
    /** @inheritDoc */
    public readonly version: number = 2;

    /** @inheritDoc */
    public readonly transferFormat: TransferFormat = TransferFormat.Text;

    /** Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation.
     *
     * @param {string} input A string containing the serialized representation.
     * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
     */
    public parseMessages(input: string, logger: ILogger): HubMessage[] {
        // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error.
        if (typeof input !== "string") {
            throw new Error("Invalid input for JSON hub protocol. Expected a string.");
        }

        if (!input) {
            return [];
        }

        if (logger === null) {
            logger = NullLogger.instance;
        }

        // Parse the messages
        const messages = TextMessageFormat.parse(input);

        const hubMessages = [];
        for (const message of messages) {
            const parsedMessage = JSON.parse(message) as HubMessage;
            if (typeof parsedMessage.type !== "number") {
                throw new Error("Invalid payload.");

View on GitHub (pinned to 3600ca084e)