{"record":{"id":"c7df101fb9e09871","repo":"apache/cassandra","slug":"error-writing-as-json","errorCode":null,"errorMessage":"Error writing as JSON: ","messagePattern":"Error writing as JSON: ","errorType":"exception","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/JsonUtils.java","lineNumber":113,"sourceCode":"        try\n        {\n            return JSON_OBJECT_MAPPER.readValue(json, Object.class);\n        }\n        catch (IOException ex)\n        {\n            throw new MarshalException(\"Error decoding JSON string: \" + ex.getMessage());\n        }\n    }\n\n    public static byte[] writeAsJsonBytes(Object value)\n    {\n        try\n        {\n            return JSON_OBJECT_MAPPER.writeValueAsBytes(value);\n        }\n        catch (IOException ex)\n        {\n            throw new MarshalException(\"Error writing as JSON: \" + ex.getMessage());\n        }\n    }\n\n    public static String writeAsJsonString(Object value)\n    {\n        try\n        {\n            return JSON_OBJECT_MAPPER.writeValueAsString(value);\n        }\n        catch (IOException ex)\n        {\n            throw new MarshalException(\"Error writing as JSON: \" + ex.getMessage());\n        }\n    }\n\n    public static String writeAsPrettyJsonString(Object value) throws MarshalException\n    {\n        try","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/JsonUtils.java#L95-L131","documentation":"JsonUtils.writeAsJsonBytes(Object) serializes a value to JSON bytes with Jackson. If Jackson raises an IOException while writing (e.g. an object with no serializable properties or a broken custom serializer), it is wrapped in MarshalException 'Error writing as JSON: <message>'.","triggerScenarios":"Calling writeAsJsonBytes with an object graph Jackson cannot serialize: self-referencing structures without handling, types lacking serializers and no failing requirement satisfied, or infinite recursion.","commonSituations":"Serializing arbitrary Java objects (e.g. Maps containing unserializable values from UDFs/UDAs); objects whose getters throw; cyclic references in nested collections.","solutions":["Inspect the wrapped IOException message to find the failing property/type","Convert the value to JSON-friendly types (Map/List/String/primitives) before writing","Ensure the object's getters do not throw and avoid cyclic references"],"exampleFix":"// before\nbyte[] b = JsonUtils.writeAsJsonBytes(customObject);\n// after\nMap<String, Object> plain = convertToPlainTypes(customObject);\nbyte[] b = JsonUtils.writeAsJsonBytes(plain);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return JsonUtils.writeAsJsonBytes(value); } catch (MarshalException e) { throw new InvalidRequestException(\"Value not JSON-serializable: \" + e.getMessage()); }","preventionTips":["Serialize only plain Maps/Lists/Strings/primitives into JSON columns","Avoid cyclic object graphs and getters with side effects","Unit-test serialization of every type you store as JSON"],"tags":["json","jackson","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}