{"record":{"id":"721541e768c1c73c","repo":"apache/cassandra","slug":"expected-an-ascii-string-but-got-a-s-s","errorCode":null,"errorMessage":"Expected an ascii string, but got a %s: %s","messagePattern":"Expected an ascii string, but got a (.+?): (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/AsciiType.java","lineNumber":81,"sourceCode":"        {\n            return theEncoder.encode(CharBuffer.wrap(source));\n        }\n        catch (CharacterCodingException exc)\n        {\n            throw new MarshalException(String.format(\"Invalid ASCII character in string literal: %s\", exc));\n        }\n    }\n\n    @Override\n    public Term fromJSONObject(Object parsed) throws MarshalException\n    {\n        try\n        {\n            return new Constants.Value(fromString((String) parsed));\n        }\n        catch (ClassCastException exc)\n        {\n            throw new MarshalException(String.format(\n                    \"Expected an ascii string, but got a %s: %s\", parsed.getClass().getSimpleName(), parsed));\n        }\n    }\n\n    @Override\n    public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)\n    {\n        try\n        {\n            return '\"' + JsonUtils.quoteAsJsonString(ByteBufferUtil.string(buffer, StandardCharsets.US_ASCII)) + '\"';\n        }\n        catch (CharacterCodingException exc)\n        {\n            throw new AssertionError(\"ascii value contained non-ascii characters: \", exc);\n        }\n    }\n\n    public CQL3Type asCQL3Type()","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/AsciiType.java#L63-L99","documentation":"AsciiType.fromJSONObject throws this when a JSON value parsed for an ascii column is not a JSON string (ClassCastException on the (String) cast). JSON numbers, booleans, objects, or arrays are rejected because ascii values must arrive as string literals. The Java type name and value are included in the message.","triggerScenarios":"JSON INSERT like INSERT INTO t JSON '{\"a\": 123}' where a is an ascii column; fromJson() function argument that is numeric or boolean instead of string for an ascii target; programmatic use of Term.fromJSONObject with a non-String parsed object.","commonSituations":"Schema evolution changed a column to ascii but JSON payloads still send numbers; developers forgetting JSON numbers must be quoted for string columns; auto-generated JSON from deserializers that emit unquoted numerics.","solutions":["Quote the value in JSON so it is a string: {\"a\": \"123\"}","Cast/serialize the value to String in application code before sending JSON","Change the column type to int/double if the data is truly numeric","Validate JSON payload types against the schema before sending"],"exampleFix":"// before\nINSERT INTO t JSON '{\"a\": 123}';\n// after\nINSERT INTO t JSON '{\"a\": \"123\"}';","handlingStrategy":"type-guard","validationCode":"if (!(value instanceof String)) throw new IllegalArgumentException(\"ascii JSON value must be a string\");","typeGuard":"boolean isAsciiJson(Object v) { return v instanceof String; }","tryCatchPattern":"try { Term t = asciiType.fromJSONObject(parsed); } catch (MarshalException e) { if (e.getMessage().startsWith(\"Expected an ascii string\")) { /* convert to String and retry */ } else throw e; }","preventionTips":["Always quote string-column values in JSON inserts","Map schema types to JSON types in client serializers","Validate JSON payloads against the table schema before send","Watch for ORMs emitting unquoted numerics"],"tags":["cassandra","marshal","json","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}