json-path/JsonPath · error · MappingException

can not map a " + src.getClass() + " to " + Integer.class.ge

Error message

can not map a " + src.getClass() + " to " + Integer.class.getName()

What it means

JsonSmartMappingProvider's IntegerReader.convert() cannot convert the source object to a java.lang.Integer. It only handles Integer, Long, Double, BigDecimal, Float and String sources (plus null); any other runtime type reaches the final throw. This is a hard failure by design: the json-smart based mapping provider refuses unmappable types instead of guessing.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java:129

        }
        public Integer convert(Object src) {
            if(src == null){
                return null;
            }
            if(Integer.class.isAssignableFrom(src.getClass())){
               return (Integer) src;
            } else if (Long.class.isAssignableFrom(src.getClass())) {
                return ((Long) src).intValue();
            } else if (Double.class.isAssignableFrom(src.getClass())) {
                return ((Double) src).intValue();
            } else if (BigDecimal.class.isAssignableFrom(src.getClass())) {
                return ((BigDecimal) src).intValue();
            } else if (Float.class.isAssignableFrom(src.getClass())) {
                return ((Float) src).intValue();
            } else if (String.class.isAssignableFrom(src.getClass())) {
                return Integer.valueOf(src.toString());
            }
            throw new MappingException("can not map a " + src.getClass() + " to " + Integer.class.getName());
        }
    }
    private static class LongReader extends JsonReaderI<Long> {
        public LongReader() {
            super(null);
        }
        public Long convert(Object src) {
            if(src == null){
                return null;
            }
            if(Long.class.isAssignableFrom(src.getClass())){
                return (Long) src;
            } else if (Integer.class.isAssignableFrom(src.getClass())) {
                return ((Integer) src).longValue();
            } else if (Double.class.isAssignableFrom(src.getClass())) {
                return ((Double) src).longValue();
            } else if (BigDecimal.class.isAssignableFrom(src.getClass())) {
                return ((BigDecimal) src).longValue();

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Ensure the value being mapped is a scalar number or numeric String before calling map(..., Integer.class, ...).
  2. Check the path: use $.scalarField (an array index or object value), not a $.node that resolves to a JSONObject/JSONArray.
  3. Extract the value as Object first, verify with instanceof, and convert manually (e.g. ((Number)v).intValue()).
  4. Switch to a different MappingProvider (JacksonMappingProvider, GsonMappingProvider) that maps containers to POJOs.
  5. Implement and register a custom MappingProvider covering your source types.

Example fix

// before
Integer n = configuration.mappingProvider().map(jsonObject, Integer.class, configuration); // MappingException
// after
Object v = JsonPath.read(json, "$.count");
Integer n = (v instanceof Number) ? ((Number) v).intValue() : Integer.valueOf(v.toString());
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = JsonPath.read(json, "$.field");
if (!(v instanceof Number || v instanceof String))
    throw new IllegalArgumentException("Cannot map " + (v == null ? "null" : v.getClass()) + " to Integer");

Type guard

static Integer toInt(Object v) {
    if (v instanceof Number) return ((Number) v).intValue();
    if (v instanceof String) return Integer.valueOf((String) v);
    return null;
}

Try / catch

try {
    Integer n = cfg.mappingProvider().map(value, Integer.class, cfg);
} catch (MappingException e) {
    Integer n = toInt(value); // fallback manual conversion
    if (n == null) throw new IllegalArgumentException("Non-numeric value", e);
}

Prevention

When it happens

Trigger: Calling Configuration.mappingProvider(new JsonSmartMappingProvider()).map(obj, Integer.class, ...) (or reading a path with .map(Integer.class)) where obj is a runtime type outside {Integer, Long, Double, BigDecimal, Float, String} — e.g. a JSONObject, JSONArray, Boolean, Date or null-wrapped custom bean.

Common situations: Using JsonSmartMappingProvider (the default for JsonSmartProvider) but attempting to map a whole JSONObject/JSONArray node to Integer instead of a scalar; switching from Jackson/Gson provider where different intermediate types appear; passing parsed values wrapped in container types.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/f45e5f41ee2d5c06. Report an issue: GitHub.