google/gson · error · IllegalArgumentException
Invalid version
Error message
Invalid version: ${version} What it means
Thrown by GsonBuilder.setVersion(double) when the supplied version is NaN or negative. Versions drive field/class inclusion via @Since and @Until annotations, so a non-finite or negative number is meaningless and rejected with an IllegalArgumentException before being applied to the Excluder.
Solutions
- Pass a non-negative, finite double version (>= 0.0 and not NaN), e.g. setVersion(1.2).
- Validate the value before calling: if (!Double.isNaN(v) && v >= 0.0) builder.setVersion(v).
- Use 0.0 or omit setVersion to disable versioning rather than passing a sentinel.
- Parse config with Double.parseDouble guarded by a try/catch to avoid NaN propagation.
Example fix
// before builder.setVersion(parseDoubleOrNaN(config)); // NaN -> throws // after double v = parseDoubleOrNaN(config); if (!Double.isNaN(v) && v >= 0.0) builder.setVersion(v);
Defensive patterns
Strategy: validation
Validate before calling
// Validate version before applying
if (Double.isNaN(version) || version < 0.0) {
throw new IllegalArgumentException("version must be >= 0 and not NaN, got " + version);
}
builder.setVersion(version); Try / catch
try { builder.setVersion(v); }
catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Invalid version")) { /* default version or fail config */ }
else throw e;
} Prevention
- Guard config parsing so NaN/negative never reach setVersion.
- Use 0.0 or omit setVersion to disable versioning.
- Unit-test the version config path.
When it happens
Trigger: Passing Double.NaN; a negative double (e.g. from a subtraction bug); computing the version from unparsed config that yields NaN; passing 0.0/0.0.
Common situations: Reading version from a properties file and parsing fails to NaN; arithmetic that can go negative; default/uninitialized double field passed through; test values like -1 to mean 'unset'.
Related errors
- Invalid style
- Class does not implement any supported type adapter class…
- Only combinations of \n and \r are allowed in newline.
- Only combinations of spaces and tabs are allowed in indent.
- The date pattern ' ' is not valid
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/da1e36742bce9038.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/GsonBuilder.java:207
/**
* Configures Gson to enable versioning support. Versioning support works based on the annotation
* types {@link Since} and {@link Until}. It allows including or excluding fields and classes
* based on the specified version. See the documentation of these annotation types for more
* information.
*
* <p>By default versioning support is disabled and usage of {@code @Since} and {@code @Until} has
* no effect.
*
* @param version the version number to use.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @throws IllegalArgumentException if the version number is NaN or negative
* @see Since
* @see Until
*/
@CanIgnoreReturnValue
public GsonBuilder setVersion(double version) {
if (Double.isNaN(version) || version < 0.0) {
throw new IllegalArgumentException("Invalid version: " + version);
}
excluder = excluder.withVersion(version);
return this;
}
/**
* Configures Gson to excludes all class fields that have the specified modifiers. By default,
* Gson will exclude all fields marked {@code transient} or {@code static}. This method will
* override that behavior.
*
* <p>This is a convenience method which behaves as if an {@link ExclusionStrategy} which excludes
* these fields was {@linkplain #setExclusionStrategies(ExclusionStrategy...) registered with this
* builder}.
*
* @param modifiers the field modifiers. You must use the modifiers specified in the {@link
* java.lang.reflect.Modifier} class. For example, {@link
* java.lang.reflect.Modifier#TRANSIENT}, {@link java.lang.reflect.Modifier#STATIC}.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" patternView on GitHub (pinned to 310ac341f2)