{"record":{"id":"5a12e5054f3d7257","repo":"json-path/JsonPath","slug":"json-decimal-number-cannot-be-mapped-to-classn","errorCode":null,"errorMessage":"JSON decimal number cannot be mapped to \" + className","messagePattern":"JSON decimal 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":242,"sourceCode":"\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\n        String className = targetType.getSimpleName();\r\n        throw new MappingException(\"JSON decimal number cannot be mapped to \" + className);\r\n    }\r\n\r\n    private Object unwrapJsonValue(Object jsonValue) {\r\n        if (jsonValue == null) {\r\n            return null;\r\n        }\r\n        if (!(jsonValue instanceof JsonValue)) {\r\n            return jsonValue;\r\n        }\r\n        switch (((JsonValue) jsonValue).getValueType()) {\r\n        case ARRAY:\r\n        \t// TODO do we unwrap JsonObjectArray proxies?\r\n            //return ((JsonArray) jsonValue).getValuesAs(JsonValue.class);\r\n            return ((JsonArray) jsonValue).getValuesAs((JsonValue v) -> unwrapJsonValue(v));\r\n        case OBJECT:\r\n            throw new IllegalArgumentException(\"Use map() method to databind a JsonObject\");\r\n        case STRING:\r\n            return ((JsonString) jsonValue).getString();\r","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L224-L260","documentation":"mapDecimalJsonNumber converts a JSON decimal (non-integral) number to Float/Double/BigDecimal (and primitive variants). Any other target type throws MappingException with the target's simple name — e.g. requesting Integer or String for a value like 3.14.","triggerScenarios":"read(path, Integer.class) or read(path, String.class) where the JSON value is a decimal number; read(path, MyClass.class) on a JSON fractional number.","commonSituations":"Documents where a value that is usually integral occasionally arrives as 1.0/1.5, so the Integer-typed read fails; expecting strings or custom types from JSON decimals; provider switch removing implicit narrowing/coercion.","solutions":["Request Double.class, Float.class or BigDecimal.class instead of the unsupported type","Read as BigDecimal and convert explicitly (intValue(), longValue(), toPlainString())","Use .map(...) with a custom conversion function","Adjust the path or document so the value's numeric shape matches the target"],"exampleFix":"// before\nint n = ctx.read(\"$.ratio\", int.class); // 1.5 -> MappingException\n// after\nint n = ctx.read(\"$.ratio\", BigDecimal.class).intValue();","handlingStrategy":"type-guard","validationCode":"Object raw = ctx.read(path);\nif (!(raw instanceof Number)) throw new IllegalStateException(path + \" is not numeric\");\nDouble d = ctx.read(path, Double.class);","typeGuard":"boolean isDecimalNumber(Object v) { return v instanceof Double || v instanceof Float || v instanceof BigDecimal; }","tryCatchPattern":"try {\n    Double d = ctx.read(path, Double.class);\n} catch (MappingException e) {\n    BigDecimal bd = ctx.read(path, BigDecimal.class);\n    double d2 = bd.doubleValue();\n}","preventionTips":["Request Double/Float/BigDecimal for decimal JSON numbers","Read as BigDecimal and convert for other types","Validate with a JSON schema (type: number) before mapping","Account for values that may arrive as 1.0 instead of 1 and choose tolerant targets"],"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-14T11:17:12.474Z"}