json-path/JsonPath · error · MappingException
can not map a " + src.getClass() + " to " + Date.class.getNa
Error message
can not map a " + src.getClass() + " to " + Date.class.getName()
What it means
JsonSmartMappingProvider's DateReader.convert() cannot convert the source object to java.util.Date. It only accepts Date instances and Strings, and Strings are parsed with DateFormat.getInstance() (a SHORT locale format); every other runtime type throws MappingException. Note that a String in a non-default format throws MappingException wrapping the ParseException instead.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java:246
public DateReader() {
super(null);
}
public Date convert(Object src) {
if(src == null){
return null;
}
if(Date.class.isAssignableFrom(src.getClass())){
return (Date) src;
} else if(Long.class.isAssignableFrom(src.getClass())){
return new Date((Long) src);
} else if(String.class.isAssignableFrom(src.getClass())){
try {
return DateFormat.getInstance().parse(src.toString());
} catch (ParseException e) {
throw new MappingException(e);
}
}
throw new MappingException("can not map a " + src.getClass() + " to " + Date.class.getName());
}
}
private static class BooleanReader extends JsonReaderI<Boolean> {
public BooleanReader() {
super(null);
}
public Boolean convert(Object src) {
if(src == null){
return null;
}
if (Boolean.class.isAssignableFrom(src.getClass())) {
return (Boolean) src;
}
throw new MappingException("can not map a " + src.getClass() + " to " + Boolean.class.getName());
}
}
}
View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Convert timestamps yourself before mapping: new java.util.Date(longValue) for epoch millis.
- Pre-format String values to the locale SHORT pattern DateFormat.getInstance() expects, or parse with a dedicated formatter (Instant.parse / SimpleDateFormat).
- Check the JsonPath resolves to a Date-compatible scalar, not a container node.
- Use JacksonMappingProvider with a configured date deserializer for robust date handling.
- Register a custom MappingProvider with your own DateReader.
Example fix
// before Date d = cfg.mappingProvider().map(1715000000000L, Date.class, cfg); // MappingException // after Long ts = JsonPath.read(json, "$.createdAt"); Date d = new java.util.Date(ts);
Defensive patterns
Strategy: type-guard
Validate before calling
Object v = JsonPath.read(json, "$.field");
if (!(v instanceof java.util.Date || v instanceof String))
throw new IllegalArgumentException("Cannot map " + (v == null ? "null" : v.getClass()) + " to Date; pre-convert timestamps yourself"); Type guard
static java.util.Date toDate(Object v) {
if (v instanceof java.util.Date) return (java.util.Date) v;
if (v instanceof Number) return new java.util.Date(((Number) v).longValue());
if (v instanceof String) {
try { return java.time.Instant.parse((String) v); }
catch (Exception ignored) { }
}
return null;
} Try / catch
try {
Date d = cfg.mappingProvider().map(value, Date.class, cfg);
} catch (MappingException e) {
Date d = toDate(value);
if (d == null) throw new IllegalArgumentException("Unparseable date value", e);
} Prevention
- Handle epoch-millis Long values manually with new Date(millis); the provider won't.
- Remember DateFormat.getInstance() parses only locale SHORT format — parse ISO-8601 yourself.
- Prefer java.time types (Instant/LocalDateTime) parsed explicitly over java.util.Date mapping.
- Configure JacksonMappingProvider with a date deserializer for complex date formats.
When it happens
Trigger: map(obj, Date.class, configuration) on JsonSmartMappingProvider where obj is a Long timestamp, a JSONObject, JSONArray, or any non-Date/non-String type.
Common situations: Trying to map epoch-millis Long values to Date (unsupported); ISO-8601 strings like "2024-01-15T10:00:00Z" that DateFormat.getInstance() cannot parse; provider swap from Jackson which binds timestamps differently.
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 " + Long.class.getNa
- 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 " + Boolean.class.ge
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/b33325cf05a5c681.
Report an issue: GitHub.