JSON deserialize generic types using Gson and Jackson
07 Sep 2016
|
2 mins read
|
gson
jackson
java
json
This blog will guide you how to deserialize json into java generic types. Suppose we have 2 json as given below.
SocialAccountResponse.json
{
"status": true,
"code": 200,
"data": {
"id": "123456",
"profileUrl": "https://www.facebook.com/123456"
}
}
UserResponse.json
{
"status": true,
"code": 200,
"data": {
"name": "MyBlogs",
"email": "admin@myblogspro.com"
}
}
Both json have common keys, only data object is being change. To deserialize these jsons without generic types we would have to create 2 wrappers with 2 actual data class. But with generic types we only need to create generic class for root keys and actual data class for every object in data key.
GenericResponse.java
public class GenericResponse<T> {
private boolean status;
private int code;
private T data;
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
SocialAccountsResponse.java
public class SocialAccountsResponse {
private String id;
private String profileUrl;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProfileUrl() {
return profileUrl;
}
public void setProfileUrl(String profileUrl) {
this.profileUrl = profileUrl;
}
}
UserResponse.java
public class UserResponse {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Now We’ll deserialize above JSON in Generic class using below libraries
Google Gson
Jackson
1. Deserialize using Google Gson
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonDeserializer {
public static void main(String []args) {
Gson gson = new Gson();
GenericResponse<UserResponse> response = gson.fromJson(jsonString, new TypeToken<GenericResponse<UserResponse>>(){}.getType());
System.out.println(response.getData().getName());
System.out.println(response.getData().getEmail());
GenericResponse<SocialAccountsResponse> socialAccountResponse = gson.fromJson(jsonString, new TypeToken<GenericResponse<SocialAccountsResponse>>(){}.getType());
System.out.println(socialAccountResponse.getData().getId());
System.out.println(socialAccountResponse.getData().getProfileUrl());
}
}
2. Deserialize using Jackson
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDeserializer {
public static void main(String []args) {
ObjectMapper mapper = new ObjectMapper();
GenericResponse<UserResponse> userResponse = mapper.readValue(jsonString, new TypeReference<GenericResponse<UserResponse>>() {});
System.out.println(userResponse.getData().getName());
System.out.println(userResponse.getData().getEmail());
GenericResponse<SocialAccountsResponse> socialAccountResponse = mapper.readValue(jsonString, new TypeReference<GenericResponse<SocialAccountsResponse>>() {});
System.out.println(socialAccountResponse.getData().getId());
System.out.println(socialAccountResponse.getData().getProfileUrl());
}
}