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

  1. Inspect the wrapped IOException message to find the failing property/type
  2. Convert the value to JSON-friendly types (Map/List/String/primitives) before writing
  3. 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

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


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
    {
        try

View on GitHub (pinned to 88fd0f6a0e)