Last active
June 24, 2019 12:44
-
-
Save farhadfaghihi/8c01b1d0404afde46df0fa7f1c9be8c7 to your computer and use it in GitHub Desktop.
Demonstrating how to use Java Generic types with the Google's gson library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.farhadfaghihi; | |
import com.google.gson.Gson; | |
import com.google.gson.annotations.SerializedName; | |
import com.google.gson.reflect.TypeToken; | |
/** | |
* This project shows a sample code for using java Generic Types de/serialization, | |
* Google's gson library. | |
* The project is about consuming the responses from a webservice, which all of its | |
* responses have the structure of [code and data] as shown in the BaseResonse class | |
* @see io.github.farhadfaghihi.BaseResponse | |
* | |
* SampleResponse class is a realworld server response which extends the BaseResponse | |
* @see io.github.farhadfaghihi.SampleResponse | |
* | |
* The "mockResponse" string is going to be deserialized to an object of BaseResponse of type "SampleResponse" | |
* @see io.github.farhadfaghihi.GsonGenericsSample#mockReponse | |
* I think the rest is self-explanatory. | |
* | |
* @author farhadfaghihi | |
* @since 01-08-2017 | |
* @version 1.0 | |
* | |
*/ | |
public class GsonGenericsSample { | |
private static String mockReponse = "{\n" + | |
"\"code\" : 0 ,\n" + | |
" \"data\" :\n" + | |
" {\n" + | |
" \"sys_device_id\" : \"52\" ,\n" + | |
" \"sys_user_id\" : \"25\" ,\n" + | |
" \"is_activated\" : \"true\"\n" + | |
" }\n" + | |
"}" ; | |
public static void main(String[] args) { | |
BaseResponse<SampleResponse> response = new Gson().fromJson(mockReponse,new TypeToken<BaseResponse<SampleResponse>>(){}.getType()) ; | |
System.out.println(response.getData().getDeviceId()); | |
} | |
class BaseResponse<T> { | |
private int code; | |
private T data; | |
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; | |
} | |
} | |
class SampleResponse { | |
@SerializedName("sys_device_id") | |
private String deviceId ; | |
@SerializedName("sys_user_id") | |
private String userId ; | |
@SerializedName("is_activated") | |
private boolean isActivated ; | |
public String getDeviceId() { | |
return deviceId; | |
} | |
public void setDeviceId(String deviceId) { | |
this.deviceId = deviceId; | |
} | |
public String getUserId() { | |
return userId; | |
} | |
public void setUserId(String userId) { | |
this.userId = userId; | |
} | |
public boolean isActivated() { | |
return isActivated; | |
} | |
public void setActivated(boolean activated) { | |
isActivated = activated; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment