package com.sy.coinage.core.respconverter;

import java.io.IOException;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;

import com.sy.coinage.core.annotation.NotRestResponse;
import com.sy.coinage.core.resp.ReturnJson;

import lombok.extern.slf4j.Slf4j;

/**
 * -自定义序列化转换类
 */
@Slf4j
public class MyMessageConverter extends FastJsonHttpMessageConverter {

    /**
     * -响应类型和不序列化注解跳过转换
     *
     * @param clazz
     * @return
     */
    @Override
    protected boolean supports(Class<?> clazz) {
        boolean b = (null == clazz.getAnnotation(NotRestResponse.class)) && (!ReturnJson.class.equals(clazz));
        return b;
    }

    @Override
    protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
                    throws IOException, HttpMessageNotReadableException {
        return super.read(clazz, inputMessage);
    }

    /**
     * -统一封装返回实体
     *
     * @param obj 响应对象，为null时不处理
     * @param outputMessage
     * @throws IOException
     * @throws HttpMessageNotWritableException
     */
    @Override
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
                    throws IOException, HttpMessageNotWritableException {
        // ReturnJson resp = new ReturnJson(obj);
        // log.debug("ResponseJson={}", JSON.toJSONString(resp));
        super.writeInternal(obj, outputMessage);
    }

}
