[英]How to access default jackson serialization in a custom serializer
I want to create a custom serializer which does a tiny bit of work and then leaves the rest for default serialization.我想创建一个自定义序列化程序,它只做一点工作,然后将其余部分留给默认序列化。
For example:例如:
@JsonSerialize(using = MyClassSerializer.class)
public class MyClass {
...
}
public class MyClassSerializer extends JsonSerializer<MyClass> {
@Override
public void serialize(MyClass myClass, JsonGenerator generator,
SerializerProvider provider)
throws JsonGenerationException, IOException {
if (myClass.getSomeProperty() == someCalculationResult) {
provider.setAttribute("special", true);
}
generator.writeObject(myClass);
}
}
With the idea of creating other custom serializers for aggregated objects which behave differently based on the 'special' attribute value.基于为聚合对象创建其他自定义序列化程序的想法,这些对象的行为基于“特殊”属性值而有所不同。 However, the above code does not work, as it unsurprisingly goes into an infinite recursion.但是,上面的代码不起作用,因为它毫不奇怪地进入了无限递归。
Is there a way to tell jackson to use default serialization once I have set the attribute?一旦我设置了属性,有没有办法告诉杰克逊使用默认序列化? I don't really want enumerate all the properties like many custom serializers as the class is fairly complex and I don't want to have to do dual maintenance with the serializer every time I change the class.我真的不想像许多自定义序列化程序一样枚举所有属性,因为该类相当复杂,而且我不想每次更改类时都必须对序列化程序进行双重维护。
A BeanSerializerModifier
will provide you access to the default serialization. BeanSerializerModifier
将为您提供对默认序列化的访问。
public class MyClassSerializer extends JsonSerializer<MyClass> {
private final JsonSerializer<Object> defaultSerializer;
public MyClassSerializer(JsonSerializer<Object> defaultSerializer) {
this.defaultSerializer = checkNotNull(defaultSerializer);
}
@Override
public void serialize(MyClass myclass, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (myclass.getSomeProperty() == true) {
provider.setAttribute("special", true);
}
defaultSerializer.serialize(myclass, gen, provider);
}
}
BeanSerializerModifier
for MyClass
为MyClass
创建一个BeanSerializerModifier
public class MyClassSerializerModifier extends BeanSerializerModifier {
@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
if (beanDesc.getBeanClass() == MySpecificClass.class) {
return new MyClassSerializer((JsonSerializer<Object>) serializer);
}
return serializer;
}
}
ObjectMapper om = new ObjectMapper()
.registerModule(new SimpleModule()
.setSerializerModifier(new MyClassSerializerModifier()));
@JsonSerialize(using = MyClassSerializer.class)
public class MyClass {
...
}
public class MyClassSerializer extends JsonSerializer<MyClass> {
@Override
public void serialize(MyClass myClass, JsonGenerator generator,
SerializerProvider provider)
throws JsonGenerationException, IOException {
if (myClass.getSomeProperty() == someCalculationResult) {
provider.setAttribute("special", true);
}
provider.defaultSerializeValue(myClass, generator);
}
}
if you are just writing an object as normal use the above如果您只是像往常一样编写对象,请使用上述内容
You can use @JsonGetter instead of using a custom serializer if that's the only change you want to make.如果这是您想要进行的唯一更改,您可以使用 @JsonGetter 而不是使用自定义序列化程序。
public class MyClass{
@JsonGetter("special")
protected boolean getSpecialForJackson() {
return myClass.getSomeProperty() == someCalculationResult;
}
}
To add to the chosen answer, the serializer implementation may also have to implement ContextualSerializer
and ResolvableSerializer
interfaces.要添加到所选答案中,序列化器实现可能还必须实现ContextualSerializer
和ResolvableSerializer
接口。 Please take a look at a related issue here https://github.com/FasterXML/jackson-dataformat-xml/issues/259请在此处查看相关问题https://github.com/FasterXML/jackson-dataformat-xml/issues/259
public class MyClassSerializer extends JsonSerializer<MyClass>
implements ContextualSerializer, ResolvableSerializer {
private final JsonSerializer<Object> defaultSerializer;
public MyClassSerializer(JsonSerializer<Object> defaultSerializer) {
this.defaultSerializer = checkNotNull(defaultSerializer);
}
@Override
public void serialize(MyClass myclass, JsonGenerator gen, SerializerProvider provider)
throws IOException {
if (myclass.getSomeProperty() == true) {
provider.setAttribute("special", true);
}
defaultSerializer.serialize(myclass, gen, provider);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException {
if (defaultSerializer instanceof ContextualSerializer) {
JsonSerializer<?> contextual = ((ContextualSerializer)defaultSerializer).createContextual(prov, property);
return new MyClassSerializer((JsonSerializer<Object>)contextual);
}
return new MyClassSerializer(defaultSerializer);
}
@Override
public void resolve(SerializerProvider provider) throws JsonMappingException {
if (defaultSerializer instanceof ResolvableSerializer) {
((ResolvableSerializer)defaultSerializer).resolve(provider);
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.