// Path: Server/src/main/java/com/easychat/exception/BadRequestException.java
// public class BadRequestException extends BasicException{
//     public BadRequestException(String error, String description) {
//         super(error, description);
//     }
// }
// 
// Path: Server/src/main/java/com/easychat/model/error/ErrorType.java
// public final class ErrorType {
//     public static final String NO_ERROR = "no_error";
//     public static final String INVALID_GRANT = "invalid_grant";
//     public static final String SERVICE_RESOURCE_NOT_FOUND = "service_resource_not_found";
//     public static final String DUPLICATE_UNIQUE_PROPERTY_EXISTS="duplicate_unique_property_exists";
//     public static final String ILLEGAL_ARGUMENT="illegal_argument";
//     public static final String AUTH_BAD_ACCESS_TOKEN="auth_bad_access_token";
// }


import com.easychat.exception.BadRequestException;
import com.easychat.model.error.ErrorType;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
        try {
            return objectMapper.readValue(json, valueType);
        } catch (JsonParseException e) {
            logger.error("decode(String, Class<T>)", e);
        } catch (JsonMappingException e) {
            logger.error("decode(String, Class<T>)", e);
        } catch (IOException e) {
            logger.error("decode(String, Class<T>)", e);
        }
        throw new BadRequestException(ErrorType.ILLEGAL_ARGUMENT, "invalid json");
    }

    /**
     * 将json array反序列化为对象
     *
     * @param json
     * @param TypeReference<T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T decode(String json, TypeReference<T> typeReference) throws BadRequestException {
        try {
            return (T) objectMapper.readValue(json, typeReference);
        } catch (JsonParseException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        } catch (JsonMappingException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        } catch (IOException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        }
// Path: Server/src/main/java/com/easychat/exception/BadRequestException.java
// public class BadRequestException extends BasicException{
//     public BadRequestException(String error, String description) {
//         super(error, description);
//     }
// }
// 
// Path: Server/src/main/java/com/easychat/model/error/ErrorType.java
// public final class ErrorType {
//     public static final String NO_ERROR = "no_error";
//     public static final String INVALID_GRANT = "invalid_grant";
//     public static final String SERVICE_RESOURCE_NOT_FOUND = "service_resource_not_found";
//     public static final String DUPLICATE_UNIQUE_PROPERTY_EXISTS="duplicate_unique_property_exists";
//     public static final String ILLEGAL_ARGUMENT="illegal_argument";
//     public static final String AUTH_BAD_ACCESS_TOKEN="auth_bad_access_token";
// }


// Path: Server/src/main/java/com/easychat/utils/JsonUtils.java
import com.easychat.exception.BadRequestException;
import com.easychat.model.error.ErrorType;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;

        try {
            return objectMapper.readValue(json, valueType);
        } catch (JsonParseException e) {
            logger.error("decode(String, Class<T>)", e);
        } catch (JsonMappingException e) {
            logger.error("decode(String, Class<T>)", e);
        } catch (IOException e) {
            logger.error("decode(String, Class<T>)", e);
        }
        throw new BadRequestException(ErrorType.ILLEGAL_ARGUMENT, "invalid json");
    }

    /**
     * 将json array反序列化为对象
     *
     * @param json
     * @param TypeReference<T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T decode(String json, TypeReference<T> typeReference) throws BadRequestException {
        try {
            return (T) objectMapper.readValue(json, typeReference);
        } catch (JsonParseException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        } catch (JsonMappingException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        } catch (IOException e) {
            logger.error("decode(String, JsonTypeReference<T>)", e);
        }
        throw new BadRequestException(ErrorType.ILLEGAL_ARGUMENT, "invalid json");