json-path/JsonPath · error · MappingException

Cannot convert a " + source.getClass().getName() + " to a "

Error message

Cannot convert a " + source.getClass().getName() + " to a " + targetType + " use Tapestry's TypeCoercer instead.

What it means

TapestryMappingProvider.map() failed to convert the source object to the requested target type using Tapestry's built-in transformations and fell through to this catch-all MappingException. Conversion runs inside try/catch with the exception swallowed, so any failure (no registered coercer, wrong runtime type) surfaces as this generic message. The message hints that Tapestry's TypeCoercer service would handle it if wired up.

Source

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

      return null;
    }
    if (targetType.isAssignableFrom(source.getClass())) {
      return (T) source;
    }
    try {
      if (targetType.isAssignableFrom(ArrayList.class) && configuration.jsonProvider().isArray(source)) {
        int length = configuration.jsonProvider().length(source);
        @SuppressWarnings("rawtypes")
        ArrayList list = new ArrayList(length);
        for (Object o : configuration.jsonProvider().toIterable(source)) {
          list.add(o);
        }
        return (T) list;
      }
    } catch (Exception e) {

    }
    throw new MappingException("Cannot convert a " + source.getClass().getName() + " to a " + targetType
        + " use Tapestry's TypeCoercer instead.");
  }

  @Override
  public <T> T map(final Object source, final TypeRef<T> targetType, final Configuration configuration) {
    throw new UnsupportedOperationException(
        "Tapestry JSON provider does not support TypeRef! Use a Jackson or Gson based provider");
  }

}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Verify a Tapestry TypeCoercer transformation exists for source type -> target type; contribute one via Contribution to TypeCoercer if missing.
  2. Convert only types the default coercers support (String, Number, Boolean, Map, List basics).
  3. Read the JSON into Maps/Lists and build your POJO manually.
  4. Switch to JacksonMappingProvider or GsonMappingProvider, which deserialize into arbitrary POJOs.
  5. Register a custom MappingProvider implementation.

Example fix

// before
MyPojo p = new TapestryMappingProvider().map(jsonObject, MyPojo.class, configuration); // MappingException
// after
MyPojo p = new JacksonMappingProvider().map(jsonObject, MyPojo.class, configuration);
Defensive patterns

Strategy: try-catch

Validate before calling

MappingProvider mp = new TapestryMappingProvider();
Set<Class<?>> supported = Set.of(String.class, Integer.class, Long.class, Double.class, Boolean.class, Map.class, List.class);
if (!supported.contains(targetType) && !targetType.isInstance(source))
    throw new IllegalArgumentException("No Tapestry coercer for " + source.getClass() + " -> " + targetType);

Type guard

static <T> boolean canMap(Object source, Class<T> targetType) {
    return source == null || targetType.isInstance(source)
        || Number.class.isAssignableFrom(targetType) && source instanceof Number;
}

Try / catch

try {
    T value = tapestryProvider.map(source, targetType, configuration);
} catch (MappingException e) {
    T value = fallbackProvider.map(source, targetType, configuration); // e.g. JacksonMappingProvider
}

Prevention

When it happens

Trigger: Calling TapestryMappingProvider.map(source, targetType, configuration) where source's runtime type has no registered TypeCoercer transformation to targetType — e.g. mapping a JSONObject to a custom POJO, or a String to a type without a coercer. All TypeRef-based calls go to the other overload (error 157).

Common situations: Using the Tapestry integration outside a full Tapestry IoC registry where the TypeCoercer service isn't configured with extra coercers; mapping to POJOs/dates that the default coercer set doesn't cover; switching providers from Jackson to Tapestry and losing deserialization power.

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/21dd6ad9cea99889. Report an issue: GitHub.