json-path/JsonPath · error · MappingException
can not map a " + src.getClass() + " to " + Long.class.getNa
Error message
can not map a " + src.getClass() + " to " + Long.class.getName()
What it means
JsonSmartMappingProvider's LongReader.convert() cannot convert the source object to a java.lang.Long. It supports Long, Integer, Short, Byte, Double, BigDecimal, Float and String sources; any other runtime type (containers, booleans, dates, arbitrary beans) is rejected with MappingException. The provider deliberately maps only well-known scalar types.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java:153
}
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();
} else if (Float.class.isAssignableFrom(src.getClass())) {
return ((Float) src).longValue();
} else if (String.class.isAssignableFrom(src.getClass())) {
return Long.valueOf(src.toString());
}
throw new MappingException("can not map a " + src.getClass() + " to " + Long.class.getName());
}
}
private static class DoubleReader extends JsonReaderI<Double> {
public DoubleReader() {
super(null);
}
public Double convert(Object src) {
if(src == null){
return null;
}
if(Double.class.isAssignableFrom(src.getClass())){
return (Double) src;
} else if (Integer.class.isAssignableFrom(src.getClass())) {
return ((Integer) src).doubleValue();
} else if (Long.class.isAssignableFrom(src.getClass())) {
return ((Long) src).doubleValue();
} else if (BigDecimal.class.isAssignableFrom(src.getClass())) {View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Confirm the source resolves to a number or numeric String before requesting Long.class.
- Narrow the JsonPath so it selects a scalar leaf value rather than a container node.
- Pre-convert: read as Object, check instanceof Number / parse the String yourself.
- Use JacksonMappingProvider or GsonMappingProvider if you need richer type coercion.
- Register a custom MappingProvider implementation for your types.
Example fix
// before Long id = cfg.mappingProvider().map(node, Long.class, cfg); // node is a JSONObject // after Object v = JsonPath.read(json, "$.id"); Long id = Long.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 Long"); Type guard
static Long toLong(Object v) {
if (v instanceof Number) return ((Number) v).longValue();
if (v instanceof String) return Long.valueOf((String) v);
return null;
} Try / catch
try {
Long n = cfg.mappingProvider().map(value, Long.class, cfg);
} catch (MappingException e) {
Long n = toLong(value);
if (n == null) throw new IllegalArgumentException("Non-numeric value", e);
} Prevention
- Confirm the JSON field is a number or numeric string before requesting Long.class.
- Avoid mapping boolean/date/container nodes to numeric types.
- Unit-test path expressions against real payloads so container nodes are caught early.
- Convert epoch timestamps with new Date(long) instead of expecting Date/Long coercion.
When it happens
Trigger: Calling map(obj, Long.class, configuration) on the JsonSmart mapping provider where obj's runtime class is not a numeric type or String — e.g. obj is a JSONObject, JSONArray, Boolean or Date.
Common situations: Mapping a JSON node that is actually an object/array to Long; mapping boolean fields to numeric IDs; migrating code from a provider (Jackson/Gson) that coerced more types; timestamp fields that arrived as strings with non-numeric content.
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
- can not map a " + src.getClass() + " to " + Integer.class.ge
- can not map a " + src.getClass() + " to " + Double.class.get
- can not map a " + src.getClass() + " to " + Float.class.getN
- can not map a " + src.getClass() + " to " + Date.class.getNa
- can not map a " + src.getClass() + " to " + Boolean.class.ge
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/4a6d2a93b18035a9.
Report an issue: GitHub.