[英]how to only serialize properties annotated with a custom annotation
We use jackson throughout our application to serialize and deserialize Java objects to JSON.我们在整个应用程序中使用 jackson 将 Java 对象序列化和反序列化为 JSON。 It works great.
它工作得很好。
Is it possible, perhaps through a custom serializer, to serialize only properties of a Java object that are Annotated with a custom annotation?是否可以通过自定义序列化程序仅序列化使用自定义注释注释的 Java 对象的属性?
So, given the custom annotation:因此,给定自定义注释:
public @interface SpecialField {}
And the following bean和下面的豆
public SomeBean {
@SpecialField
private Object propertyIncluded;
private Object propertyExcluded;
}
What would a custom serializer (or some equivalent mechanism) look like to serialize propertyIncluded (using the normal jackson object mapper) and ignore propertyExcluded?自定义序列化程序(或某些等效机制)将如何序列化 propertyIncluded(使用普通的杰克逊对象映射器)并忽略 propertyExcluded?
We can't use standard jackson annotations (@JsonIgnore) in this use case because it would break our other serialization uses cases in the application.我们不能在这个用例中使用标准的杰克逊注解(@JsonIgnore),因为它会破坏我们在应用程序中的其他序列化用例。
While this might not be quite what your looking for, It is possible to make the jackson engine serialize objects differently via some tweaking.虽然这可能不是您想要的,但可以通过一些调整使杰克逊引擎以不同的方式序列化对象。 In my example below I create two types of serializers which will or wont serialize a field marked as transient.
在下面的示例中,我创建了两种类型的序列化程序,它们将或不会序列化标记为瞬态的字段。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.map.ObjectMapper;
public class Test {
public static void main(String[] args) throws Exception {
ISerializer d = new Doesnt();
ISerializer o = new Observes();
SomeObject obj = new SomeObject();
System.out.println("Doesnt: " + d.serialize(obj));
System.out.println("Observes: " + o.serialize(obj));
}
public static class Doesnt implements ISerializer<SomeObject> {
@Override
public String serialize(SomeObject o) throws Exception {
ObjectMapper om = new ObjectMapper();
om.setVisibilityChecker(
om.getSerializationConfig().
getDefaultVisibilityChecker().
withFieldVisibility(JsonAutoDetect.Visibility.ANY).
withGetterVisibility(JsonAutoDetect.Visibility.ANY));
return om.writeValueAsString(o);
}
}
public static class Observes implements ISerializer<SomeObject> {
@Override
public String serialize(SomeObject o) throws Exception {
ObjectMapper om = new ObjectMapper();
om.setVisibilityChecker(
om.getSerializationConfig().
getDefaultVisibilityChecker().
withFieldVisibility(JsonAutoDetect.Visibility.ANY).
withGetterVisibility(JsonAutoDetect.Visibility.NONE));
return om.writeValueAsString(o);
}
}
public interface ISerializer<T> {
public String serialize(T o) throws Exception;
}
public static class SomeObject implements Serializable {
private static final long serialVersionUID = 745063791749142843L;
private transient String myVar = "Transient";
private String myOther = "Not Transient";
public String getMyVar() {
return myVar;
}
public void setMyVar(String myVar) {
this.myVar = myVar;
}
public String getMyOther() {
return myOther;
}
public void setMyOther(String myOther) {
this.myOther = myOther;
}
}
}
output:输出:
Doesnt: {"myVar":"Transient","myOther":"Not Transient"}
Observes: {"myOther":"Not Transient"}
I would think it would be fairly easy to change serializers to extend the JsonSerializer class, and do something similar in them.我认为更改序列化程序以扩展 JsonSerializer 类并在其中做类似的事情会相当容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.