google/gson · error · IllegalArgumentException
Too big for an int: " + x
Error message
Too big for an int: " + x
What it means
Gson's Calendar/IntegerFieldsTypeAdapter reads year/month/day/hour/minute/second as long values then narrows them to int via the internal toIntExact helper. If a field exceeds Integer.MAX_VALUE or is below Integer.MIN_VALUE, toIntExact throws IllegalArgumentException 'Too big for an int: <x>'. This guards against silent truncation of absurdly large numeric fields.
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:910
@Override
long[] integerValues(Calendar calendar) {
return new long[] {
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND)
};
}
};
// TODO: update this when we are on at least Android API Level 24.
private static int toIntExact(long x) {
int i = (int) x;
if (i != x) {
throw new IllegalArgumentException("Too big for an int: " + x);
}
return i;
}
public static final TypeAdapterFactory CALENDAR_FACTORY =
newFactoryForMultipleTypes(Calendar.class, GregorianCalendar.class, CALENDAR);
public static final TypeAdapter<Locale> LOCALE =
new TypeAdapter<Locale>() {
@Override
public Locale read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String locale = in.nextString();
StringTokenizer tokenizer = new StringTokenizer(locale, "_");
String language = null;View on GitHub (pinned to 8b8628c656)
Solutions
- Inspect the offending field reported in the exception and correct the source data.
- Validate/sanitize the JSON before deserialization, clamping or rejecting out-of-range numbers.
- Switch to java.time types (Instant, ZonedDateTime) with a proper date-time adapter instead of the legacy Calendar representation.
- Register a custom TypeAdapter<Calendar> that interprets the numeric fields differently.
Example fix
// before
Calendar cal = gson.fromJson("{\"year\":1700000000000,\"month\":0,...}", Calendar.class);
// after: use java.time + ISO format
Instant inst = gson.fromJson("\"2024-01-01T00:00:00Z\"", Instant.class); Defensive patterns
Strategy: validation
Validate before calling
boolean calendarFieldsInRange(long[] v) {
for (long x : v) if (x < Integer.MIN_VALUE || x > Integer.MAX_VALUE) return false;
return true;
} Type guard
static boolean isIntRange(long x) { return x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE; } Try / catch
try {
Calendar c = gson.fromJson(json, Calendar.class);
} catch (IllegalArgumentException e) {
// one of the six fields overflowed int; sanitize and retry, or reject
} Prevention
- Prefer java.time over legacy Calendar.
- Validate numeric ranges in the source data before mapping to Calendar.
- Treat epoch-millis-in-year-field as a producer bug, not a Gson bug.
When it happens
Trigger: Deserializing a Calendar (legacy mode) where one of the six integer fields (year/month/day/hour/minute/second) is a JSON number outside the int range, e.g. year 5000000000.
Common situations: Garbage or sentinel values from upstream, epoch-millisecond accidentally placed in the year field, or non-Gregorian calendars producing out-of-range components.
Related errors
- duplicate key: {key}
- Expecting number, got: " + jsonToken + "; at path " + in.get
- Unexpected token: " + peeked
- Failed parsing '" + s + "' as BigDecimal; at path " + in.get
- Failed parsing '" + s + "' as BigInteger; at path " + in.get
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/195e40ea4def0dca.json.
Report an issue: GitHub.