{"id":"4005ff10ac6fdfb7","repo":"google/gson","slug":"lossy-conversion-from-intvalue-to-short-a","errorCode":null,"errorMessage":"Lossy conversion from \" + intValue + \" to short; at path \" + in.getPreviousPath()","messagePattern":"Lossy conversion from \" \\+ intValue \\+ \" to short; 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":242,"sourceCode":"\n  public static final TypeAdapter<Number> SHORT =\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 65535 to support unsigned values\n          if (intValue > 65535 || intValue < Short.MIN_VALUE) {\n            throw new JsonSyntaxException(\n                \"Lossy conversion from \" + intValue + \" to short; at path \" + in.getPreviousPath());\n          }\n          return (short) 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.shortValue());\n          }\n        }\n      };\n\n  public static final TypeAdapterFactory SHORT_FACTORY =\n      newFactory(short.class, Short.class, SHORT);\n","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L224-L260","documentation":"The SHORT TypeAdapter reads an integer and rejects values outside [-32768, 65535] (the 65535 upper bound supports unsigned short). Anything wider is reported as a lossy conversion with JsonSyntaxException including the value and previous path.","triggerScenarios":"Deserialifying a JSON number like 70000 or -40000 into a short/Short field; receiving identifiers or port-like values that overflow a short; signed/unsigned boundary confusion. Triggered at line 242.","commonSituations":"Port numbers beyond 32767 stored as short; year fields approaching overflow; counters that exceed short range; cross-language protocols with different numeric widths.","solutions":["Change the field type from short/Short to int.","Constrain the upstream value to the short range before serialization.","Register a custom TypeAdapter<Number> for Short that clamps.","Re-evaluate whether the field genuinely needs to be a short."],"exampleFix":"// before\nclass Endpoint { short port; }\ngson.fromJson(\"{\\\"port\\\":70000}\", Endpoint.class); // throws\n\n// after\nclass Endpoint { int port; }","handlingStrategy":"validation","validationCode":"int v = JsonParser.parseString(json).getAsJsonObject().get(\"port\").getAsInt();\nif (v > 65535 || v < Short.MIN_VALUE) throw new IllegalArgumentException(\"Lossy short: \" + v);","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, Endpoint.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Lossy conversion from\") && e.getMessage().contains(\"to short\")) {\n    // widen the field type to int and retry\n  } else throw e;\n}","preventionTips":["Prefer int over short for wire numeric fields.","Constrain the producer to the short range.","Re-evaluate whether short is the right model type."],"tags":["gson","short","deserialization","number","range"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}