java - Jackson JSON processor problems -
i have been scratching head on hours :
jsckson deserializes b
bombs on c
below :
b
, c
both subclasses of a
, , has setter getname
. note uppercase n
in name
intentional, how json looks. deserializing c
complains unrecognized field name name
, b
ok.
version 1.7.2
objectmapper mapper = new objectmapper(); mapper.getdeserializationconfig().addmixinannotations(b.class, mixin.class); string json = "{\"name\" : \"13\"}"; b b = m.readvalue(json, b.class); system.out.println(b.getname()); c c = m.readvalue(json, c.class); system.out.println(c.getname());
public class { private int id ; private string name; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
public class b extends { private string address; public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } }
public class c extends { private string country; public string getcountry() { return country; } public void setcountry(string country) { this.country = country; } }
@jsonautodetect(fieldvisibility = visibility.none,settervisibility=visibility.any) abstract class mixin { @jsonproperty("name") public abstract void setname(string name); @jsonproperty("id") public abstract void setid(int id); }
this because default json field name match setter called setname
name
, not name
. java property name convention.
the deserialization works b
because you've introduced mixin alters field name matches against setname
.
can't introduce mixin c
also?
Comments
Post a Comment