You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently found myself trying to communicate what amounts to a feature flag into a custom serializer meant to be used by an ObjectIdGenerator via @JsonIdentityInfo(generator=), and setting an attribute on the SerializizerProvider seems to be a way to do that. Initially, I was quite pleased to find that mapper.getSerializerProvider().setAttribute() existed and at first blush it seemed perfect, but shockingly that produces a null pointer exception. This can be seen in both 2.10.2 and in 2.19.0 with this trivial example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonNPEExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializerProvider().setAttribute("foo", "bar");
}
}
I am aware that one can set the attribute via mapper.writer().withAttribute() but that's inconvenient in systems where an ObjectMapper is injected, because then EVERY place that mapper is used, the attribute must be set independently.
I notice that the javadoc on setAttribute is focused on overriding defaults, but it seems like this method should throw a better exception suggesting the correct method, and there should be a setDefaultAttribute() or some other mechanism to allow intialization of a mapper with default attributes (or if it exists it should be mentioned in this javadoc because I couldn't find it ;) ).
Of course I could use a system property, but that's an ugly, needlessly global solution that will cross talk across all instantiated mappers in the JVM.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I recently found myself trying to communicate what amounts to a feature flag into a custom serializer meant to be used by an ObjectIdGenerator via
@JsonIdentityInfo(generator=)
, and setting an attribute on the SerializizerProvider seems to be a way to do that. Initially, I was quite pleased to find that mapper.getSerializerProvider().setAttribute() existed and at first blush it seemed perfect, but shockingly that produces a null pointer exception. This can be seen in both 2.10.2 and in 2.19.0 with this trivial example:I am aware that one can set the attribute via mapper.writer().withAttribute() but that's inconvenient in systems where an ObjectMapper is injected, because then EVERY place that mapper is used, the attribute must be set independently.
I notice that the javadoc on setAttribute is focused on overriding defaults, but it seems like this method should throw a better exception suggesting the correct method, and there should be a setDefaultAttribute() or some other mechanism to allow intialization of a mapper with default attributes (or if it exists it should be mentioned in this javadoc because I couldn't find it ;) ).
Of course I could use a system property, but that's an ugly, needlessly global solution that will cross talk across all instantiated mappers in the JVM.
Beta Was this translation helpful? Give feedback.
All reactions