-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customization of ObjectReader
/ObjectWriter
during serialization
#35122
Comments
Hi @geoand, Using
I think I found a workaround for this, although I'm not completely happy with it since it requires multiple ObjectMapper instances, a @Provider
@ApplicationScoped
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper adminObjectMapper = new ObjectMapper();
private final ObjectMapper userObjectMapper = new ObjectMapper();
@PostConstruct
void initialize() {
adminObjectMapper.setConfig(
adminObjectMapper.getDeserializationConfig().withView(Views.Admin.class)
);
adminObjectMapper.setConfig(
adminObjectMapper.getSerializationConfig().withView(Views.Admin.class)
);
userObjectMapper.setConfig(
userObjectMapper.getDeserializationConfig().withView(Views.User.class)
);
userObjectMapper.setConfig(
userObjectMapper.getSerializationConfig().withView(Views.User.class)
);
}
@Override
public ObjectMapper getContext(Class<?> type) {
if (new Random().nextBoolean()) { // Just for demo purposes
return adminObjectMapper;
} else {
return userObjectMapper;
}
}
} I could execute my custom logic inside |
As I assume @Sgitario will also say, |
Ultimately, However, why not provide a |
Yeah, that's why I want to keep this open and see if people really need this |
There's one drawback my workaround comes with, though. Annotating an endpoint with I like the idea of |
Do you want me to work on adding it? |
If you having spare time, why not :) |
To customize the default object mapper, you must provide a custom
Will do |
What I meant was some kind of functionality to register a global Something like the following: @Produces
@Singleton
public BiFunction<ObjectMapper, Type, ObjectWriter> globalSerializer() {
return new MyCustomSerializer();
}
@Produces
@Singleton
public BiFunction<ObjectMapper, Type, ObjectReader> globalDeserializer() {
return new MyCustomDeserializer();
} This would result in a mechanism like |
The custom serialization is designed to work by method, not for global purposes. For the latter, the way to go should be |
#35138 adds the mentioned |
afaik, Assuming I have the following Views: public class Views {
public interface User {}
public interface Admin extends User {}
} And the following DTO: public class InformationDto {
@JsonView(Views.User.class)
private String publicInfo;
@JsonView(Views.Admin.class)
private String privateInfo;
public InformationDto(String publicInfo, String privateInfo) {
this.publicInfo = publicInfo;
this.privateInfo = privateInfo;
}
} Now, there's a resource method which returns this DTO. @GET
@Path("/")
public InformationDto getInformation() {
return new InformationDto("public", "private");
} Annotating the resource method with @GET
@Path("/")
@JsonViewResolver(MyViewResolver.class)
public InformationDto getInformation() {
return new InformationDto("public", "private");
} @ApplicationScoped
public class MyViewResolver implements ViewResolver {
@Override
public Class<?> resolve() {
// custom logic to return the appropriate view
}
} |
#35138 should allow you to do that |
Yes, this allows setting serializer / deserializer per method, but there doesn't seem to be a way to register a global serializer / deserializer for all methods. Saw your comment (#35138 (comment)) from the referencend PR. This would also work for me. I just don't want having to annotate each method with the same annotation. Also, thanks for creating a PR with a possible solution so fast! 🙂 |
+100 I will create a follow-up pull request after merging the linked PR. |
Done in #35158 |
Awesome! Thank you so much! 🙂 |
#35138 and #35122 have been merged. I'm closing this issue as done. |
Description
Hi,
jackson-jakarta-rs-providers
provides the following classes for customization ofObjectReader
/ObjectWriter
instances:ObjectReaderModifier
ObjectWrtierModifier
Searching the web and looking through sources, I couldn't find a way to use such a functionality within a Quarkus application. My use case requires to set different
JsonView
s based on a custom condition.I already looked into
ObjectMapperCustomizer
, but this only allows for (global) customization on theObjectMapper
instance and not on the underlyingObjectReader
andObjectWriter
instances used for serialization/deserialization.please see FasterXML/jackson-jaxrs-providers#33 for the relevant issue at
jackson-jakarta-rs-providers
.Implementation ideas
No response
The text was updated successfully, but these errors were encountered: