FasterXML / FasterXML/jackson-databind

Allow changing serializer used for a single property of a Class

Open
#4,385 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### 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 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
```java
// other json annotations here for this class
class MyClass {
// this property should always be serialized using MyPropertySerializer
@JsonSerialize(using = MyPropertySerializer.class)
T myPropertyMySerialization = new MyProperty();

// this property should be serialized using MyPropertySerializer when I don't want to override that behaviour below
// BUT null values should be serialized with my NullSerializer
@JsonSerialize(using = MyPropertySerializer.class, nullsUsing = NullSerializer.class)
T myPropertyCustomSerialization = new MyProperty();
}

// elsewhere in the code
ObjectMapper myMethod() {
SimpleModule module = new SimpleModule();
module.setSerializerModifier(new BeanSerializerModifier() {

@Override
public List changeProperties(SerializationConfig config, BeanDescription beanDesc, List beanProperties) {
// modify serialization of MyClass
if (MyClass.class.isAssignableFrom(beanDesc.getBeanClass())) {
for (int i = 0; i < beanProperties.size(); i++) {
BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
// modify serialization of myPropertyCustomSerialization
if ("myPropertyCustomSerialization".equals(beanPropertyWriter.getName()) {
beanProperties.set(i, new MyCustomBeanPropertyWriter(beanPropertyWriter, new MyCustomPropertySerializer()));
}
}
return beanProperties;
}
return super.changeProperties(config, beanDesc, beanProperties);
}

class MyCustomBeanPropertyWriter extends BeanPropertyWriter {
private MyCustomBeanPropertyWriter(BeanPropertyWriter base, JsonSerializer newSerializer) {
super(base);
this._serializer = newSerializer;
}
}
});
JsonMapper.Builder builder = JsonMapper.builder();
builder.addModule(module);
return builder.build();
}
```

### Additional context

Using version 2.15.3 of jackson-databind

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.