package com.xunmei.common.core.json;


import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 图片前缀
 */
public class ImageListDeserializer extends JsonDeserializer<List<String>> {

    @Override
    public List<String> deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
        if (p.getCurrentToken() == JsonToken.START_ARRAY) {
            final List<String> resourceList = new ArrayList<>();
            while (p.nextToken() != JsonToken.END_ARRAY) {
                final String resource = p.getValueAsString();
                if (StringUtils.isNotBlank(resource)) {
                    resourceList.add(resource);
//                    resourceList.add(UploadProperties.INSTANCE.pureResource(resource));
                }
            }
            return resourceList;
        }
        return Collections.emptyList();
    }
}
