google/gson · error · JsonSyntaxException
Invalid bitset value type: " + tokenType + "; at path " + in
Error message
Invalid bitset value type: " + tokenType + "; at path " + in.getPath()
What it means
The BitSet TypeAdapter expects each element to be NUMBER, STRING, or BOOLEAN; any other JSON token (NULL, BEGIN_OBJECT, BEGIN_ARRAY, NAME) triggers JsonSyntaxException with the token type and current path. Essentially the array element is structurally wrong, not just out of range.
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:120
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 8b8628c656)
Solutions
- Filter out or replace null/nested elements in the source JSON before deserialization.
- Change the target type to a List<Object> or List<Integer> if the structure is genuinely heterogeneous.
- Register a custom TypeAdapter<BitSet> that tolerates null (treats as 0).
- Validate the JSON shape (array of scalars only) at the trust boundary.
Example fix
// before
BitSet b = gson.fromJson("[0,null,1]", BitSet.class); // throws
// after
JsonArray a = JsonParser.parseString(json).getAsJsonArray();
BitSet bs = new BitSet();
for (int i=0;i<a.size();i++) if (!a.get(i).isJsonNull() && a.get(i).getAsBoolean()) bs.set(i); Defensive patterns
Strategy: validation
Validate before calling
// Ensure BitSet array elements are NUMBER/STRING/BOOLEAN only
for (JsonElement e : arr) {
if (e.isJsonNull() || e.isJsonObject() || e.isJsonArray()) {
throw new IllegalArgumentException("Bad bitset element type: " + e);
}
} Type guard
null
Try / catch
try {
gson.fromJson(json, BitSet.class);
} catch (JsonSyntaxException e) {
if (e.getMessage().startsWith("Invalid bitset value type")) {
// filter out null/nested elements and retry
} else throw e;
} Prevention
- Validate JSON shape (flat array of scalars) before deserializing into BitSet.
- Switch to a tolerant custom adapter if nulls are expected.
- Keep schema validation at the trust boundary.
When it happens
Trigger: Deserializing a JSON array containing null, a nested object, or another array into a BitSet field; feeding malformed JSON where a BitSet is expected. Triggered at line 120 in the default branch of the switch.
Common situations: API responses that include `null` placeholders in flag arrays; nested structures where a BitSet was mistakenly declared; partially-typed schema changes; corrupt/attacker-supplied JSON.
Related errors
- Invalid bitset value " + intValue + ", expected 0 or 1; at p
- duplicate key: {key}
- Expecting number, got: " + jsonToken + "; at path " + in.get
- Unexpected token: " + peeked
- null is not allowed as value for record component '" + field
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/82cdcd027f91b612.json.
Report an issue: GitHub.