让我们假设我有以下POJO:
class Pojo {
String s;
Object o;
Map<String, String> m;
}
在运行时,我想要除了一个属性之外的所有属性的默认序列化/反序列化。通常,我希望在序列化时用数据库中的ID替换字段,类似于this other question。
例如,我想将o
替换为从外部映射获取的字符串(例如:object1
&lt; =&gt;“123”和object2
&lt; =&gt; ;“456”):
o
并替换(如果o
为object1
,则序列化为字符串“123”)o
的原始值(即object1
),使用Pojo
重新创建o = object1
对象。< / LI>
我理解模块是一种方法,但是我不确定如何使用它们同时为不需要更改的属性保留自动BeanSerializer / Deserializer。
有人可以提供一个例子(甚至做作)或另类方法吗?
注意:
修改
为了更清楚一点,我可以使用Mixins进行以下操作:
ObjectMapper mapper = new ObjectMapper(MongoBsonFactory.createFactory());
mapper.addMixInAnnotations(Pojo.class, PojoMixIn.class);
ObjectReader reader = mapper.reader(Pojo.class);
DBEncoder dbEncoder = DefaultDBEncoder.FACTORY.create();
OutputBuffer buffer = new BasicOutputBuffer();
dbEncoder.writeObject(buffer, o);
使用以下Mixin:
abstract class PojoMixIn {
@JsonIgnore Object o;
}
然后将所需的字符串添加到JSON内容中。但是我需要在编译时知道需要替换o
字段,我不知道。
答案 0 :(得分:17)
我认为@JsonSerialize
和@JsonDeserialize
就是您所需要的。这些注释使您可以控制特定字段的序列化/反序列化。 This question显示了将它们组合成一个注释的优雅方式。
UPD。对于这种复杂的情况,您可以查看BeanSerializerModifier/BeanDeserializerModifier
类。我们的想法是使用您的特定字段的自定义逻辑修改常规BeanSerializer/BeanDeserializer
,并让基本实现执行其他操作。稍后会发布一个例子。
UPD2。正如我所见,其中一种方法是使用changeProperties
方法并指定自己的序列化程序。
UPD3。更新了自定义序列化程序的工作示例。反序列化可以用类似的方式完成。
UPD4。更新了完整自定义序列化/反序列化的示例。 (我使用过 jakson-mapper-asl-1.9.8 )
public class TestBeanSerializationModifiers {
static final String PropertyName = "customProperty";
static final String CustomValue = "customValue";
static final String BaseValue = "baseValue";
// Custom serialization
static class CustomSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
String customValue = CustomValue; // someService.getCustomValue(value);
jgen.writeString(customValue);
}
}
static class MyBeanSerializerModifier extends BeanSerializerModifier {
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
for (int i = 0; i < beanProperties.size(); i++) {
BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
if (PropertyName.equals(beanPropertyWriter.getName())) {
beanProperties.set(i, beanPropertyWriter.withSerializer(new CustomSerializer()));
}
}
return beanProperties;
}
}
// Custom deserialization
static class CustomDeserializer extends JsonDeserializer<Object> {
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// serialized value, 'customValue'
String serializedValue = jp.getText();
String baseValue = BaseValue; // someService.restoreOldValue(serializedValue);
return baseValue;
}
}
static class MyBeanDeserializerModifier extends BeanDeserializerModifier {
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) {
Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
while (beanPropertyIterator.hasNext()) {
SettableBeanProperty settableBeanProperty = beanPropertyIterator.next();
if (PropertyName.equals(settableBeanProperty.getName())) {
SettableBeanProperty newSettableBeanProperty = settableBeanProperty.withValueDeserializer(new CustomDeserializer());
builder.addOrReplaceProperty(newSettableBeanProperty, true);
break;
}
}
return builder;
}
}
static class Model {
private String customProperty = BaseValue;
private String[] someArray = new String[]{"one", "two"};
public String getCustomProperty() {
return customProperty;
}
public void setCustomProperty(String customProperty) {
this.customProperty = customProperty;
}
public String[] getSomeArray() {
return someArray;
}
public void setSomeArray(String[] someArray) {
this.someArray = someArray;
}
}
public static void main(String[] args) {
SerializerFactory serializerFactory = BeanSerializerFactory
.instance
.withSerializerModifier(new MyBeanSerializerModifier());
DeserializerFactory deserializerFactory = BeanDeserializerFactory
.instance
.withDeserializerModifier(new MyBeanDeserializerModifier());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializerFactory(serializerFactory);
objectMapper.setDeserializerProvider(new StdDeserializerProvider(deserializerFactory));
try {
final String fileName = "test-serialization.json";
// Store, "customValue" -> json
objectMapper.writeValue(new File(fileName), new Model());
// Restore, "baseValue" -> model
Model model = objectMapper.readValue(new File(fileName), Model.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}