[英]Jackson serialize property with dynamically different names using mixins
I use different NoSQL
databases and depending on the database I need to name the "id" different.我使用不同的
NoSQL
数据库,并且根据数据库我需要将“id”命名为不同的。 So for example in OrientDB
the id is named "@rid"因此,例如在
OrientDB
,id 被命名为“@rid”
@JsonProperty("@rid")
private String id;
And for MongoDB the id is named "_id"对于 MongoDB,id 被命名为“_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" ^^.我不知道现代数据库开发人员有什么问题,而不仅仅是将 id 字段命名为“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"?
如何在某些情况下将 id 字段动态序列化/反序列化为“@rid”,而在另一种情况下为“_id”?
EDIT:编辑:
Based on rmullers suggestion I have tried to use mixins.根据 rmullers 的建议,我尝试使用 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.其中 IdMixins 是一个完全空的接口,只是用来获得更多控制哪些接口可以传递给映射器。
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":但是当我运行这个简单的测试时,输出仍然是“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"} {"@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 ?您是否尝试过使用http://wiki.fasterxml.com/JacksonMixInAnnotations ? Then you can use an
OrientDbMixin
and a MongoDbMixin
with different @JsonProperty
configuration.然后你可以使用一个
OrientDbMixin
和MongoDbMixin
不同@JsonProperty
配置。
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.