{"record":{"id":"16ab33d758468494","repo":"google/gson","slug":"lossy-conversion-from-intvalue-to-short-at-pat","errorCode":null,"errorMessage":"Lossy conversion from ${intValue} to short; at path ${path}","messagePattern":"Lossy conversion from (.+?) to short; at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":244,"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":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L226-L262","documentation":"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.","triggerScenarios":"Deserializing a JSON number like 70000 or -40000 into a short or Short field.","commonSituations":"A port number or counter field modeled as short that exceeds 65535; an API field whose value range grew beyond short capacity over time.","solutions":["Change the field type from short/Short to int/Integer 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 Server { short port; } // max 65535\ngson.fromJson(\"{\\\"port\\\":70000}\", Server.class); // throws\n\n// after\nclass Server { int port; }","handlingStrategy":"validation","validationCode":"// Validate numeric range before deserializing into a short field\npublic static boolean isValidShort(int value) {\n    return value >= Short.MIN_VALUE && value <= 65535;\n}\n// Usage: check before calling fromJson, or model the field as int instead","typeGuard":null,"tryCatchPattern":"try {\n    Server s = gson.fromJson(json, Server.class);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"Lossy conversion\") && e.getMessage().contains(\"short\")) {\n        // widen the field type to int\n    }\n}","preventionTips":["Model fields as int when the value range may exceed [-32768, 65535]","Validate numeric ranges at the API boundary before deserialization","Review short fields in DTOs for potential overflow from external data"],"tags":["deserialization","short","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"}