package com.ruoyi.rocketmq.domain;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.api.domain.NetworkInfo;

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

public class NetworkInfoDeserializer extends JsonDeserializer<List<NetworkInfo>> {
    // 添加无参构造函数（Jackson 需要）
    public NetworkInfoDeserializer() {
    }
    @Override
    public List<NetworkInfo> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        ObjectMapper mapper = (ObjectMapper) p.getCodec();
        JsonNode node = mapper.readTree(p);

        // 如果 networkInfo 是字符串（如日志中的情况），先解析字符串
        if (node.isTextual()) {
            String jsonStr = node.textValue();
            return mapper.readValue(jsonStr, new TypeReference<List<NetworkInfo>>() {});
        }
        // 如果 networkInfo 是数组，直接解析
        else if (node.isArray()) {
            return mapper.readValue(node.traverse(), new TypeReference<List<NetworkInfo>>() {});
        }
        throw new IOException("Invalid networkInfo format");
    }
}
