jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Feature request: Support Suppliers in Jackson 3

Open chrylis opened this issue 1 year ago • 1 comments

There are a number of places across Jackson where no-arg constructors are required, such as for TypeIdResolvers. I have some use cases involving both partitioned type names (separate JSON:API "APIs", where type names are namespaced) and dynamic type resolution where it would be helpful to be able to parameterize the TypeIdResolver used in particular cases.

In addition, inside my implementation of TypeIdResolver I would like to be able to capture the information I'm using to resolve the type and insert it into the instance of the data class that's produced by readValue.

For Jackson 3, I would like to be able to pass a Supplier<TypeIdResolver> instead of specifying a Class<? extends TypeIdResolver> that must have a no-arg constructor; similarly, instead of returning JavaType from typeFromId, I would like to be able to return a Supplier<T>, so that I can do something like:

Supplier<Foo> somethingFromId(DatabindContext ctx, String typeId) {
  return () -> {
    var foo = new Foo();
    foo.setType(typeId);
    return foo;
  };
}

If done as a consistent global refactor, supporting the no-arg constructor case is still simple (Bar::new), but extending Jackson would be both easier and probably cleaner.

chrylis avatar Sep 01 '22 19:09 chrylis

i don't understand, where would you pass the supplier? we can't change the annotation to use a supplier instead of a class, because annotations don't support that.

If it helps, you can already provide your own HandlerInstantiator, which you can use to hook into the creation logic of the resolvers.

yawkat avatar Sep 02 '22 07:09 yawkat