{"record":{"id":"20d1e8c4b7a9681c","repo":"quarkusio/quarkus","slug":"failed-to-encode-as-json","errorCode":null,"errorMessage":"Failed to encode as JSON: ","messagePattern":"Failed to encode as JSON: ","errorType":"exception","errorClass":"EncodeException","httpStatus":null,"severity":"error","filePath":"extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/jackson/QuarkusJacksonJsonCodec.java","lineNumber":170,"sourceCode":"    }\n\n    @Override\n    public String toString(Object object, boolean pretty) throws EncodeException {\n        try {\n            ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();\n            return mapper.writeValueAsString(object);\n        } catch (Exception e) {\n            throw new EncodeException(\"Failed to encode as JSON: \" + e.getMessage(), e);\n        }\n    }\n\n    @Override\n    public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {\n        try {\n            ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();\n            return Buffer.buffer(mapper.writeValueAsBytes(object));\n        } catch (Exception e) {\n            throw new EncodeException(\"Failed to encode as JSON: \" + e.getMessage(), e);\n        }\n    }\n\n    private static void close(Closeable parser) {\n        try {\n            parser.close();\n        } catch (IOException ignore) {\n        }\n    }\n\n    @SuppressWarnings(\"rawtypes\")\n    private static Object adapt(Object o) {\n        try {\n            if (o instanceof List) {\n                List list = (List) o;\n                return new JsonArray(list);\n            } else if (o instanceof Map) {\n                @SuppressWarnings(\"unchecked\")","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/jackson/QuarkusJacksonJsonCodec.java#L152-L188","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nclass Order { User user; }\nclass User { @JsonIgnore omitted; Order order; } // cycle -> EncodeException\n\n// after\nclass Order { User user; }\nclass User { @JsonIgnore Order order; }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Buffer buf = codec.toBuffer(obj);\n} catch (EncodeException e) {\n    log.errorf(e, \"Cannot serialize %s: %s\", obj.getClass(), e.getMessage());\n    throw new BadRequestException(\"Unserializable payload\");\n}","preventionTips":["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"],"tags":["jackson","json","serialization","vertx"],"backgroundTag":"json-encode-failed","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}