I have a class, say Person, that I want to populate from JSON using Jackson, but the property names vary depending on the source. Here's what the code currently looks like:
class Person {
protected String firstName;
protected String lastName;
protected String address;
public abstract void setFirstName(String firstName);
public abstract void setLastName(String lastName);
public abstract void setAddress(String address);
}
class PersonFormat1 extends Person {
@Override
@JsonProperty("firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
@JsonProperty("lastName")
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override("address")
public void setAddress(String address) {
this.address = address;
}
}
class PersonFormat2 extends Person {
@Override
@JsonProperty("fName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
@JsonProperty("lName")
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override("addr")
public void setAddress(String address) {
this.address = address;
}
}
As you can see, PersonFormat1they are PersonFormat2identical in structure, but I need different subclasses to indicate different property names.
Is there a way to force the use of a model without a pattern of the need to reuse and override each method?
source
share