apache/cassandra · error · MarshalException
Error writing as JSON:
Error message
Error writing as JSON:
What it means
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>'.
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
Example fix
// before byte[] b = JsonUtils.writeAsJsonBytes(customObject); // after Map<String, Object> plain = convertToPlainTypes(customObject); byte[] b = JsonUtils.writeAsJsonBytes(plain);
Defensive patterns
Strategy: try-catch
Try / catch
try { return JsonUtils.writeAsJsonBytes(value); } catch (MarshalException e) { throw new InvalidRequestException("Value not JSON-serializable: " + e.getMessage()); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Serializing arbitrary Java objects (e.g. Maps containing unserializable values from UDFs/UDAs); objects whose getters throw; cyclic references in nested collections.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Error decoding JSON bytes:
- Error decoding JSON string:
- Attempted to encode a response with an unset stream id:
- Cannot convert value
- Cannot decode string as UTF8: '" +…
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/c7df101fb9e09871.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/utils/JsonUtils.java:113
try
{
return JSON_OBJECT_MAPPER.readValue(json, Object.class);
}
catch (IOException ex)
{
throw new MarshalException("Error decoding JSON string: " + ex.getMessage());
}
}
public static byte[] writeAsJsonBytes(Object value)
{
try
{
return JSON_OBJECT_MAPPER.writeValueAsBytes(value);
}
catch (IOException ex)
{
throw new MarshalException("Error writing as JSON: " + ex.getMessage());
}
}
public static String writeAsJsonString(Object value)
{
try
{
return JSON_OBJECT_MAPPER.writeValueAsString(value);
}
catch (IOException ex)
{
throw new MarshalException("Error writing as JSON: " + ex.getMessage());
}
}
public static String writeAsPrettyJsonString(Object value) throws MarshalException
{
tryView on GitHub (pinned to 88fd0f6a0e)