I have the following POJO class for a JSON object:
public class JSONChangeSet {
public JSONChangeSet {
System.out.println("Owner: " + owner);
}
@SerializedName("comment")
private String comment;
@SerializedName("lastUpdatedDate")
private String modifiedDate;
@SerializedName("owner")
private Resource owner;
@SerializedName("modifiedBy")
private Resource modifier;
public String getComment() {
return comment;
}
}
Obviously this doesnt work, because the field owner has not yet a value assigned when the constructor is called. Is there any possibility to call a method automatically after the JSON object is parsed?
You tagged your question with gson, but I would recommend you the Jackson library instead, because I saw your last two questions, and seems like gson is not flexible enough for you.
In Jackson your example would look like this:
With this solution you can have immutable objects, which will built by the constructor. It’s also good for the DI pattern. And BTW Jackson is lightning fast.
You may want to read this question also.