java - Jackson serialize property with dynamically different names using mixins -


i use different nosql databases , depending on database need name "id" different. example in orientdb id named "@rid"

@jsonproperty("@rid") private string id; 

and mongodb id named "_id"

@jsonproperty("@_id") private string id; 

i not know wrong modern db developers not naming id field "id" ^^. have problem. how can dynamically serialize/deserialize id field in case "@rid" , in case "_id"?

edit:

based on rmullers suggestion have tried use mixins. have 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 completly empty interface used more controll interfaces can passet mapper.

then there 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 run simple test, output 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 tried using http://wiki.fasterxml.com/jacksonmixinannotations? can use orientdbmixin , mongodbmixin 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));     }  } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -