Immutable JsonMapper #229
Replies: 3 comments 5 replies
-
My current "best-effort" approach is to create a subclass of JsonMapper (as a copy-constructor) and override all mutating methods to throw the exception. The caveat is that any new mutating method may come in a later version that this class would miss. public class ImmutableJsonMapper extends JsonMapper {
ImmutableJsonMapper(@Nonnull final JsonMapper original) {
super(original);
}
@Override
public ObjectMapper addHandler(DeserializationProblemHandler h) {
throw new UnsupportedOperationException(errorMessage);
}
@Override
public ObjectMapper addMixIn(Class<?> target, Class<?> mixinSource) {
throw new UnsupportedOperationException(errorMessage);
}
@Override
public ObjectMapper clearProblemHandlers() {
throw new UnsupportedOperationException(errorMessage);
}
@Override
public ObjectMapper configure(DatatypeFeature f, boolean state) {
throw new UnsupportedOperationException(errorMessage);
}
@Override
public ObjectMapper configure(DeserializationFeature f, boolean state) {
throw new UnsupportedOperationException(errorMessage);
}
@Override
public ObjectMapper configure(JsonParser.Feature f, boolean state) {
throw new UnsupportedOperationException(errorMessage);
}
// ... same overriding for all other mutating methods; {enable,disable,set,...}(XXXFeature ...)
} |
Beta Was this translation helpful? Give feedback.
-
I think the plan is that Jackson 3 would remove some of these methods and that users would more or less be required to use the builder. |
Beta Was this translation helpful? Give feedback.
-
I agree with @pjfanning Jackson 3.0 (in So I am -1 on trying to do something like this for Jackson 2.x. |
Beta Was this translation helpful? Give feedback.
-
What about supporting an immutability option for the JsonMapper as a builder property? An immutable JsonMapper would throw an exception (e.g., java.lang.UnsupportedOperationException) if any mutating method is called (e.g., {configure, enable, disable}(XXXFeature ...)).
This feature would be helpful in use cases where one wants to share a pre-configured mapper instance in many places and prevent any accidental mutation efforts (custom further configurations).
Beta Was this translation helpful? Give feedback.
All reactions