{"id":"2494e5932dc77fbb","repo":"google/gson","slug":"lossy-conversion-from-intvalue-to-byte-at","errorCode":null,"errorMessage":"Lossy conversion from \" + intValue + \" to byte; at path \" + in.getPreviousPath()","messagePattern":"Lossy conversion from \" \\+ intValue \\+ \" to byte; at path \" \\+ in\\.getPreviousPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":207,"sourceCode":"\n  public static final TypeAdapter<Number> BYTE =\n      new TypeAdapter<Number>() {\n        @Override\n        public Number read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();\n            return null;\n          }\n\n          int intValue;\n          try {\n            intValue = in.nextInt();\n          } catch (NumberFormatException e) {\n            throw new JsonSyntaxException(e);\n          }\n          // Allow up to 255 to support unsigned values\n          if (intValue > 255 || intValue < Byte.MIN_VALUE) {\n            throw new JsonSyntaxException(\n                \"Lossy conversion from \" + intValue + \" to byte; at path \" + in.getPreviousPath());\n          }\n          return (byte) intValue;\n        }\n\n        @Override\n        public void write(JsonWriter out, Number value) throws IOException {\n          if (value == null) {\n            out.nullValue();\n          } else {\n            out.value(value.byteValue());\n          }\n        }\n      };\n\n  public static final TypeAdapterFactory BYTE_FACTORY = newFactory(byte.class, Byte.class, BYTE);\n\n  public static final TypeAdapter<Number> SHORT =","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L189-L225","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Change the field type from byte/Byte to short or int to accommodate the range.","Clamp the source value to [-128, 255] before sending the JSON.","Register a custom TypeAdapter<Number> for Byte that clamps or wraps.","Fix the upstream producer to send values within the byte range."],"exampleFix":"// before\nclass Pixel { byte r; }\ngson.fromJson(\"{\\\"r\\\":300}\", Pixel.class); // throws\n\n// after\nclass Pixel { int r; }\n// or clamp at source: Math.max(0, Math.min(255, r))","handlingStrategy":"validation","validationCode":"// Pre-check integer range before deserializing into a byte field\nint v = JsonParser.parseString(json).getAsJsonObject().get(\"r\").getAsInt();\nif (v > 255 || v < Byte.MIN_VALUE) throw new IllegalArgumentException(\"Lossy byte: \" + v);","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, Pixel.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Lossy conversion from\") && e.getMessage().contains(\"to byte\")) {\n    // widen the field type to int and retry\n  } else throw e;\n}","preventionTips":["Prefer int over byte for external-facing numeric fields.","Clamp values at the source to [-128, 255].","Document byte-range expectations in your API schema."],"tags":["gson","byte","deserialization","number","range"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}