google/gson · error · JsonSyntaxException
Invalid bitset value " + intValue + ", expected 0 or 1; at p
Error message
Invalid bitset value " + intValue + ", expected 0 or 1; at path " + in.getPreviousPath()
What it means
The BitSet TypeAdapter reads an array of numbers/strings/booleans. When an element parses as a NUMBER or STRING but the integer value is neither 0 nor 1, Gson throws JsonSyntaxException with the offending value and the previous path. BitSet semantics only allow bit-off (0) and bit-on (1).
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:109
new TypeAdapter<BitSet>() {
@Override
public BitSet read(JsonReader in) throws IOException {
BitSet bitset = new BitSet();
in.beginArray();
int i = 0;
JsonToken tokenType = in.peek();
while (tokenType != JsonToken.END_ARRAY) {
boolean set;
switch (tokenType) {
case NUMBER:
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();View on GitHub (pinned to 8b8628c656)
Solutions
- Ensure each element of the JSON array is exactly 0, 1, true, or false.
- If you have an index list (positions of set bits), preprocess it into a 0/1 array before deserialization.
- Change the target field type to List<Integer> or boolean[] if the source data is not a true bitset.
- Register a custom TypeAdapter<BitSet> that maps values like 2 -> set bit 1 etc., if your protocol encodes differently.
Example fix
// before
BitSet b = gson.fromJson("[0,1,2]", BitSet.class); // throws
// after
BitSet b = gson.fromJson("[0,1,0,1]", BitSet.class);
// or fix source to emit 0/1 per position Defensive patterns
Strategy: validation
Validate before calling
// Validate array elements are 0/1 (or boolean) before parsing into BitSet
JsonArray arr = JsonParser.parseString(json).getAsJsonArray();
for (JsonElement e : arr) {
if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isNumber()) {
int v = e.getAsInt();
if (v != 0 && v != 1) throw new IllegalArgumentException("Bad bitset value " + v);
}
} Type guard
null
Try / catch
try {
gson.fromJson(json, BitSet.class);
} catch (JsonSyntaxException e) {
if (e.getMessage().startsWith("Invalid bitset value")) {
// sanitize array and retry
} else throw e;
} Prevention
- Define your wire contract for BitSet as a 0/1 array and validate at the boundary.
- Prefer boolean[] on the wire if you control both ends.
- Reject index-list encodings before they reach Gson.
When it happens
Trigger: Deserialifying JSON like [0,1,2] or [0,1,"5"] into a BitSet; feeding arbitrary integer arrays to a BitSet-typed field; misconfigured serializers emitting raw integer lists where a BitSet is expected. Triggered at line 109.
Common situations: API contract drift where a numeric array is sent for a BitSet field; legacy data with multi-valued flags; client libraries that produce [0..N] index arrays misinterpreted by the receiving Gson model.
Related errors
- Invalid bitset value type: " + tokenType + "; at path " + in
- Lossy conversion from " + intValue + " to byte; at path " +
- Lossy conversion from " + intValue + " to short; at path " +
- null is not a valid AtomicLongArray element
- Expecting character, got: " + str + "; at " + in.getPrevious
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/2b5e923e1650be75.json.
Report an issue: GitHub.