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

  1. Send the smallint as a JSON number or numeric string (e.g. 42 or "42").
  2. Guard the JSON document before sending: check the field is a number/string and non-null.
  3. 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

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


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/c41e1a3f27053260. Report an issue: GitHub.