简体   繁体   中英

how to only serialize properties annotated with a custom annotation

We use jackson throughout our application to serialize and deserialize Java objects to 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?

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?

We can't use standard jackson annotations (@JsonIgnore) in this use case because it would break our other serialization uses cases in the application.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM