package com.ruoyi.framework.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.data.domain.Page;

import java.io.IOException;

public class PageSerializer extends JsonSerializer<Page> {

    @Override
    public void serialize(Page value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeNumberField("size", value.getSize());
        gen.writeNumberField("page", value.getNumber());
        gen.writeNumberField("totalElements", value.getTotalElements());
        gen.writeNumberField("totalPages", value.getTotalPages());
        gen.writeObjectField("content", value.getContent());
        gen.writeEndObject();
    }

    @Override
    public Class<Page> handledType() {
        return Page.class;
    }
}
