Jackson使用mixins以动态不同的名称序列化属性 [英] Jackson serialize property with dynamically different names using mixins

查看:890
本文介绍了Jackson使用mixins以动态不同的名称序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用不同的 NoSQL 数据库,根据数据库,我需要将id命名为不同。例如,在 OrientDB 中,id被命名为@rid

  @JsonProperty(@ rid)
private String id;

对于MongoDB,id名为_id

  @JsonProperty(@_ id)
private String id;

我不知道现代数据库开发人员有什么问题,不只是命名id字段id ^^。但现在我有一个问题。如何在某些情况下动态序列化/反序列化id字段为@rid,在另一种情况下为_id?



编辑:



根据rmullers建议我尝试使用mixins。所以我有例如:

  public interface IdMixins {
}

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

公共接口OrientIdMixIn扩展IdMixins {
@JsonProperty(@ rid)String getId();
@JsonProperty(@ rid)void setId(String id);
}

其中IdMixins是一个完全空的接口,用于获得更多控制,哪些接口可以可以映射到映射器。



然后有一个类:

  @JsonTypeInfo(使用= JsonTypeInfo.Id.CLASS,包括= JsonTypeInfo.As.PROPERTY,财产= @ javaClass)
公共抽象类AbstractBean实现Serializable {
私有静态最后的serialVersionUID长= -1286900676713424199L ;

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

public String getId(){
return id;
}

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

但是当我运行这个简单的测试时,输出是仍然是id:

  public class MixinTest {
public static void main(String [] args)抛出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));

}

公共静态类Foo扩展AbstractBean {
private String bar;

public String getBar(){
return bar;
}

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

输出:



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


'的div类= h2_lin>解决方案

你尝试过使用 HTTP://维基.fasterxml.com / JacksonMixInAnnotations ?然后可以使用一个 OrientDbMixin MongoDbMixin 具有不同 @JsonProperty 配置。



更新:工作示例

 公共最终课程JacksonTest {

静态最终类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;
}

}

公共接口MongoIdMixIn {

@JsonProperty(_ id)String getId();

}

公共接口OrientIdMixIn {

@JsonProperty(@ rid)String getId();

}

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

public static void main(String [] args)抛出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));
}

}


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"}

解决方案

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));
    }

}

这篇关于Jackson使用mixins以动态不同的名称序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆