Java Google Json (Gson) Type Adapter – Studytrails (original) (raw)

In the earlier tutorials we have seen how gson can serialize and deserialize java classes with or without hierarchies. By default, it introspects the classes and comes with with a strategy for serializing and deserializing it. However, in some cases, you want to specify your own conversion strategy. That is, you want to control how the java object is converted to json string and the other way round. Gson provides a capability to specify a custom type adapter. You tell Gson that for a particular class, use the conversion strategy specified by your custom adapter. Lets look at how to write the type adapter :
To write a custom adapter extend the com.google.gson.TypeAdapter abstract class. Implement the public abstract T read(JsonReader in) throws IOException; and public abstract void write(JsonWriter out, T value) throws IOException; methods. The adapter should also handle nulls. Create the Type adapter
instance and then register it with the GsonBuilder. Create the Gson object from the GsonBuilder and then use that to serialize and deserialize. Lets look at an example

package com.studytrails.json.gson;

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

import org.apache.commons.io.IOUtils;

import com.google.gson.Gson; import com.google.gson.GsonBuilder;

public class DatasetTypeAdapterExample8 {

public static void main(String[] args) throws MalformedURLException, IOException {
    String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5";
    String json = IOUtils.toString(new URL(url));
    // Create the custom type adapter and register it with the GsonBuilder
    // class.
    Gson gson = new GsonBuilder().registerTypeAdapter(Dataset.class, new DatasetTypeAdapter()).create();
    // deserialize the json to Albums class. The Dataset objects are part of
    // the Albums class. Whenever Gson encounters an object of type DataSet
    // it calls the DatasetTypeAdapter to read and write json.
    Albums albums = gson.fromJson(json, Albums.class);
    System.out.println(albums.getDataset()[1].getAlbum_title());
    // prints
    // http://freemusicarchive.org/music/The_Yes_Sirs/Through_The_Cracks_Mix_Vol_1/
}

}

The Adapter

package com.studytrails.json.gson;

import java.io.IOException;

import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter;

/**

}

The Albums class

package com.studytrails.json.gson;

public class Albums {

private String title;
private Dataset[] dataset;

public void setTitle(String title) {
    this.title = title;
}

public void setDataset(Dataset[] dataset) {
    this.dataset = dataset;
}

public String getTitle() {
    return title;
}

public Dataset[] getDataset() {
    return dataset;
}

}

The Dataset class

package com.studytrails.json.gson;

import java.util.HashMap; import java.util.Map;

public class Dataset { private String album_id; private String album_title; private Map<String , Object> otherProperties = new HashMap<String , Object>();

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

}