FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.53k stars 1.38k forks source link

Feature request: Support Suppliers in Jackson 3 #3588

Open chrylis opened 2 years ago

chrylis commented 2 years ago

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.

yawkat commented 2 years ago

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.