{"record":{"id":"1d013ff318b8a300","repo":"google/gson","slug":"invalid-bitset-value-intvalue-expected-0-or-1","errorCode":null,"errorMessage":"Invalid bitset value ${intValue}, expected 0 or 1; at path ${path}","messagePattern":"Invalid bitset value (.+?), expected 0 or 1; at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":111,"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":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L93-L129","documentation":"The built-in BitSet adapter reads a JSON array and expects each element to be 0 or 1 (from NUMBER or STRING tokens). If a numeric element is any other value, it throws JsonSyntaxException because the bit value cannot be determined.","triggerScenarios":"Deserializing a JSON array like [0, 2, 1] into a BitSet field — the value 2 is neither 0 nor 1.","commonSituations":"An API returns integer values other than 0/1 for a bitmask field; confusion between BitSet and general integer-array representations; data from a system that uses -1/0/1 conventions.","solutions":["Pre-validate the JSON array to ensure all numeric elements are 0 or 1 before deserializing","Register a custom TypeAdapter<BitSet> that maps a wider range of values (e.g. non-zero = true)","Fix the source data to emit only 0 and 1 values"],"exampleFix":"// before\ngson.fromJson(\"[0, 2, 1]\", BitSet.class); // throws on value 2\n\n// after — use boolean representation or fix data\ngson.fromJson(\"[false, true, true]\", BitSet.class); // booleans are accepted","handlingStrategy":"validation","validationCode":"// Validate BitSet array values before deserializing\npublic static void validateBitSet(JsonArray arr) {\n    for (JsonElement e : arr) {\n        if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isNumber()) {\n            int v = e.getAsInt();\n            if (v != 0 && v != 1) {\n                throw new IllegalArgumentException(\"Invalid bitset value: \" + v + \" (expected 0 or 1)\");\n            }\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\")) {\n        // sanitize input or register a custom BitSet adapter\n    }\n}","preventionTips":["Validate bitset array elements are 0 or 1 before deserializing","Use boolean arrays ([true, false]) instead of integer arrays when the source supports it","Register a custom TypeAdapter<BitSet> if the source data uses a different encoding"],"tags":["deserialization","bitset","validation","numeric"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}