{"record":{"id":"710a89dca91c5e33","repo":"JuliusBrussee/caveman","slug":"not-losslessly-toon-encodable-invalid-json-or-ne-710a89","errorCode":null,"errorMessage":"not losslessly TOON-encodable (invalid JSON, or nested/non-uniform shape); keep JSON","messagePattern":"not losslessly TOON-encodable \\(invalid JSON, or nested/non-uniform shape\\); keep JSON","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"engine/toon.go","lineNumber":15,"sourceCode":"package engine\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/JuliusBrussee/caveman/engine/compressors\"\n)\n\n// EncodeTOON converts JSON to the lossless TOON subset without CCR or size\n// gating. It is for explicit agent/CLI requests, not automatic proxy routing.\nfunc (e *Engine) EncodeTOON(input []byte) ([]byte, error) {\n\tout, ok := compressors.NewTOON().Compress(input)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"not losslessly TOON-encodable (invalid JSON, or nested/non-uniform shape); keep JSON\")\n\t}\n\treturn out, nil\n}\n\n// DecodeTOON converts TOON back to compact JSON. Malformed TOON fails closed.\nfunc (e *Engine) DecodeTOON(input []byte) ([]byte, error) {\n\tv, ok := compressors.DecodeTOON(input)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"not valid TOON\")\n\t}\n\tout, err := json.Marshal(v)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn out, nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/toon.go#L1-L32","documentation":"EncodeTOON only accepts input that the TOON compressor can encode losslessly: valid JSON with a flat/uniform structure. If the compressor reports failure (invalid JSON or nested/non-uniform shapes that TOON cannot represent losslessly), the engine refuses to guess and tells you to keep JSON. This is a deliberate fail-closed guard, not a corrupt-output condition.","triggerScenarios":"Calling EncodeTOON with non-JSON bytes, JSON with nested objects/arrays inside uniform arrays, or otherwise irregular shapes that compressors.NewTOON().Compress rejects.","commonSituations":"Passing arbitrary user JSON to a TOON tool expecting it always works; trying to TOON-encode deeply nested API responses; piping truncated/invalid JSON.","solutions":["Validate the input parses as JSON before calling EncodeTOON","Check the shape: TOON needs uniform arrays/flat structures; if nested, keep JSON as the message says","Catch the error and fall back to sending the original JSON untouched","Pre-flatten or reshape the payload if TOON encoding is required"],"exampleFix":"out, err := engine.EncodeTOON(input)\nif err != nil {\n    out = input // keep JSON\n}","handlingStrategy":"fallback","validationCode":"var probe any\nif err := json.Unmarshal(input, &probe); err != nil { /* not JSON; skip TOON */ }","typeGuard":null,"tryCatchPattern":"out, err := engine.EncodeTOON(input)\nif err != nil {\n  out = input // fall back to JSON\n}","preventionTips":["Only request TOON for flat/uniform JSON payloads","Treat EncodeTOON failure as a signal to keep JSON, per the error message","Pre-validate JSON parses before attempting TOON encoding"],"tags":["toon","encoding","json","validation"],"backgroundTag":"unsupported-operation","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}