问题描述
我有一个自定义JsonSerializer
,并且在serialize()
方法中,我想使用已注册ObjectMapper
的默认序列化器行为:
I have a custom JsonSerializer
and in the serialize()
method I want to use the default serializer behavior of a registered ObjectMapper
:
class Serializer extends JsonSerializer<MyEntity> {
private ObjectMapper myObjectMapper;
@Override
public void serialize(MyEntity value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// after processing some logic
myObjectMapper.getSerializerProviderInstance().defaultSerializeValue(value, gen);
}
}
上面的代码可以正常工作.现在我想做类似的事情,但是对于UnwrappingSerializer
,我的意思是我想将unwrappingSerializer委派给已注册的ObjectMapper
,例如:
The code above works fine. now I want to do a similar thing but for UnwrappingSerializer
, I mean I want to delegate the unwrappingSerializer to the registered ObjectMapper
something like this:
class UnwrappingSerializer extends JsonSerializer<MyEntity> {
private ObjectMapper myObjectMapper;
@Override
public boolean isUnwrappingSerializer() {
return true;
}
@Override
public void serialize(MyEntity value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// after processing some logic
myObjectMapper.getDefaultUnwrappingSerializer().serialize(value); // how to do such a thing???
}
}
我不确定杰克逊是否支持这种事情.我知道另一种选择是扩展UnwrappingBeanSerializer
,这也很好,对我而言,唯一重要的事情是我想将展开的序列化委托给ObjectMapper,而我不想手动进行.有什么办法吗?
I'm not sure if Jackson supports such a thing or not. I know another option would be extending UnwrappingBeanSerializer
, that's also fine, the only important thing for me is that I want to delegate the unwrapping serialization to an ObjectMapper and I don't want to do it manually. is there any way to do this?
推荐答案
您始终可以创建ObjectMapper的新实例,并在序列化程序中使用它.
You can always create a new instance of ObjectMapper and use it within your serializer.
这篇关于使用Jackson的ObjectMapper的默认展开序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!