Activiti/Activiti · error · JSONException

Nesting too deep.

Error message

Nesting too deep.

What it means

JSONWriter maintains a fixed-size scope stack with a configurable maxdepth to protect against stack overflow and maliciously deep input. push(jo) throws "Nesting too deep." when object() or array() is called while the current nesting depth already equals maxdepth.

Solutions

  1. Increase maxdepth via the JSONWriter constructor that accepts a depth limit, if the data is legitimately deep.
  2. Flatten or restructure the data model before serialization.
  3. Detect and cut cycles in the object graph (they inflate depth artificially).
  4. For untrusted input, enforce a sane depth cap rather than raising the limit.

Example fix

// before
JSONWriter w = new JSONWriter(sw); // default maxdepth, deep tree overflows
// after
JSONWriter w = new JSONWriter(sw, 512); // raise limit for deep structures
Defensive patterns

Strategy: validation

Validate before calling

if (currentDepth >= maxDepth) throw new IllegalStateException("structure exceeds JSONWriter max depth");

Try / catch

try { writer.object(); } catch (JSONException e) { if (e.getMessage().contains("Nesting too deep")) { /* flatten or increase limit */ } throw e; }

Prevention

When it happens

Trigger: Calling writer.object()/writer.array() when nesting depth has reached the writer's maxdepth (default limit), typically while serializing deeply recursive structures like trees or linked structures.

Common situations: Serializing recursive object graphs (parent/child trees, linked lists) with JSONWriter, or parsing/writing untrusted deeply nested JSON that exceeds the configured depth limit.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of Activiti/Activiti@56435b1a97 (2026-09-09). Data as JSON: /api/errors/d1536ec05a88745b. Report an issue: GitHub.

Appendix: source

Thrown at activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/JSONWriter.java:286

            throw new JSONException("Nesting error.");
        }
        char m = this.stack[this.top - 1] == null ? 'a' : 'k';
        if (m != c) {
            throw new JSONException("Nesting error.");
        }
        this.top -= 1;
        this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
    }

    /**
     * Push an array or object scope.
     *
     * @throws JSONException
     *           If nesting is too deep.
     */
    private void push(JSONObject jo) throws JSONException {
        if (this.top >= maxdepth) {
            throw new JSONException("Nesting too deep.");
        }
        this.stack[this.top] = jo;
        this.mode = jo == null ? 'a' : 'k';
        this.top += 1;
    }

    /**
     * Append either the value <code>true</code> or the value <code>false</code> .
     *
     * @param b
     *          A boolean.
     * @return this
     * @throws JSONException
     */
    public JSONWriter value(boolean b) throws JSONException {
        return this.append(b ? "true" : "false");
    }

View on GitHub (pinned to 56435b1a97)