{"record":{"id":"c65b6e9400697642","repo":"dotnet/aspnetcore","slug":"invalid-input-for-json-hub-protocol-expected-a-st","errorCode":null,"errorMessage":"Invalid input for JSON hub protocol. Expected a string.","messagePattern":"Invalid input for JSON hub protocol\\. Expected a string\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/JsonHubProtocol.ts","lineNumber":31,"sourceCode":"export class JsonHubProtocol implements IHubProtocol {\n\n    /** @inheritDoc */\n    public readonly name: string = JSON_HUB_PROTOCOL_NAME;\n    /** @inheritDoc */\n    public readonly version: number = 2;\n\n    /** @inheritDoc */\n    public readonly transferFormat: TransferFormat = TransferFormat.Text;\n\n    /** Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation.\n     *\n     * @param {string} input A string containing the serialized representation.\n     * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\n     */\n    public parseMessages(input: string, logger: ILogger): HubMessage[] {\n        // The interface does allow \"ArrayBuffer\" to be passed in, but this implementation does not. So let's throw a useful error.\n        if (typeof input !== \"string\") {\n            throw new Error(\"Invalid input for JSON hub protocol. Expected a string.\");\n        }\n\n        if (!input) {\n            return [];\n        }\n\n        if (logger === null) {\n            logger = NullLogger.instance;\n        }\n\n        // Parse the messages\n        const messages = TextMessageFormat.parse(input);\n\n        const hubMessages = [];\n        for (const message of messages) {\n            const parsedMessage = JSON.parse(message) as HubMessage;\n            if (typeof parsedMessage.type !== \"number\") {\n                throw new Error(\"Invalid payload.\");","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/JsonHubProtocol.ts#L13-L49","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Use the MessagePackHubProtocol when you need binary transfer.","If you must feed bytes to JSON, decode to a string first (TextDecoder) before calling parseMessages.","Ensure the negotiated transfer format matches the protocol (Text for JSON, Binary for MessagePack)."],"exampleFix":"// before\nprotocol.parseMessages(uint8Array, logger);\n// after - JSON requires text\nprotocol.parseMessages(new TextDecoder().decode(uint8Array), logger);","handlingStrategy":"type-guard","validationCode":"function ensureStringInput(input: unknown): string {\n  if (typeof input !== \"string\") {\n    if (input instanceof ArrayBuffer) return new TextDecoder().decode(input);\n    throw new TypeError(\"JSON protocol requires a string\");\n  }\n  return input;\n}","typeGuard":"function isStringInput(i: unknown): i is string {\n  return typeof i === \"string\";\n}","tryCatchPattern":null,"preventionTips":["Match the protocol to the negotiated transfer format.","Use MessagePackHubProtocol for binary.","Decode bytes to text before JSON parsing."],"tags":["protocol","json","binary","parsing"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}