pep1.commons.jpa.jackson.PageDeserializer Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Leonard Osang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package pep1.commons.jpa.jackson;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import pep1.commons.domain.dto.MetaDataDTO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created on 9/2/15.
*/
@Slf4j
public class PageDeserializer extends JsonDeserializer {
@Override
public Page deserialize(final JsonParser jsonParser, final DeserializationContext ctxt) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
JsonNode metaDataNode = node.get("meta");
MetaDataDTO metaDataDTO;
Class> contentClass = Object.class;
PageRequest request = null;
Long totalItems = null;
if (null != metaDataNode) {
metaDataDTO = oc.readValue(metaDataNode.traverse(), MetaDataDTO.class);
if (null != metaDataDTO) {
contentClass = guessClassByName(metaDataDTO.getContentType());
request = new PageRequest(metaDataDTO.getPage(), metaDataDTO.getPerPage());
totalItems = metaDataDTO.getTotalItems();
}
}
JsonNode contentNode = node.get("content");
List