package com.storlead.framework.web.process;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import java.io.IOException;

/**
 * 保证 API 始终输出 UTF-8 文本 JSON。
 */
public class StorleadFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {

    @Override
    protected void writeInternal(Object object, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        if (object instanceof byte[]) {
            byte[] bytes = (byte[]) object;
            outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            outputMessage.getBody().write(bytes, 0, bytes.length);
            return;
        }
        super.writeInternal(object, outputMessage);
    }
}
