{"id":"2b5e923e1650be75","repo":"google/gson","slug":"invalid-bitset-value-intvalue-expected-0","errorCode":null,"errorMessage":"Invalid bitset value \" + intValue + \", expected 0 or 1; at path \" + in.getPreviousPath()","messagePattern":"Invalid bitset value \" \\+ intValue \\+ \", expected 0 or 1; at path \" \\+ in\\.getPreviousPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":109,"sourceCode":"      new TypeAdapter<BitSet>() {\n        @Override\n        public BitSet read(JsonReader in) throws IOException {\n          BitSet bitset = new BitSet();\n          in.beginArray();\n          int i = 0;\n          JsonToken tokenType = in.peek();\n          while (tokenType != JsonToken.END_ARRAY) {\n            boolean set;\n            switch (tokenType) {\n              case NUMBER:\n              case STRING:\n                int intValue = in.nextInt();\n                if (intValue == 0) {\n                  set = false;\n                } else if (intValue == 1) {\n                  set = true;\n                } else {\n                  throw new JsonSyntaxException(\n                      \"Invalid bitset value \"\n                          + intValue\n                          + \", expected 0 or 1; at path \"\n                          + in.getPreviousPath());\n                }\n                break;\n              case BOOLEAN:\n                set = in.nextBoolean();\n                break;\n              default:\n                throw new JsonSyntaxException(\n                    \"Invalid bitset value type: \" + tokenType + \"; at path \" + in.getPath());\n            }\n            if (set) {\n              bitset.set(i);\n            }\n            ++i;\n            tokenType = in.peek();","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L91-L127","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nBitSet b = gson.fromJson(\"[0,1,2]\", BitSet.class); // throws\n\n// after\nBitSet b = gson.fromJson(\"[0,1,0,1]\", BitSet.class);\n// or fix source to emit 0/1 per position","handlingStrategy":"validation","validationCode":"// Validate array elements are 0/1 (or boolean) before parsing into BitSet\nJsonArray arr = JsonParser.parseString(json).getAsJsonArray();\nfor (JsonElement e : arr) {\n  if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isNumber()) {\n    int v = e.getAsInt();\n    if (v != 0 && v != 1) throw new IllegalArgumentException(\"Bad bitset value \" + v);\n  }\n}","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, BitSet.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Invalid bitset value\")) {\n    // sanitize array and retry\n  } else throw e;\n}","preventionTips":["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."],"tags":["gson","bitset","deserialization","number","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}