String strFrom = "{\"totalCount\":5,\"pageSize\":3,\"currentPage\":1,\"persons\":[{\"name\":\"1\"},{\"name\":\"2\"},{\"name\":\"3\"}]}";
GsonBuilder customGsonBuilder = new GsonBuilder().registerTypeAdapter(new TypeToken<PaginationGson<PersonWithName>>(){}.getType(),new PaginationDeser<PersonWithName>(3,5,2));
class PaginationDeser<T> implements JsonDeserializer<PaginationGson<T>>{
private int pageSize;
private long totalCount;
private int currentPage;
public PaginationDeser(int pageSize,long totalCount,int currentPage){
this.pageSize = pageSize;
this.totalCount = totalCount;
this.currentPage = currentPage;
}
@Override
public PaginationGson<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
PaginationGson<T> paginationGson = new PaginationGson<T>(pageSize,totalCount,currentPage);
Type typeA = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
JsonArray ja = json.getAsJsonArray();
List list = new ArrayList();
for (JsonElement je : ja) {
list.add(context.deserialize(je, typeA));
}
paginationGson.setData(list);
return paginationGson;
}
}
class PersonListGson{
private Long totalCount;
private Integer pageSize;
private Integer currentPage;
// @SerializedName("persons")
private PaginationGson<PersonWithName> persons;
public Long getTotalCount() {
return totalCount;
}
public void setTotalCount(Long totalCount) {
this.totalCount = totalCount;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
}
import java.util.ArrayList;
import java.util.List;
public class PaginationGson<T>{
protected long totalCount;
protected int totalPage;
protected int prePage;
protected int nextPage;
protected int currentPage;
protected int pageLimit;
private List<T> data = new ArrayList<T>();
public List<T> getData() {
return data;
}
public PaginationGson(int pageLimit, long totalCount, int requestPage) {
this.totalPage = (int) Math.ceil((double) totalCount / (double) pageLimit);
this.currentPage = caculateCurrentPage(requestPage);
this.prePage = (this.currentPage > 1) ? this.currentPage - 1 : 0;
this.nextPage = (totalPage > this.currentPage) ? this.currentPage + 1 : totalPage;
this.totalCount = totalCount;
this.pageLimit = pageLimit;
}
public PaginationGson(){
}
public void setData(List data) {
this.data = data;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageLimit() {
return pageLimit;
}
public void setPageLimit(int pageLimit) {
this.pageLimit = pageLimit;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
private int caculateCurrentPage(int requestPage) {
return Math.min(totalPage, Math.max(1, requestPage));
}
}