package com.softwarebr.nunu.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect;

/**
 * @Author: ljr
 * @Date: 2019/9/5 15:39
 * @Description:
 */
@Configuration
public class MessageConverterConfig {

    @Bean
    public HttpMessageConverters customConverters() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        converter.setFastJsonConfig(config);
        config.setSerializerFeatures(DisableCircularReferenceDetect,
                //null的字段也会输出
                SerializerFeature.WriteMapNullValue
                //字符串为null字段返回""
                //SerializerFeature.WriteNullStringAsEmpty,
                //List字段如果为null,输出为[],而非null
                //SerializerFeature.WriteNullListAsEmpty,
                ////数值字段如果为null,输出为0,而非null
                //SerializerFeature.WriteNullNumberAsZero,
                ////Boolean字段如果为null,输出为false,而非null
                //SerializerFeature.WriteNullBooleanAsFalse);
        );
        return new HttpMessageConverters(converter);
    }
}
