package com.market.model.ws;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

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

/**
 * 兼容单个字符串和字符串数组的反序列化器。
 * <ul>
 *   <li>{@code "btcusdt"} → {@code ["btcusdt"]}</li>
 *   <li>{@code ["btcusdt","ethusdt"]} → {@code ["btcusdt","ethusdt"]}</li>
 * </ul>
 */
public class StringOrListDeserializer extends StdDeserializer<List<String>> {

    public StringOrListDeserializer() {
        super(List.class);
    }

    @Override
    public List<String> deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
        List<String> result = new ArrayList<>();
        if (p.currentToken() == JsonToken.START_ARRAY) {
            while (p.nextToken() != JsonToken.END_ARRAY) {
                result.add(p.getText());
            }
        } else {
            result.add(p.getText());
        }
        return result;
    }
}
