{"record":{"id":"335776ee82b98c46","repo":"json-path/JsonPath","slug":"json-integral-number-cannot-be-mapped-to-class","errorCode":null,"errorMessage":"JSON integral number cannot be mapped to \" + className","messagePattern":"JSON integral number cannot be mapped to \" \\+ className","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":222,"sourceCode":"    private <T> T mapIntegralJsonNumber(JsonNumber jsonNumber, Class<?> targetType) {\r\n        if (targetType.isPrimitive()) {\r\n            if (int.class.equals(targetType)) {\r\n                return (T) Integer.valueOf(jsonNumber.intValueExact());\r\n            } else if (long.class.equals(targetType)) {\r\n                return (T) Long.valueOf(jsonNumber.longValueExact());\r\n            }\r\n        } else if (Integer.class.equals(targetType)) {\r\n            return (T) Integer.valueOf(jsonNumber.intValueExact());\r\n        } else if (Long.class.equals(targetType)) {\r\n            return (T) Long.valueOf(jsonNumber.longValueExact());\r\n        } else if (BigInteger.class.equals(targetType)) {\r\n            return (T) jsonNumber.bigIntegerValueExact();\r\n        } else if (BigDecimal.class.equals(targetType)) {\r\n            return (T) jsonNumber.bigDecimalValue();\r\n        }\r\n\r\n        String className = targetType.getSimpleName();\r\n        throw new MappingException(\"JSON integral number cannot be mapped to \" + className);\r\n    }\r\n\r\n    @SuppressWarnings(\"unchecked\")\r\n    private <T> T mapDecimalJsonNumber(JsonNumber jsonNumber, Class<?> targetType) {\r\n        if (targetType.isPrimitive()) {\r\n            if (float.class.equals(targetType)) {\r\n                return (T) new Float(jsonNumber.doubleValue());\r\n            } else if (double.class.equals(targetType)) {\r\n                return (T) Double.valueOf(jsonNumber.doubleValue());\r\n            }\r\n        } else if (Float.class.equals(targetType)) {\r\n            return (T) new Float(jsonNumber.doubleValue());\r\n        } else if (Double.class.equals(targetType)) {\r\n            return (T) Double.valueOf(jsonNumber.doubleValue());\r\n        } else if (BigDecimal.class.equals(targetType)) {\r\n            return (T) jsonNumber.bigDecimalValue();\r\n        }\r\n\r","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L204-L240","documentation":"mapIntegralJsonNumber converts a JSON integral number to primitive wrappers/BigInteger/BigDecimal. If the target class is none of the supported numeric types (Byte..Long, BigInteger, BigDecimal), it throws MappingException with the simple class name. E.g. requesting Float for an integral number or a non-numeric type like String.","triggerScenarios":"read(path, Float.class) or read(path, String.class) where the JSON value is an integral number like 42; read(path, SomeEnum.class or Date.class) on an integral JSON number.","commonSituations":"Expecting numeric strings or dates to be produced from JSON integers; asking for Float/Double when the document contains an integer literal; type parameter mismatch where generics erase to an unexpected class.","solutions":["Request one of the supported integral targets: Integer, Long, Short, Byte, BigInteger or BigDecimal","Request BigDecimal then convert (bd.floatValue(), bd.intValue())","Use .map(...) with a custom converter for unsupported target types","Change the path/document so the value matches the expected numeric type"],"exampleFix":"// before\nFloat f = ctx.read(\"$.count\", Float.class); // 42 -> MappingException\n// after\nFloat f = ctx.read(\"$.count\", BigDecimal.class).floatValue();","handlingStrategy":"type-guard","validationCode":"Object raw = ctx.read(path);\nif (!(raw instanceof Number)) throw new IllegalStateException(path + \" is not numeric\");\nInteger n = ctx.read(path, Integer.class);","typeGuard":"boolean isIntegralNumber(Object v) { return v instanceof Integer || v instanceof Long || v instanceof Short || v instanceof Byte || v instanceof BigInteger; }","tryCatchPattern":"try {\n    Integer n = ctx.read(path, Integer.class);\n} catch (MappingException e) {\n    BigDecimal bd = ctx.read(path, BigDecimal.class);\n    int n = bd.intValue();\n}","preventionTips":["Request only Integer/Long/Short/Byte/BigInteger/BigDecimal for integral JSON numbers","Read as BigDecimal and convert explicitly for exotic targets","Validate the JSON schema (type: integer) before typed reads","Keep target types aligned with the document's numeric literals"],"tags":["json","type-mapping","numbers","json-path"],"backgroundTag":"incompatible-source-type","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}