google/gson · error · JsonSyntaxException
Invalid bitset value type: ${tokenType}; at path ${path}
Error message
Invalid bitset value type: ${tokenType}; at path ${path} What it means
The BitSet adapter handles NUMBER, STRING, and BOOLEAN token types. If an array element is any other JSON token type (NULL, BEGIN_OBJECT, BEGIN_ARRAY), it falls through to the default case and throws JsonSyntaxException identifying the unexpected token type.
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:122
case STRING:
int intValue = in.nextInt();
if (intValue == 0) {
set = false;
} else if (intValue == 1) {
set = true;
} else {
throw new JsonSyntaxException(
"Invalid bitset value "
+ intValue
+ ", expected 0 or 1; at path "
+ in.getPreviousPath());
}
break;
case BOOLEAN:
set = in.nextBoolean();
break;
default:
throw new JsonSyntaxException(
"Invalid bitset value type: " + tokenType + "; at path " + in.getPath());
}
if (set) {
bitset.set(i);
}
++i;
tokenType = in.peek();
}
in.endArray();
return bitset;
}
@Override
public void write(JsonWriter out, BitSet src) throws IOException {
out.beginArray();
for (int i = 0, length = src.length(); i < length; i++) {
int value = src.get(i) ? 1 : 0;
out.value(value);View on GitHub (pinned to 310ac341f2)
Solutions
- Validate the JSON array structure before deserialization — ensure every element is a number, string, or boolean
- Clean or reject malformed input at the API boundary
- Register a custom TypeAdapter<BitSet> that tolerates and skips null elements
Example fix
// before
gson.fromJson("[1, null, 0]", BitSet.class); // throws — NULL token
// after — strip nulls from the array before deserializing
JsonArray arr = JsonParser.parseString("[1, null, 0]").getAsJsonArray();
arr.removeIf(JsonNull.class::isInstance);
BitSet bs = gson.fromJson(arr, BitSet.class); Defensive patterns
Strategy: validation
Validate before calling
// Validate BitSet array element types before deserializing
public static void validateBitSetTypes(JsonArray arr) {
for (JsonElement e : arr) {
if (!e.isJsonPrimitive()) {
throw new IllegalArgumentException(
"Invalid bitset element type: " + e + " (expected number, string, or boolean)");
}
var p = e.getAsJsonPrimitive();
if (!p.isNumber() && !p.isString() && !p.isBoolean()) {
throw new IllegalArgumentException("Invalid bitset element: " + e);
}
}
} Try / catch
try {
BitSet bs = gson.fromJson(json, BitSet.class);
} catch (JsonSyntaxException e) {
if (e.getMessage().contains("Invalid bitset value type")) {
// filter out null or non-primitive elements before retrying
}
} Prevention
- Validate that every element in a bitset JSON array is a number, string, or boolean
- Clean API responses to remove nulls or objects from bitset arrays at the boundary
- Register a custom BitSet adapter that tolerates unexpected element types
When it happens
Trigger: Deserialifying a JSON array containing null, an object, or a nested array into a BitSet field, e.g. [1, null, 0] or [1, {}, 0].
Common situations: Malformed API responses; schema mismatch where a bitset field receives mixed types; upstream serialization bug producing nulls in a bitset array.
Related errors
- Invalid bitset value ${intValue}, expected 0 or 1; at path $
- Expecting character, got: ${str}; at ${path}
- cannot deserialize ${baseType} because it does not define a
- cannot deserialize ${baseType} subtype named ${label}; did y
- Failed to parse date [${input}]: ${fail.getMessage()}
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/a10eee16147390c8.
Report an issue: GitHub.