{"record":{"id":"937413263e190cd1","repo":"kubernetes/kops","slug":"unhandled-state-for-json-value-t-q-serializati","errorCode":null,"errorMessage":"unhandled state for json value (%T %q) serialization: %v %q","messagePattern":"unhandled state for json value \\(%T %q\\) serialization: (.+?) %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/jsonutils/streamwriter.go","lineNumber":172,"sourceCode":"\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\n\t\t}\n\t\tj.deferred = \",\\n\"\n\t\treturn nil\n\t}\n\n\treturn fmt.Errorf(\"unhandled state for json value (%T %q) serialization: %v %q\", token, v, state, j.state)\n}\n\nfunc (j *JSONStreamWriter) writeRaw(s string) error {\n\tif j.deferred != \"\" {\n\t\tif _, err := j.out.Write([]byte(j.deferred)); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tj.deferred = \"\"\n\t}\n\t_, err := j.out.Write([]byte(s))\n\treturn err\n}\n","sourceCodeStart":154,"sourceCodeEnd":185,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/jsonutils/streamwriter.go#L154-L185","documentation":"After a value token passes the type switch, the writer serializes it according to the current state character. Valid states are '{' (a field name follows in an object), '[' (an array element), and 'F' (the value of a field). Any other state (including state 0, i.e. a bare value at the top level) has no serialization rule, so a JSON value was written where the state machine does not expect one.","triggerScenarios":"Writing a scalar token at top level before any '{' or '[' delimiter (state is 0); writing two field-name strings in a row without a value between them; continuing a writer whose prior stream was aborted.","commonSituations":"Streaming a bare JSON string/number (valid JSON per RFC 7159) — the writer only supports documents rooted in objects/arrays; misordered hand-driven token sequences.","solutions":["Start the token stream with json.Delim('{') or json.Delim('['); wrap bare scalars in an object/array.","Emit alternating field-name then value tokens inside objects.","Use a fresh writer per document and feed tokens in decoder order."],"exampleFix":"// before\nw.WriteToken(\"hello\") // top-level scalar, state==0\n// after\nw.WriteToken(json.Delim('{'))\nw.WriteToken(\"msg\")\nw.WriteToken(\"hello\")\nw.WriteToken(json.Delim('}'))","handlingStrategy":"validation","validationCode":"// Only call WriteToken with value tokens while inside an object (after a field name)\n// or an array. Reject top-level scalar documents:\nfirst, _ := dec.Token()\nif d, ok := first.(json.Delim); !ok || (d != '{' && d != '[') {\n\treturn fmt.Errorf(\"document must start with an object or array\")\n}","typeGuard":"func docStartsWithContainer(first json.Token) bool {\n\td, ok := first.(json.Delim)\n\treturn ok && (d == '{' || d == '[')\n}","tryCatchPattern":"if err := writer.WriteToken(tok); err != nil {\n\treturn fmt.Errorf(\"cannot serialize token %v in current state: %w\", tok, err)\n}","preventionTips":["Always open a document with '{' or '[' before writing values.","Alternate field-name/value tokens strictly inside objects.","Keep field name strings and value strings distinguishable in your token pipeline."],"tags":["json","state-machine","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-12T07:17:12.445Z"}