0

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?

2 Answers2

2

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:

public final class JSONChangeSet {
  private final String comment;
  private final Resource owner;

  @JsonCreator
  public JSONChangeSet(
    @JsonProperty("comment") final Resource owner,
    @JsonProperty("comment") final String comment
  ) {
    this.comment = comment;
    this.owner = owner;
  }

  public String getComment() {
    return comment;
  }
}

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.

Community
  • 1
  • 1
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • Just a design/good practice consideration: If you put a method call in your constructor. Be sure that it's final method. – Alejandro Diaz Dec 09 '11 at 13:07
  • Hmm I just read through the Jackson documentation a little bit, but it seems to me that Jackson can´t parse through a tree of JSON objects if some fields are URI´s that have to be parsed too. – RoflcoptrException Dec 09 '11 at 13:15
  • @KARASZIIstván I know. But, since the question implies to call a method after the deseralization process, I understand that you need to hook the call within the constructor in order to solve the problem. Or I'm missing something :-) – Alejandro Diaz Dec 09 '11 at 14:30
  • @Roflcoptr that's not true. I think Jackson can parse everything which can be written to a file :) – KARASZI István Dec 09 '11 at 17:55
1

I think Gson does not has a "listener" for that. You can try the following trick:

static class JSONChangeSet {

    @SerializedName("comment")
    private String comment;

    @SerializedName("owner")
    private int owner;

}

static class JSONChangeSetDeserializer implements JsonDeserializer<JSONChangeSet> {
    Gson gson = new Gson();

    @Override
    public JSONChangeSet deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {
        final JSONChangeSet obj = gson.fromJson(json, typeOfT);

        // Code you want to run
        System.out.println("Owner: " + obj.owner);

        return obj;
    }
}

public static void main(final String[] args) throws Exception, JsonMappingException, IOException {

    final GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(JSONChangeSet.class, new JSONChangeSetDeserializer());

    gson.create().fromJson("{\"comment\": \"it works!\", \"owner\": 23}", JSONChangeSet.class);

}
Alejandro Diaz
  • 431
  • 2
  • 9