package com.xunmei.common.core.json;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 文件前缀
 */
public class FileListSerializer extends JsonSerializer<List<Map<String, Object>>> {

    @Override
    public void serialize(final List<Map<String, Object>> value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
        gen.writeStartArray();
        for (Map<String, Object> s : (Iterable<Map<String, Object>>) Optional.<List>ofNullable(
                value).orElse(Collections.emptyList())) {
            final Object url = s.get("url");
            if (ObjectUtil.isEmpty(url)) {
                throw new RuntimeException("文件上传失败");
            }
            //s.put("url", UploadProperties.INSTANCE.getResource(url.toString()));
            gen.writeObject(s);
        }
        gen.writeEndArray();
    }
}
