{"record":{"id":"eb4141aaa68c6de4","repo":"eclipse-vertx/vert.x","slug":"failed-to-encode-as-json-e-getmessage","errorCode":null,"errorMessage":"Failed to encode as JSON: ${e.getMessage()}","messagePattern":"Failed to encode as JSON: (.+?)","errorType":"exception","errorClass":"EncodeException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java","lineNumber":169,"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":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/json/jackson/DatabindCodec.java#L151-L187","documentation":"DatabindCodec.toString wraps any Jackson exception raised while serializing an object to a JSON string in io.vertx.core.encode.EncodeException. The library throws it whenever mapper.writeValueAsString (or the pretty-printer variant) fails, so callers get a uniform Vert.x exception type. The original Jackson message is appended after the prefix.","triggerScenarios":"Calling Json.encode / JsonObject.toString or DatabindCodec.toString(obj) with an object Jackson cannot serialize: no serializer found for a type (unmapped POJO, no getters), infinite recursion/cyclic reference, or an exception thrown inside a custom JsonSerializer.","commonSituations":"Encoding a plain Java object without a public getter or @JsonProperty; a POJO graph with a circular reference; putting non-JSON-serializable values (InputStream, Hibernate lazy proxies) into a JsonObject map; Vert.x upgrade where a custom codec no longer matches.","solutions":["Read the wrapped Jackson cause message and fix the offending field/type (add getters, @JsonProperty, or make the class serializable)","Break cycles with @JsonIgnore or @JsonManagedReference/@JsonBackReference on the involved fields","Register a JsonSerializer via ObjectMapper customizer (DatabindCodec.mapper()) for the unsupported type","If encoding MapLike proxies (Hibernate), initialize/DTO-map entities before calling Json.encode"],"exampleFix":"// before\nJson.encode(userEntity); // EncodeException: No serializer found for class User\n// after\n@JsonIgnore private LazyProxy proxy;\npublic String getName() { return name; }\nJson.encode(userEntity);","handlingStrategy":"try-catch","validationCode":"public static boolean isJsonEncodable(Object o) {\n  try { DatabindCodec.toBuffer(o); return true; }\n  catch (EncodeException e) { return false; }\n}","typeGuard":"if (o instanceof JsonObject || o instanceof JsonArray || o instanceof String || o instanceof Number || o instanceof Boolean) { /* safely encodable */ }","tryCatchPattern":"try {\n  String json = Json.encode(obj);\n} catch (EncodeException e) {\n  logger.warn(\"JSON encode failed: {}\", e.getMessage());\n  // fall back to a safe representation\n}","preventionTips":["Keep POJOs encoded over Vert.x JSON simple: public getters, no cyclic references","Add @JsonIgnore to transient/infrastructure fields (connections, streams, proxies)","Encode domain objects to JsonObject DTOs at the boundary instead of raw entities","Add a unit test that encodes every outgoing payload type"],"tags":["json","serialization","jackson","vertx-core"],"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"}