简体   繁体   English

如何仅序列化使用自定义注释注释的属性

[英]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.

相关问题 杰克逊:如何仅序列化带注释的属性 - Jackson: how to serialize only annotated properties Jackson 和 Kotlin:如何仅序列化带注释的属性 - Jackson with Kotlin: how to serialize only annotated properties 如何创建仅包含用自定义注释注释的类/方法的JavaDoc? - How to create JavaDoc which contains only classes/method annotated with custom Annotation? 如何构建用我的自定义注释注释的类列表? - How to build a list of classes annotated with my custom annotation? 如何使用自定义注释来验证带有Spring @Value字段的注释? - How to validate annotated with Spring @Value field using custom annotation? 如何只允许在 Java 中的方法参数中使用某些注释进行注释的类? - How to allow only classes annotated with some annotation in method parameters in Java? 使用自定义注释注释的bean的自定义初始化 - Custom initializing of beans annotated with a custom annotation 如何排除对使用特定注释注释但未使用其他注释注释的bean的扫描? - How to exclude scanning of beans that annotated with specific annotation and not annotated with another annotation? 我们可以只加载那些用自定义注释注释过的类吗? - Can we load only those classes which we have annotated with our custom annotation? 如何获取使用特定注释注释的对象的所有字段和属性? - How do I get all fields and properties of an object that are annotated with specific annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM