{"record":{"id":"81ff73132ee293ea","repo":"apache/hadoop","slug":"failed-to-serialize-object-to-json","errorCode":null,"errorMessage":"Failed to serialize object to JSON","messagePattern":"Failed to serialize object to JSON","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java","lineNumber":88,"sourceCode":"   */\n  public static <T> T parse(String json, TypeReference<T> typeRef) {\n    try {\n      return MAPPER.readValue(json, typeRef);\n    } catch (IOException e) {\n      throw new RuntimeException(\"Failed to parse JSON\", e);\n    }\n  }\n\n  /**\n   * Serialize an object to a JSON string.\n   * @param obj the object to serialize\n   * @return the JSON string\n   */\n  public static String toString(Object obj) {\n    try {\n      return MAPPER.writeValueAsString(obj);\n    } catch (IOException e) {\n      throw new RuntimeException(\"Failed to serialize object to JSON\", e);\n    }\n  }\n}\n","sourceCodeStart":70,"sourceCodeEnd":92,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java#L70-L92","documentation":"JsonUtils.toString(Object) serializes with Jackson's writeValueAsString; any serialization failure - a type with no serializable properties, a getter that throws, a cyclic object graph, or a type needing an unregistered module - surfaces as RuntimeException(\"Failed to serialize object to JSON\", e) with the Jackson exception as cause.","triggerScenarios":"toString(dto) where the DTO has only private fields and no getters; a lazy getter that throws; a bidirectional parent/child graph causing unbounded recursion; Java 8 date or Optional types without the matching Jackson module.","commonSituations":"Adding JsonUtils-based logging to domain objects that were never serialized before; ORM/proxy objects whose getters hit a closed session; DTOs gaining a back-reference during a refactor.","solutions":["Read the cause: JsonMappingException pinpoints the offending property and the exact reason.","Expose the data: public getters or fields annotated with @JsonProperty.","Break cycles with @JsonIgnore or @JsonManagedReference/@JsonBackReference, and register required modules on the mapper."],"exampleFix":"// before\nclass Node {\n  private String name;          // no getter -> nothing serializable\n  private Node parent;          // cycle\n  String log() { return JsonUtils.toString(this); }\n}\n\n// after\nclass Node {\n  @JsonProperty private String name;\n  @JsonIgnore private Node parent;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  String s = JsonUtils.toString(obj);\n} catch (RuntimeException e) {\n  // e.getCause() is a JsonMappingException naming the offending property\n}","preventionTips":["Keep DTOs dedicated to serialization: public getters, no back-references.","Unit-test toString() of every type that ends up in logs or API responses."],"tags":["json","serialization","hadoop-common"],"backgroundTag":"json-serialization-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}