{"record":{"id":"523eacbcadee1895","repo":"Activiti/Activiti","slug":"value-out-of-sequence","errorCode":null,"errorMessage":"Value out of sequence.","messagePattern":"Value out of sequence\\.","errorType":"exception","errorClass":"JSONException","httpStatus":null,"severity":"error","filePath":"activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONWriter.java","lineNumber":139,"sourceCode":"        if (s == null) {\n            throw new JSONException(\"Null pointer\");\n        }\n        if (this.mode == 'o' || this.mode == 'a') {\n            try {\n                if (this.comma && this.mode == 'a') {\n                    this.writer.write(',');\n                }\n                this.writer.write(s);\n            } catch (IOException e) {\n                throw new JSONException(e);\n            }\n            if (this.mode == 'o') {\n                this.mode = 'k';\n            }\n            this.comma = true;\n            return this;\n        }\n        throw new JSONException(\"Value out of sequence.\");\n    }\n\n    /**\n     * Begin appending a new array. All values until the balancing <code>endArray</code> will be appended to this array. The <code>endArray</code> method must be called to mark the array's end.\n     *\n     * @return this\n     * @throws JSONException\n     *           If the nesting is too deep, or if the object is started in the wrong place (for example as a key or after the end of the outermost array or object).\n     */\n    public JSONWriter array() throws JSONException {\n        if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {\n            this.push(null);\n            this.append(\"[\");\n            this.comma = false;\n            return this;\n        }\n        throw new JSONException(\"Misplaced array.\");\n    }","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/Activiti/Activiti/blob/56435b1a97deeafdc09dd40074b056c89fba5a8a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONWriter.java#L121-L157","documentation":"JSONWriter tracks nesting state via a mode flag ('i' initial, 'o' object value pending, 'a' array element, 'k' key expected, 'd' done). append() only accepts writes in modes 'o' or 'a'; anything else throws \"Value out of sequence.\" This is a state-machine violation: you tried to emit a value where the grammar forbids it.","triggerScenarios":"Calling value(...) right after object() (a key is required first), calling value() twice for one key, calling value() after endObject()/endArray() closed the document, or value() at top level after array() without array element ordering.","commonSituations":"Hand-rolled serialization code that forgets key() before a value in an object; missing endObject()/endArray() calls in conditional branches leaving the writer in the wrong mode; reusing a finished JSONWriter for a second document.","solutions":["Call key(\"...\") before every value() while inside an object","Balance every array()/object() with endArray()/endObject() before emitting further values","Do not reuse a JSONWriter after the document is complete; create a new instance","Centralize writing in small helper methods (writeKey/writeValue) that enforce the key-then-value ordering"],"exampleFix":"// before\nwriter.object().value(42); // value before key -> out of sequence\n// after\nwriter.object().key(\"answer\").value(42).endObject();","handlingStrategy":"validation","validationCode":"// assert a key is expected before any value() while in an object:\nif (inObject && !lastCallWasKey) throw new IllegalStateException(\"call key() before value()\");","typeGuard":"null","tryCatchPattern":"try { writer.value(v); } catch (JSONException e) { if (\"Value out of sequence.\".equals(e.getMessage())) { /* fix call ordering: key() first inside objects */ } throw e; }","preventionTips":["Always pair object().key().value() and array().value() sequences","Balance every open with endObject/endArray on all code paths","Never reuse a JSONWriter after the document is complete","Wrap streaming in helper methods enforcing the mode sequence"],"tags":["json","state-machine","serialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"56435b1a97deeafdc09dd40074b056c89fba5a8a","analyzedAt":"2026-09-09T21:00:06.703Z","contentChangedAt":"2026-09-09T21:00:06.703Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}