apache/cassandra · error · MarshalException
Expected a float value, but got a %s: %s
Error message
Expected a float value, but got a %s: %s
What it means
FloatType.fromJSONObject() deserializes a parsed JSON value into a float; after the string and Number paths it catches ClassCastException and throws this MarshalException. It means the JSON value passed to fromJSONObject was neither a String nor a Number (e.g. a Boolean, Map, or List), so the float conversion cast failed. Cassandra throws it because a float column can only be materialized from a textual or numeric JSON representation.
Source
Thrown at src/java/org/apache/cassandra/db/marshal/FloatType.java:109
catch (NumberFormatException e1)
{
throw new MarshalException(String.format("Unable to make float from '%s'", source), e1);
}
}
@Override
public Term fromJSONObject(Object parsed) throws MarshalException
{
try
{
if (parsed instanceof String)
return new Constants.Value(fromString((String) parsed));
else
return new Constants.Value(getSerializer().serialize(((Number) parsed).floatValue()));
}
catch (ClassCastException exc)
{
throw new MarshalException(String.format(
"Expected a float value, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
}
}
@Override
public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)
{
Float value = getSerializer().deserialize(buffer);
if (value == null)
return "\"\"";
// JSON does not support NaN, Infinity and -Infinity values. Most of the parser convert them into null.
if (value.isNaN() || value.isInfinite())
return "null";
return value.toString();
}
public CQL3Type asCQL3Type()
{View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Ensure the JSON value for the float field is a number or numeric string before calling fromJSONObject
- Fix the client/producer so it serializes the field as a JSON number (e.g. 3.14) instead of a boolean/array/object
- If the field can legitimately be non-numeric, change the column type or drop/transform the value upstream
- Wrap fromJSONObject in try-catch for MarshalException to reject the record with a clear validation message
Example fix
// before
Object parsed = json.get("score"); // Boolean.TRUE
ByteBuffer v = FloatType.instance.fromJSONObject(parsed, ProtocolVersion.V5);
// after
Object parsed = json.get("score");
if (!(parsed instanceof Number || parsed instanceof String))
throw new IllegalArgumentException("score must be a number or numeric string");
ByteBuffer v = FloatType.instance.fromJSONObject(parsed, ProtocolVersion.V5); Defensive patterns
Strategy: validation
Validate before calling
if (!(parsed instanceof Number || parsed instanceof String))
throw new IllegalArgumentException("Expected a float (number or string), got: " + parsed.getClass().getSimpleName()); Type guard
static boolean isFloatCompatible(Object v) { return v instanceof Number || v instanceof String; } Try / catch
try { FloatType.instance.fromJSONObject(parsed, pv); } catch (MarshalException e) { /* reject record: non-numeric JSON value for float field */ } Prevention
- Emit float fields as JSON numbers from producers
- Validate JSON field types against the table schema before insert
- Never map booleans/arrays/objects into numeric columns
- Add schema-conformance tests for JSON payloads
When it happens
Trigger: Calling FloatType.instance.fromJSONObject(parsed, protocolVersion) where parsed is a JSON object that parsed to a non-String/non-Number class such as Boolean, Map, or List (the cast ((Number) parsed).floatValue() throws ClassCastException, rethrown as MarshalException).
Common situations: Inserting JSON like {"f": true} or {"f": [1,2]} into a float column via the JSON path; buggy client serializers emitting booleans/nulls for float fields; schema drift where a column changed type but old JSON payloads still flow.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Expected a UTF-8 string, but got a %s: %s
- Expected an ascii string, but got a %s: %s
- Expected a double value, but got a %s: %s
- Expected an empty string, but got: %s
- Expected a string representation of an inet value, but got a
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/1155dffb79e2e554.
Report an issue: GitHub.