apache/seatunnel · error · IllegalStateException

failed to serialize event as JSON

Error message

failed to serialize event as JSON

What it means

After building the JSON representation of the event, the formatter wraps any Jackson JsonProcessingException in an IllegalStateException indicating JSON serialization failed. This is an internal serialization failure: the ObjectNode tree Jackson built could not be written out as a string.

Source

Thrown at seatunnel-edge-agent/seatunnel-edge-agent-connector/src/main/java/org/apache/seatunnel/edge/agent/connector/file/output/JsonOutputFormatter.java:63

                ObjectNode node = buildLineObjectWithParsedPayload(0, first, first.getText());
                json = objectMapper.writeValueAsString(node);
            } else {
                ArrayNode array = objectMapper.createArrayNode();
                for (int i = 0; i < event.size(); i++) {
                    MultilineAssembler.LineElement e = event.get(i);
                    array.add(buildLineObjectWithParsedPayload(i, e, e.getText()));
                }
                json = objectMapper.writeValueAsString(array);
            }
            return new CollectedRecord(
                    json,
                    sourceId,
                    first.getFilePath(),
                    last.getOffset(),
                    first.getLineNumber(),
                    first.getTs());
        } catch (JsonProcessingException e) {
            throw new IllegalStateException("failed to serialize event as JSON", e);
        }
    }

    private ObjectNode buildLineObjectWithParsedPayload(
            int index, MultilineAssembler.LineElement line, String lineText) {
        ObjectNode node = objectMapper.createObjectNode();
        node.put("_index", index);
        node.put("_file", line.getFilePath());
        node.put("_line", line.getLineNumber());
        node.put("_offset", line.getOffset());
        node.put("_ts", line.getTs());
        putPayload(node, lineText);
        return node;
    }

    private void putPayload(ObjectNode node, String lineText) {
        try {
            JsonNode parsed = objectMapper.readTree(lineText);

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Inspect the wrapped cause (e.getCause()) to find the actual Jackson failure
  2. Ensure all values inserted into the ObjectNode are Jackson-serializable types
  3. Catch JsonProcessingException upstream and fall back to plain-text line output
  4. Check for circular or oversized payload data in parsed line content

Example fix

// before
String json = formatter.format(event, sourceId);
// after
try {
    String json = formatter.format(event, sourceId);
} catch (IllegalStateException e) {
    LOG.error("JSON serialization failed for source {}: {}", sourceId, e.getCause());
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure payload values are plain strings/numbers before inserting into ObjectNode
Objects.requireNonNull(lineText, "lineText");

Try / catch

try {
    formatter.format(event, sourceId);
} catch (IllegalStateException e) {
    LOG.error("JSON serialization failed: {}", e.getCause());
}

Prevention

When it happens

Trigger: Jackson throws JsonProcessingException inside format() — e.g. a broken ObjectNode configuration, an unsupported value type inserted into the tree, or an I/O-backed writer failure.

Common situations: Custom ObjectNode extensions or exotic value types added via buildLineObjectWithParsedPayload; a misconfigured ObjectMapper module; extremely large payloads hitting writer limits.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/5c494bac4017750d. Report an issue: GitHub.