{"record":{"id":"5130921f91c1887d","repo":"kubernetes/kops","slug":"unhandled-token-type-t","errorCode":null,"errorMessage":"unhandled token type %T","messagePattern":"unhandled token type %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/jsonutils/streamwriter.go","lineNumber":148,"sourceCode":"\n\t\t//\tstring, for JSON string literals\n\tcase string:\n\t\tv = \"\\\"\" + tt + \"\\\"\"\n\n\t\t//\tfloat64, for JSON numbers\n\tcase float64:\n\t\tv = fmt.Sprintf(\"%g\", tt)\n\n\t\t//\tNumber, for JSON numbers\n\tcase json.Number:\n\t\tv = tt.String()\n\n\t\t//\tnil, for JSON null\n\tcase nil:\n\t\tv = \"null\"\n\n\tdefault:\n\t\treturn fmt.Errorf(\"unhandled token type %T\", tt)\n\t}\n\n\tswitch state {\n\tcase '{':\n\t\tj.state += \"F\"\n\t\tj.path = append(j.path, fmt.Sprintf(\"%s\", token))\n\t\treturn j.writeRaw(j.indent + v + \": \")\n\tcase '[':\n\t\tif err := j.writeRaw(j.indent + v); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tj.deferred = \",\\n\"\n\t\treturn nil\n\tcase 'F':\n\t\tj.state = j.state[:len(j.state)-1]\n\t\tj.path = j.path[:len(j.path)-1]\n\t\tif err := j.writeRaw(v); err != nil {\n\t\t\treturn err","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/jsonutils/streamwriter.go#L130-L166","documentation":"WriteToken handles Go types corresponding to JSON values: bool, string, float64, json.Number, json.Delim, and nil. Passing any other Go type (int, map[string]any, struct, time.Time, etc.) fails with this error because the writer only re-emits tokens produced by a json.Decoder, not arbitrary values.","triggerScenarios":"Calling WriteToken(v) directly with a raw Go value such as an int, int64, []string, or struct instead of a json.Token from Decoder.Token(); e.g. w.WriteToken(42) — note json.Decoder yields float64 or json.Number, never int.","commonSituations":"Mixing encoding/json marshalling with token streaming; assuming numbers arrive as int; feeding decoded map values straight into the writer.","solutions":["Convert the value to a supported token type first: use json.Number(strconv.FormatInt(n, 10)) for integers.","Only feed tokens from json.Decoder.Token() into WriteToken.","For non-token values, marshal to JSON first and re-decode with UseNumber, then stream the tokens."],"exampleFix":"// before\nw.WriteToken(count) // count is int\n// after\nw.WriteToken(json.Number(strconv.Itoa(count)))","handlingStrategy":"type-guard","validationCode":"func isJSONToken(v any) bool {\n\tswitch v.(type) {\n\tcase nil, bool, string, float64, json.Number, json.Delim:\n\t\treturn true\n\t}\n\treturn false\n}","typeGuard":"func isJSONToken(v any) bool {\n\tswitch v.(type) {\n\tcase nil, bool, string, float64, json.Number, json.Delim:\n\t\treturn true\n\t}\n\treturn false\n}","tryCatchPattern":null,"preventionTips":["Convert ints to json.Number before writing.","Never pass raw Go structs/maps to WriteToken.","Remember json.Decoder yields float64/json.Number, never int."],"tags":["json","type-mismatch","streaming"],"backgroundTag":"json-invalid-token","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}