反序列化无分页Json字符串加入分页信息

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


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值