google/gson · error · JsonSyntaxException

Lossy conversion from " + intValue + " to byte; at path " +

Error message

Lossy conversion from " + intValue + " to byte; at path " + in.getPreviousPath()

What it means

The BYTE TypeAdapter reads an integer but rejects values outside the allowed byte-ish range [-128, 255]. The upper bound is 255 (not 127) to support unsigned byte values, but anything outside that window is reported as a lossy conversion with JsonSyntaxException including the value and previous path.

Source

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

  public static final TypeAdapter<Number> BYTE =
      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 255 to support unsigned values
          if (intValue > 255 || intValue < Byte.MIN_VALUE) {
            throw new JsonSyntaxException(
                "Lossy conversion from " + intValue + " to byte; at path " + in.getPreviousPath());
          }
          return (byte) intValue;
        }

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

  public static final TypeAdapterFactory BYTE_FACTORY = newFactory(byte.class, Byte.class, BYTE);

  public static final TypeAdapter<Number> SHORT =

View on GitHub (pinned to 8b8628c656)

Solutions

  1. Change the field type from byte/Byte to short or int to accommodate the range.
  2. Clamp the source value to [-128, 255] before sending the JSON.
  3. Register a custom TypeAdapter<Number> for Byte that clamps or wraps.
  4. Fix the upstream producer to send values within the byte range.

Example fix

// before
class Pixel { byte r; }
gson.fromJson("{\"r\":300}", Pixel.class); // throws

// after
class Pixel { int r; }
// or clamp at source: Math.max(0, Math.min(255, r))
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check integer range before deserializing into a byte field
int v = JsonParser.parseString(json).getAsJsonObject().get("r").getAsInt();
if (v > 255 || v < Byte.MIN_VALUE) throw new IllegalArgumentException("Lossy byte: " + v);

Type guard

null

Try / catch

try {
  gson.fromJson(json, Pixel.class);
} catch (JsonSyntaxException e) {
  if (e.getMessage().startsWith("Lossy conversion from") && e.getMessage().contains("to byte")) {
    // widen the field type to int and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Deserialifying a JSON number like 300 or -200 into a byte/Byte field; receiving IDs or counts that overflow a byte; misconfigured upstream producing out-of-range integers. Triggered at line 207 when intValue>255 || intValue<Byte.MIN_VALUE.

Common situations: Compact protocol fields encoded as byte but receiving wider integers; unsigned/signed confusion across language boundaries; RGB color components that occasionally exceed 255; configuration values stored as byte but set to larger defaults.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/2494e5932dc77fbb.json. Report an issue: GitHub.