{"id":"82cdcd027f91b612","repo":"google/gson","slug":"invalid-bitset-value-type-tokentype-at-p","errorCode":null,"errorMessage":"Invalid bitset value type: \" + tokenType + \"; at path \" + in.getPath()","messagePattern":"Invalid bitset value type: \" \\+ tokenType \\+ \"; at path \" \\+ in\\.getPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":120,"sourceCode":"              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();\n          }\n          in.endArray();\n          return bitset;\n        }\n\n        @Override\n        public void write(JsonWriter out, BitSet src) throws IOException {\n          out.beginArray();\n          for (int i = 0, length = src.length(); i < length; i++) {\n            int value = src.get(i) ? 1 : 0;\n            out.value(value);","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L102-L138","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nBitSet b = gson.fromJson(\"[0,null,1]\", BitSet.class); // throws\n\n// after\nJsonArray a = JsonParser.parseString(json).getAsJsonArray();\nBitSet bs = new BitSet();\nfor (int i=0;i<a.size();i++) if (!a.get(i).isJsonNull() && a.get(i).getAsBoolean()) bs.set(i);","handlingStrategy":"validation","validationCode":"// Ensure BitSet array elements are NUMBER/STRING/BOOLEAN only\nfor (JsonElement e : arr) {\n  if (e.isJsonNull() || e.isJsonObject() || e.isJsonArray()) {\n    throw new IllegalArgumentException(\"Bad bitset element type: \" + e);\n  }\n}","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, BitSet.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Invalid bitset value type\")) {\n    // filter out null/nested elements and retry\n  } else throw e;\n}","preventionTips":["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."],"tags":["gson","bitset","deserialization","jsontoken","malformed"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}