Java Jackson – Json annotations and dyna beans

Also see json simple org.json

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:

  1. @JsonProperty-This annotation is used to mark a method as a getter or setter for a property. In other words, it associates a json field with a java property. If a name is specified (@JsonProperty(“age”)) then the java property that is annotated with this annotation is mapped to the ‘age’ field of the json, If no name is specified the java property name is used.
  2. @JsonCreator-This annotation is used the define constructors that are used to create java objects from json string. It is used during data binding and specifies properties that will be used to create java objects during deserialization.
  3. @JsonAnyGetter and @JsonAnySetter – This annotations are used to mark methods that set or read fields that are not handled by any other java property. These act like catch-all and handle all fields that are not handled by any other java property. The fields are stored in a map as key value pairs.

Data Binding Example

Databinding

package com.studytrails.json.jackson;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DataBindingDynaBean {
	public static void main(String[] args) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
		String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=2";
		ObjectMapper mapper = new ObjectMapper();
		mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		AlbumsDynamic albums = mapper.readValue(new URL(url), AlbumsDynamic.class);
		DatasetDynamic[] datasets = albums.getDataset();
		for (DatasetDynamic dataset : datasets) {
			System.out.println(dataset.get("album_type"));
			
		}
	}

	
}

The Albums class

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;
	}
}

Dataset class

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);
	}
}

Leave a Comment