Jackson serialize property with dynamically different names using mixins

橙三吉。 提交于 2019-12-02 19:14:47

问题


I use different NoSQL databases and depending on the database I need to name the "id" different. So for example in OrientDB the id is named "@rid"

@JsonProperty("@rid")
private String id;

And for MongoDB the id is named "_id"

@JsonProperty("@_id")
private String id;

I do not know what is wrong with the modern DB developers not just naming the id field "id" ^^. But now I have a problem. How can I dynamically serialize/deserialize the id field in some case as "@rid" and in another case as "_id"?

EDIT:

Based on rmullers suggestion I have tried to use mixins. So I have for example:

public interface IdMixins {
}

public interface MongoIdMixIn extends IdMixins {
    @JsonProperty("_id") String getId();
    @JsonProperty("_id") void setId(String id);
}

public interface OrientIdMixIn extends IdMixins{
    @JsonProperty("@rid") String getId();
    @JsonProperty("@rid") void setId(String id);
}

Where IdMixins is a completly empty interface just used to get more controll which interfaces can be passet to the mapper.

Then there is a class:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@javaClass")
public abstract class AbstractBean implements Serializable {
    private static final long serialVersionUID = -1286900676713424199L;

    // @JsonProperty("@rid")
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

But when I run this simple test, the output is still "id":

public class MixinTest {
    public static void main(String[] args) throws JsonProcessingException {
        Foo f = new Foo();
        f.setId("123");
        f.setBar("lala");

        ObjectMapper mapper = new ObjectMapper();

        ObjectMapper m2 = mapper.copy();
        m2.addMixInAnnotations(AbstractBean.class, MongoIdMixIn.class);
        System.out.println(m2.writeValueAsString(f));

        ObjectMapper m3 = mapper.copy();
        m3.addMixInAnnotations(AbstractBean.class, OrientIdMixIn.class);
        System.out.println(m3.writeValueAsString(f));

    }

    public static class Foo extends AbstractBean {
        private String bar;

        public String getBar() {
            return bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }
    }
}

Outputs:

{"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"} {"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"}


回答1:


Have you tried using http://wiki.fasterxml.com/JacksonMixInAnnotations? Then you can use an OrientDbMixin and a MongoDbMixin with different @JsonProperty configuration.

Update: Working example

public final class JacksonTest {

    static final class ExampleBean {

        private String id;
        private String bar;

        @JsonProperty("donotwanttoseethis")
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBar() {
            return bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }

    }

    public interface MongoIdMixIn {

        @JsonProperty("_id") String getId();

    }

    public interface OrientIdMixIn {

        @JsonProperty("@rid") String getId();

    }

    private final static Logger LOG = LoggerFactory.getLogger();

    public static void main(String[] args) throws JsonProcessingException {
        ExampleBean bean = new ExampleBean();
        bean.setId("1234");
        bean.setBar("lala");

        ObjectMapper m2 = new ObjectMapper();
        m2.addMixInAnnotations(ExampleBean.class, MongoIdMixIn.class);
        LOG.info(m2.writeValueAsString(bean));

        ObjectMapper m3 = new ObjectMapper();
        m3.addMixInAnnotations(ExampleBean.class, OrientIdMixIn.class);
        LOG.info(m3.writeValueAsString(bean));
    }

}


来源:https://stackoverflow.com/questions/29388694/jackson-serialize-property-with-dynamically-different-names-using-mixins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!