Java Jackson – Json data binding filters

Also see json simple org.json

Jackson provides an effective an efficient way to bind json to POJOs. However, at times, certain properties may need to be ignored while converting a json to java ojbect and a java object to json string. Jackson provides three ways to filter properties.

  1. @JsonIgnoreProperties– This annotation can be used at the type level to ignore json properties. In the example below we ignore the ‘tags’ property from the albums dataset.
  2. @JsonIgnore – This annotation can be set at property level to ignore certain properties.
  3. Using Custom filters

The example below shows method 1 and 2. Also note the use of the @JsonAutoDetect annotation.

Data Binding Filters 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 DataBindingFilter {
	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);
		AlbumsFilter albums = mapper.readValue(new URL(url), AlbumsFilter.class);
		System.out.println(albums.getTotal_pages());
		System.out.println(albums.getTitle());
		for (DatasetFilter dataset : albums.getDatasetFilter()) {
			System.out.println(dataset.getAlbum_comments());
			System.out.println(dataset.get("album_images"));
			System.out.println(dataset.get("tags"));
			System.out.println(dataset.get("album_listens"));
			break;
		}
	}

}

The AlbumsFilter class

package com.studytrails.json.jackson;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonProperty;

// Do not use fields to autodetect. use the public getter methods to autodetect properties
@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.PUBLIC_ONLY)
public class AlbumsFilter {

	private String title;
	private DatasetFilter[] datasetFilter;
	public String total_pages;

	protected String getTotal_pages() {
		return total_pages;
	}

	public String getTitle() {
		return title;
	}

	// this getter method is for the 'dataset' property
	@JsonProperty("dataset")
	public DatasetFilter[] getDatasetFilter() {
		return datasetFilter;
	}
}

DatasetFilter 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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

// ignore the property with name 'tags'.
@JsonIgnoreProperties({ "tags" })
public class DatasetFilter {
	private String album_id;
	private String album_title;
	private Map<String , Object> otherProperties = new HashMap<String , Object>();
	private String album_comments;

	@JsonCreator
	public DatasetFilter(@JsonProperty("album_id") String album_id, @JsonProperty("album_title") String album_title) {
		this.album_id = album_id;
		this.album_title = album_title;
	}

	// ignore the property specified by this getter.
	@JsonIgnore
	public String getAlbum_comments() {
		return album_comments;
	}

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

	// this method is used to get all properties not specified earlier.
	@JsonAnyGetter
	public Map<String , Object> any() {
		return otherProperties;
	}

	@JsonAnySetter
	public void set(String name, Object value) {
		otherProperties.put(name, value);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *