{"record":{"id":"351ec2bd08dbd0d9","repo":"google/gson","slug":"lossy-conversion-from-intvalue-to-byte-at-path","errorCode":null,"errorMessage":"Lossy conversion from ${intValue} to byte; at path ${path}","messagePattern":"Lossy conversion from (.+?) to byte; at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":209,"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":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L191-L227","documentation":"The BYTE adapter reads a JSON number as an int and rejects values outside the range [-128, 255] (the extended upper bound supports unsigned byte values). Values outside this range would lose data when cast to byte, so Gson throws JsonSyntaxException rather than silently truncating.","triggerScenarios":"Deserializing a JSON number like 300 or -200 into a byte or Byte field.","commonSituations":"An API returns an int ID or counter that occasionally exceeds the byte range; a field modeled as byte that should be int or short; unsigned/signed confusion.","solutions":["Change the field type from byte/Byte to int/Integer or short/Short to accommodate the value range","Pre-validate the numeric range of the JSON value before deserializing","Register a custom TypeAdapter that clamps or rejects out-of-range values"],"exampleFix":"// before\nclass Config { byte port; } // max 255\ngson.fromJson(\"{\\\"port\\\":300}\", Config.class); // throws\n\n// after\nclass Config { int port; }","handlingStrategy":"validation","validationCode":"// Validate numeric range before deserializing into a byte field\npublic static boolean isValidByte(int value) {\n    return value >= Byte.MIN_VALUE && value <= 255;\n}\n// Usage: check before calling fromJson, or model the field as int instead","typeGuard":null,"tryCatchPattern":"try {\n    Config c = gson.fromJson(json, Config.class);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"Lossy conversion\") && e.getMessage().contains(\"byte\")) {\n        // widen the field type to int or short\n    }\n}","preventionTips":["Model fields as int or short when the value range may exceed [-128, 255]","Validate numeric ranges at the API boundary before deserialization","Review byte fields in DTOs for potential overflow from external data"],"tags":["deserialization","byte","numeric","range-check"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}