{"record":{"id":"0f2e647236ee4bfe","repo":"eclipse-vertx/vert.x","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":"vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java","lineNumber":148,"sourceCode":"    }\n    if (type.getType() == Object.class) {\n      value = (T) adapt(value);\n    }\n    return value;\n  }\n\n  @Override\n  public String toString(Object object, boolean pretty) throws EncodeException {\n    try {\n      String result;\n      if (pretty) {\n        result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);\n      } else {\n        result = mapper.writeValueAsString(object);\n      }\n      return result;\n    } catch (Exception e) {\n      throw new EncodeException(\"Failed to encode as JSON: \" + e.getMessage());\n    }\n  }\n\n  @Override\n  public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {\n    try {\n      byte[] result;\n      if (pretty) {\n        result = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(object);\n      } else {\n        result = mapper.writeValueAsBytes(object);\n      }\n      return Buffer.buffer(result);\n    } catch (Exception e) {\n      throw new EncodeException(\"Failed to encode as JSON: \" + e.getMessage());\n    }\n  }\n","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java#L130-L166","documentation":"DatabindCodec.toString serializes an object with the Jackson ObjectMapper (optionally pretty-printed). Any exception during serialization — unserializable types, self-referencing structures, failing getters — is rethrown as Vert.x EncodeException with message 'Failed to encode as JSON: ' plus the cause message.","triggerScenarios":"Json.encode(obj) / toString(obj) where obj contains types Jackson cannot serialize: no public getters, infinite recursion between objects, unhandled types like Throwable or InputStream in fields.","commonSituations":"Passing domain objects with circular references (parent<->child) to Json.encode; POJOs without getters; embedding non-serializable library objects (e.g. Vert.x future, exceptions) in a response DTO.","solutions":["Read the cause message to find the failing property; annotate it with @JsonIgnore or remove it from the DTO.","Break circular references (drop the back-reference or use @JsonManagedReference/@JsonBackReference).","Add public getters or Jackson annotations (@JsonProperty) so the bean is serializable.","Register a module/serializer for the unsupported type, or convert it to a Map/POJO before encoding."],"exampleFix":"// before\nclass Node { Node parent; Node child; } // infinite recursion\n// after\nclass Node { @JsonIgnore Node parent; Node child; }","handlingStrategy":"type-guard","validationCode":"static boolean isJsonSafe(Object o) {\n  try { Json.encode(o); return true; } catch (EncodeException e) { return false; }\n}","typeGuard":"boolean serializable(Object o) {\n  return !(o instanceof Throwable || o instanceof InputStream || o instanceof Logger);\n}","tryCatchPattern":"try {\n  String json = Json.encode(dto);\n} catch (EncodeException e) {\n  log.error(\"Cannot serialize {}: {}\", dto.getClass(), e.getMessage());\n}","preventionTips":["Keep DTOs free of circular references; use @JsonIgnore for back-references.","Never put runtime objects (streams, connections, loggers) in response DTOs.","Round-trip test Json.encode(Json.decodeValue(sample, Dto.class)) in CI."],"tags":["json","encoding","serialization","jackson"],"backgroundTag":"json-serialization-failed","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}