{"record":{"id":"7aa20e3ad42ec548","repo":"apache/dolphinscheduler","slug":"object-obj-to-json-serialization-exception","errorCode":null,"errorMessage":"Object: + obj + to json serialization exception.","messagePattern":"Object: \\+ obj \\+ to json serialization exception\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java","lineNumber":332,"sourceCode":"            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     */\n    public static <T> byte[] toJsonByteArray(T obj) {\n        if (obj == null) {\n            return null;\n        }\n        try {\n            return toJsonString(obj).getBytes(UTF_8);\n        } catch (Exception e) {\n            throw new IllegalArgumentException(\"Object: \" + obj + \" to json serialization exception.\", e);\n        }\n\n    }\n\n    public static ObjectNode parseObject(String text) {\n        try {\n            if (StringUtils.isEmpty(text)) {\n                return parseObject(text, ObjectNode.class);\n            } else {\n                return (ObjectNode) objectMapper.readTree(text);\n            }\n        } catch (Exception e) {\n            throw new RuntimeException(\"String json deserialization exception.\", e);\n        }\n    }\n\n    public static ArrayNode parseArray(String text) {\n        try {","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java#L314-L350","documentation":"JSONUtils.toJsonByteArray converts an arbitrary Java object to a UTF-8 JSON byte array. Any exception thrown by the underlying Jackson serialization (via toJsonString/objectMapper) is wrapped in an IllegalArgumentException whose message echoes the offending object. It signals the object graph is not JSON-serializable rather than a caller input-format problem.","triggerScenarios":"Calling JSONUtils.toJsonByteArray(obj) when Jackson fails on the object: no accessible getters/fields, infinite recursion from cyclic references, unserializable types (InputStream, Thread), a property getter that throws, or invalid ObjectMapper configuration","commonSituations":"Putting a non-POJO (e.g. an Hadoop Configuration, a lambda, an open stream) into a task parameter or workflow context that is later serialized to JSON; adding a getter with side effects or a self-referencing object graph after a version upgrade.","solutions":["Print/inspect the wrapped cause (e.getCause()) to find the exact failing property or type","Annotate unserializable fields with @JsonIgnore or mark them transient and add getters Jackson can see","Break cyclic references with @JsonManagedReference/@JsonBackReference or set SerializationFeature.FAIL_ON_SELF_REFERENCES appropriately","Register a custom JsonSerializer/Module for third-party types that Jackson cannot handle","Ensure the object is a plain POJO (no-arg constructor + getters) or convert it to a Map before serializing"],"exampleFix":"// before\nbyte[] data = JSONUtils.toJsonByteArray(taskExecutionContext.getInjectedStream());\n// after\n@JsonIgnore\nprivate transient InputStream rawStream; // exclude non-serializable field","handlingStrategy":"try-catch","validationCode":"if (obj == null) return null; // also ensure fields are Jackson-friendly: no cycles, no raw streams","typeGuard":"boolean isSerializable(Object o) {\n  try { JSONUtils.toJsonString(o); return true; } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  byte[] data = JSONUtils.toJsonByteArray(obj);\n} catch (IllegalArgumentException e) {\n  log.error(\"Unserializable object of type {}: {}\", obj.getClass().getName(), e.getCause());\n  throw new IllegalStateException(\"Object not serializable to JSON\", e);\n}","preventionTips":["Keep DTOs as plain POJOs with getters and no cycles","Annotate third-party or transient fields with @JsonIgnore","Unit-test serialization of every object stored in task parameters","Never put open resources (streams, connections) into serialized contexts"],"tags":["json","serialization","jackson","illegal-argument"],"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-14T05:17:10.506Z"}