{"record":{"id":"d13a15d0a54be9e1","repo":"apache/dolphinscheduler","slug":"object-json-deserialization-exception","errorCode":null,"errorMessage":"Object json deserialization exception.","messagePattern":"Object json deserialization exception\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java","lineNumber":306,"sourceCode":"\n        try {\n            return objectMapper.readValue(json, type);\n        } catch (Exception e) {\n            throw new IllegalArgumentException(\"Parse json: \" + json + \" to type: \" + type.getType() + \" failed\", e);\n        }\n    }\n\n    /**\n     * object to json string\n     *\n     * @param object object\n     * @return json string\n     */\n    public static String toJsonString(Object object) {\n        try {\n            return objectMapper.writeValueAsString(object);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Object json deserialization exception.\", e);\n        }\n    }\n\n    public static String toPrettyJsonString(Object object) {\n        try {\n            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Object json deserialization exception.\", e);\n        }\n    }\n\n    /**\n     * serialize to json byte\n     *\n     * @param obj object\n     * @param <T> object type\n     * @return byte array\n     */","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java#L288-L324","documentation":"JSONUtils.toJsonString serializes an object to a JSON string with a shared Jackson ObjectMapper. Any exception during writeValueAsString is rethrown as a RuntimeException with the (misleading) fixed message 'Object json deserialization exception.' and the original cause attached. Despite the wording, this is a serialization failure, not deserialization.","triggerScenarios":"Serializing an object with cyclic references (StackOverflowError/JsonMappingException infinite recursion), Jackson-incompatible types (no getters and no visibility config, unsupported classes like InputStream/Thread), a getter that throws, or self-referencing lazy/Hibernate-proxied entities.","commonSituations":"Logging an object graph containing cycles (parent<->child); serializing classes whose getters compute/throw; adding a field of a non-serializable type; serializing entities with bidirectional relationships; objects carrying unserializable JDK types.","solutions":["Inspect e.getCause() to find the actual serialization failure (JsonMappingException naming the offending property).","Break cycles with @JsonIgnore on the back-reference field or manage the bidirectional relation with @JsonManagedReference/@JsonBackReference.","Annotate problematic getters/fields with @JsonIgnore or make the class Jackson-friendly (public getters, default constructor if it must also deserialize).","For a field that legitimately fails, register a custom serializer on the shared ObjectMapper or implement JsonSerializable.","As a last resort, serialize a reduced DTO containing only the needed fields instead of the whole graph."],"exampleFix":"// before\nclass Node {\n    private Node parent;\n    private List<Node> children;\n    // getters for both -> infinite recursion on toJsonString(node)\n}\n// after\nclass Node {\n    @JsonIgnore\n    private Node parent; // break the cycle\n    private List<Node> children;\n}","handlingStrategy":"try-catch","validationCode":"// detect the most common cause (cycles) before serializing complex graphs\nSet<Object> seen = Collections.newSetFromMap(new IdentityHashMap<>());\nboolean cyclic = false;\nfor (Object o : graphNodes) { if (!seen.add(o)) { cyclic = true; break; } }","typeGuard":null,"tryCatchPattern":"try {\n    return JSONUtils.toJsonString(obj);\n} catch (RuntimeException e) {\n    logger.error(\"toJsonString failed for {}: {}\", obj.getClass().getName(), e.getCause(), e);\n    return String.valueOf(obj); // fallback to toString\n}","preventionTips":["Annotate back-references with @JsonIgnore or @JsonBackReference to prevent infinite recursion.","Avoid serializing objects holding streams, connections, or threads.","Keep getters side-effect-free; a throwing getter breaks writeValueAsString.","Prefer serializing purpose-built DTOs over arbitrary domain graphs.","Remember: despite the message, this is a serialization failure — read e.getCause()."],"tags":["json","serialization","jackson","runtime-exception"],"backgroundTag":"json-serialization-failed","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}