apache/cassandra · error · MarshalException
Expected a short value, but got a %s: %s
Error message
Expected a short value, but got a %s: %s
What it means
ShortType.fromJSONObject converts a parsed JSON value into a short Term. Only JSON strings and numbers are accepted; any other JSON type (object, array, boolean, null) cannot be interpreted as a short and triggers this MarshalException naming the offending Java class.
Source
Thrown at src/java/org/apache/cassandra/db/marshal/ShortType.java:95
try
{
s = Short.parseShort(source);
}
catch (Exception e)
{
throw new MarshalException(String.format("Unable to make short from '%s'", source), e);
}
return decompose(s);
}
public Term fromJSONObject(Object parsed) throws MarshalException
{
if (parsed instanceof String || parsed instanceof Number)
return new Constants.Value(fromString(String.valueOf(parsed)));
throw new MarshalException(String.format(
"Expected a short value, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
}
@Override
public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)
{
return Objects.toString(getSerializer().deserialize(buffer), "\"\"");
}
@Override
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.SMALLINT;
}
public TypeSerializer<Short> getSerializer()
{
return ShortSerializer.instance;View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Send the smallint as a JSON number or numeric string (e.g. 42 or "42").
- Guard the JSON document before sending: check the field is a number/string and non-null.
- Fix the producer so absent values are omitted rather than serialized as null/objects.
Example fix
// before
{"id": 1, "smallint_col": null}
// after
{"id": 1, "smallint_col": 42} Defensive patterns
Strategy: type-guard
Validate before calling
if (v != null && !(v instanceof String) && !(v instanceof Number)) throw new IllegalArgumentException("smallint must be number/string"); Type guard
boolean isShortLike(Object v) { return v instanceof String || v instanceof Number; } Try / catch
try { return ShortType.instance.fromJSONObject(parsed); } catch (MarshalException e) { /* handle non-numeric JSON type */ } Prevention
- Validate JSON payloads against a schema before INSERT JSON
- Ensure producers omit missing fields instead of emitting null/objects
- Map client numeric types to JSON numbers, not nested wrappers
When it happens
Trigger: Executing a JSON insert (INSERT ... JSON or fromJson()) where a smallint column receives a JSON true/false/null/array/object instead of a number or numeric string.
Common situations: JSON payloads generated programmatically where a field is accidentally a nested object or null because the upstream value was missing; schema drift between the client model and the CQL table.
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 an ascii string, but got a %s: %s
- Expected a boolean value, but got a %s: %s
- Expected a byte value, but got a %s: %s
- String representation of blob is missing 0x prefix: %s
- Value '%s' is not a valid blob representation: %s
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/c41e1a3f27053260.
Report an issue: GitHub.