Activiti/Activiti · error · JSONException
Value out of sequence.
Error message
Value out of sequence.
What it means
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.
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
Example fix
// before
writer.object().value(42); // value before key -> out of sequence
// after
writer.object().key("answer").value(42).endObject(); Defensive patterns
Strategy: validation
Validate before calling
// assert a key is expected before any value() while in an object:
if (inObject && !lastCallWasKey) throw new IllegalStateException("call key() before value()"); Type guard
null
Try / catch
try { writer.value(v); } catch (JSONException e) { if ("Value out of sequence.".equals(e.getMessage())) { /* fix call ordering: key() first inside objects */ } throw e; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
AI-assisted analysis of Activiti/Activiti@56435b1a97 (2026-09-09).
Data as JSON: /api/errors/523eacbcadee1895.
Report an issue: GitHub.
Appendix: source
Thrown at activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONWriter.java:139
if (s == null) {
throw new JSONException("Null pointer");
}
if (this.mode == 'o' || this.mode == 'a') {
try {
if (this.comma && this.mode == 'a') {
this.writer.write(',');
}
this.writer.write(s);
} catch (IOException e) {
throw new JSONException(e);
}
if (this.mode == 'o') {
this.mode = 'k';
}
this.comma = true;
return this;
}
throw new JSONException("Value out of sequence.");
}
/**
* 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.
*
* @return this
* @throws JSONException
* 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).
*/
public JSONWriter array() throws JSONException {
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
this.push(null);
this.append("[");
this.comma = false;
return this;
}
throw new JSONException("Misplaced array.");
}View on GitHub (pinned to 56435b1a97)