google/gson · error · JsonSyntaxException

Lossy conversion from

Error message

Lossy conversion from ${intValue} to short; at path ${path}

What it means

The SHORT adapter reads a JSON number as an int and rejects values outside [-32768, 65535] (the extended upper bound supports unsigned short values). Values outside this range would lose data when cast to short, so Gson throws JsonSyntaxException.

Solutions

  1. Change the field type from short/Short to int/Integer to accommodate the value range
  2. Pre-validate the numeric range of the JSON value before deserializing
  3. Register a custom TypeAdapter that clamps or rejects out-of-range values

Example fix

// before
class Server { short port; } // max 65535
gson.fromJson("{\"port\":70000}", Server.class); // throws

// after
class Server { int port; }
Defensive patterns

Strategy: validation

Validate before calling

// Validate numeric range before deserializing into a short field
public static boolean isValidShort(int value) {
    return value >= Short.MIN_VALUE && value <= 65535;
}
// Usage: check before calling fromJson, or model the field as int instead

Try / catch

try {
    Server s = gson.fromJson(json, Server.class);
} catch (JsonSyntaxException e) {
    if (e.getMessage().contains("Lossy conversion") && e.getMessage().contains("short")) {
        // widen the field type to int
    }
}

Prevention

When it happens

Trigger: Deserializing a JSON number like 70000 or -40000 into a short or Short field.

Common situations: A port number or counter field modeled as short that exceeds 65535; an API field whose value range grew beyond short capacity over time.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/16ab33d758468494. Report an issue: GitHub.

Appendix: source

Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:244

  public static final TypeAdapter<Number> SHORT =
      new TypeAdapter<Number>() {
        @Override
        public Number read(JsonReader in) throws IOException {
          if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return null;
          }

          int intValue;
          try {
            intValue = in.nextInt();
          } catch (NumberFormatException e) {
            throw new JsonSyntaxException(e);
          }
          // Allow up to 65535 to support unsigned values
          if (intValue > 65535 || intValue < Short.MIN_VALUE) {
            throw new JsonSyntaxException(
                "Lossy conversion from " + intValue + " to short; at path " + in.getPreviousPath());
          }
          return (short) intValue;
        }

        @Override
        public void write(JsonWriter out, Number value) throws IOException {
          if (value == null) {
            out.nullValue();
          } else {
            out.value(value.shortValue());
          }
        }
      };

  public static final TypeAdapterFactory SHORT_FACTORY =
      newFactory(short.class, Short.class, SHORT);

View on GitHub (pinned to 310ac341f2)