quarkusio/quarkus · error · EncodeException
Failed to encode as JSON:
Error message
Failed to encode as JSON:
What it means
QuarkusJacksonJsonCodec.toBuffer serializes an object to JSON using the Quarkus-configured Jackson ObjectMapper and wraps any Jackson failure in a Jackson's EncodeException with the message 'Failed to encode as JSON: ' plus the underlying cause message. It exists so event bus / JSON codecs surface serialization errors consistently.
Source
Thrown at extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/jackson/QuarkusJacksonJsonCodec.java:170
}
@Override
public String toString(Object object, boolean pretty) throws EncodeException {
try {
ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();
return mapper.writeValueAsString(object);
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage(), e);
}
}
@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
try {
ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();
return Buffer.buffer(mapper.writeValueAsBytes(object));
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage(), e);
}
}
private static void close(Closeable parser) {
try {
parser.close();
} catch (IOException ignore) {
}
}
@SuppressWarnings("rawtypes")
private static Object adapt(Object o) {
try {
if (o instanceof List) {
List list = (List) o;
return new JsonArray(list);
} else if (o instanceof Map) {
@SuppressWarnings("unchecked")View on GitHub (pinned to e1c734241f)
Solutions
- Read the cause message appended to 'Failed to encode as JSON:' to identify the offending property
- Register missing Jackson modules or annotate types with @JsonSerialize/@JsonIgnore where appropriate
- Break cyclic references with @JsonIgnore on the back-reference or @JsonManagedReference/@JsonBackReference
- Ensure lazy JPA associations are initialized or DTO-mapped before encoding
Example fix
// before
class Order { User user; }
class User { @JsonIgnore omitted; Order order; } // cycle -> EncodeException
// after
class Order { User user; }
class User { @JsonIgnore Order order; } Defensive patterns
Strategy: try-catch
Try / catch
try {
Buffer buf = codec.toBuffer(obj);
} catch (EncodeException e) {
log.errorf(e, "Cannot serialize %s: %s", obj.getClass(), e.getMessage());
throw new BadRequestException("Unserializable payload");
} Prevention
- Test-serialize domain DTOs in unit tests (round-trip toBuffer/fromBuffer)
- Annotate back-references with @JsonIgnore to avoid cycles
- Register all required Jackson modules (jsr310, jdk8) in the application
When it happens
Trigger: Calling toBuffer (directly or via event bus publishing/routing with JSON codec) with an object Jackson cannot serialize: no serializer, self-referencing structure, invalid Java types, or a custom getter that throws.
Common situations: Publishing a POJO over the Vert.x event bus in clustered mode or with JSON encoding; missing @JsonProperty or Jackson modules (java.time, jdk8); cyclic object graphs; lazy Hibernate entities serialized outside a session.
Related errors
- Unknown JSON Value
- Failed to decode:
- Could not serialize the provided output.
- Cannot create JsonArray
- Cannot create JsonObject
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/20d1e8c4b7a9681c.
Report an issue: GitHub.