The json string at times have a lot of properties. It seems a waste creating a POJO with all those properties. Wouldnt it be great if there was a catch’all that could read all properties in a map? Jackson provides annotations to do just that. In the example below we have set two properties in the bean and the other properties are read into a map. These example also introduces some common annotations using in Jackson. Lets look at them briefly:
package com.studytrails.json.jackson;
public class AlbumsDynamic {
private String title;
private DatasetDynamic[] dataset;
public void setTitle(String title) {
this.title = title;
}
public void setDataset(DatasetDynamic[] dataset) {
this.dataset = dataset;
}
public String getTitle() {
return title;
}
public DatasetDynamic[] getDataset() {
return dataset;
}
}
package com.studytrails.json.jackson;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class DatasetDynamic {
private String album_id;
private String album_title;
private Map<String , Object> otherProperties = new HashMap<String , Object>();
@JsonCreator
public DatasetDynamic(@JsonProperty("album_id") String album_id, @JsonProperty("album_title") String album_title) {
this.album_id = album_id;
this.album_title = album_title;
}
public String getAlbum_id() {
return album_id;
}
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public String getAlbum_title() {
return album_title;
}
public void setAlbum_title(String album_title) {
this.album_title = album_title;
}
public Object get(String name) {
return otherProperties.get(name);
}
@JsonAnyGetter
public Map<String , Object> any() {
return otherProperties;
}
@JsonAnySetter
public void set(String name, Object value) {
otherProperties.put(name, value);
}
}
(Note: This article’s original links is here )