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
Change serializer applied to a single property of an class #4385
Comments
I am not sure why explicit override on property that you showed is insufficient:
since that is the specific mechanism for overriding things on per-property basis. So I feel I am missing something here. |
Rereading my question the following morning, you're right. My question was unclear and lacks important context, Sorry about that. While Mixins would generally be an acceptable solution to this issue, in my case I'm not sure it is appropriate. I want to apply custom external configuration to the beanProperties.set(i, new MyCustomBeanPropertyWriter(beanPropertyWriter, new MyCustomPropertySerializer())); I am actually using the constructor class MyCustomPropertySerializer extends JsonSerializer<T> {
private boolean serializeFirstProperty = true;
// default no-arg constructor
public MyCustomPropertySerializer() {
super();
}
// constructor to instantiate with my configuration
public MyCustomPropertySerializer(boolean serializeFirstProperty) {
this();
this.serializeFirstProperty = serializeFirstProperty;
}
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider serializers) {
if (serializeFirstProperty)
{
gen.writePOJOField("prop1", value);
}
// other serialization omitted
}
} So I may construct different JsonMappers to serialize I hope this adds enough context and my question makes more sense now. Thanks. |
Ok. Yes, that makes more sense; thank you for the explanation. As things are, most similar configurability (dynamic, non-annotation based) is attached to types (classes); specifically "config overrides" allow changing various aspects (like serialization inclusion) but are attached to types. In the meantime, I do think use of |
Thanks for your thoughts. I'll continue using the |
Is your feature request related to a problem? Please describe.
The current means (or at least the means that I used) of overriding the serializer on a single property of a class is convoluted.
Describe the solution you'd like
I would like a means of setting a custom serializer for a single property of a class where my custom serializer overrides any serializer applied to that property. Please delete this if it's a duplicate issue or if there is some way to do this that I missed.
Usage of a mixin for the class containing the property is inappropriate because I don't want to override the serialization of the class in any other way, just the property itself. Usage of
SimpleModule.AddSerializer()
for the property type is inappropriate because I don't want to override serialization of other properties of the same type.Additionally I may only want to override the "regular" serializer and not the nullSerializer for the property.
I did find a way to accomplish this which I have provided an example of below, but it seems an easier way to accomplish this would be to remove the condition that blocks overriding the set serializer in a BeanPropertyWriter using the
BeanPropertyWriter.assignSerializer(JsonSerializer<Object> ser)
method, though I assume this is not that simple and that that condition is set for a reason.Usage example
This is my workaround for the issue
Additional context
Using version 2.15.3 of jackson-databind
The text was updated successfully, but these errors were encountered: