package com.ruoyi.common.utils.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.List;
import java.util.Map;

public class ResponseHandler {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 转换为自定义对象
    public static <T> T parseJsonToObject(String json, Class<T> valueType) throws Exception {
        return objectMapper.readValue(json, valueType);
    }

    // 转换为Map
    public static Map<String, Object> parseJsonToMap(String json) throws Exception {
        return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
    }

    // 转换为List
    public static <T> List<T> parseJsonToList(String json, Class<T> elementType) throws Exception {
        return objectMapper.readValue(json,
                objectMapper.getTypeFactory().constructCollectionType(List.class, elementType));
    }
}