package com.lanyuanxiaoyao.service.executor.task.functions.pulsar;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.apache.flink.core.io.SimpleVersionedSerializer;

/**
 * @author lanyuanxiaoyao
 * @date 2024-01-18
 */
public class ReadPulsarVersionedCheckpointSerializer implements SimpleVersionedSerializer<Collection<ReadPulsarSplit>>, Serializable {
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public int getVersion() {
        return 0;
    }

    @Override
    public byte[] serialize(Collection<ReadPulsarSplit> obj) throws IOException {
        return mapper.writeValueAsBytes(obj);
    }

    @Override
    public Collection<ReadPulsarSplit> deserialize(int version, byte[] serialized) throws IOException {
        return mapper.readValue(serialized, new TypeReference<List<ReadPulsarSplit>>() {
        });
    }
}
