I have a class, let's say Person
, that I want to populate from JSON using Jackson, but the property names vary by source. Here's how the code looks currently:
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);
// getters etc.
}
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, PersonFormat1
and PersonFormat2
are identical in structure, but I need different subclasses in order to specify different property names.
Is there some way to enforce the model without the boilerplate of having to redeclare and reimplement each method?
See Question&Answers more detail:os