apache/seatunnel · error · IllegalStateException

failed to serialize event as JSON

Error message

failed to serialize event as JSON

What it means

LineOutputFormatter wraps Jackson JsonProcessingException in an IllegalStateException reporting that the event could not be serialized as JSON. It signals an internal serialization failure while writing the ObjectNode tree to a string.

Source

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

                ObjectNode node = buildLineObject(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(buildLineObject(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 buildLineObject(
            int index, MultilineAssembler.LineElement line, String payloadText) {
        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());
        node.put("payload", payloadText);
        return node;
    }
}

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Read the wrapped cause from the IllegalStateException to identify the Jackson error
  2. Sanitize/validate line payload text before inserting it into the ObjectNode
  3. Catch JsonProcessingException-based failures and fall back to emitting the raw line
  4. Verify ObjectMapper setup (modules, features) matches the expected output format

Example fix

// before
String json = lineFormatter.format(event, sourceId);
// after
try {
    String json = lineFormatter.format(event, sourceId);
} catch (IllegalStateException e) {
    LOG.warn("Falling back to raw line for source {}: {}", sourceId, e.getCause());
}
Defensive patterns

Strategy: try-catch

Validate before calling

// validate payload text is well-formed before formatting
Objects.requireNonNull(payloadText, "payloadText");

Try / catch

try {
    lineFormatter.format(event, sourceId);
} catch (IllegalStateException e) {
    LOG.warn("Serialization failed, using raw line: {}", e.getCause());
}

Prevention

When it happens

Trigger: Jackson throws JsonProcessingException inside format(), e.g. an unserializable value inserted into the ObjectNode or a writer-level failure.

Common situations: Unserializable or malformed payload content placed into the tree by buildLineObject; ObjectMapper misconfiguration; corrupted line data from a source file.

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/50a28dead13a2455. Report an issue: GitHub.