{"record":{"id":"a10eee16147390c8","repo":"google/gson","slug":"invalid-bitset-value-type-tokentype-at-path","errorCode":null,"errorMessage":"Invalid bitset value type: ${tokenType}; at path ${path}","messagePattern":"Invalid bitset value type: (.+?); at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":122,"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":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L104-L140","documentation":"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.","triggerScenarios":"Deserialifying a JSON array containing null, an object, or a nested array into a BitSet field, e.g. [1, null, 0] or [1, {}, 0].","commonSituations":"Malformed API responses; schema mismatch where a bitset field receives mixed types; upstream serialization bug producing nulls in a bitset array.","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"],"exampleFix":"// before\ngson.fromJson(\"[1, null, 0]\", BitSet.class); // throws — NULL token\n\n// after — strip nulls from the array before deserializing\nJsonArray arr = JsonParser.parseString(\"[1, null, 0]\").getAsJsonArray();\narr.removeIf(JsonNull.class::isInstance);\nBitSet bs = gson.fromJson(arr, BitSet.class);","handlingStrategy":"validation","validationCode":"// Validate BitSet array element types before deserializing\npublic static void validateBitSetTypes(JsonArray arr) {\n    for (JsonElement e : arr) {\n        if (!e.isJsonPrimitive()) {\n            throw new IllegalArgumentException(\n                \"Invalid bitset element type: \" + e + \" (expected number, string, or boolean)\");\n        }\n        var p = e.getAsJsonPrimitive();\n        if (!p.isNumber() && !p.isString() && !p.isBoolean()) {\n            throw new IllegalArgumentException(\"Invalid bitset element: \" + e);\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    BitSet bs = gson.fromJson(json, BitSet.class);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"Invalid bitset value type\")) {\n        // filter out null or non-primitive elements before retrying\n    }\n}","preventionTips":["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"],"tags":["deserialization","bitset","validation","json-syntax"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}