package com.jwipc.api;

import com.alibaba.fastjson2.JSONWriter;
import com.dtflys.forest.converter.json.ForestFastjson2Converter;
import com.dtflys.forest.converter.json.ForestJsonConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Configuration
public class ForestConfig {
    @Bean
    public ForestJsonConverter forestFastjson2Converter() {
        // 2. 添加fastjson转换器
        ForestFastjson2Converter converter = new ForestFastjson2Converter();
        converter.addWriterFeature(JSONWriter.Feature.WriteNullListAsEmpty);
        //json格式化
//        converter.addWriterFeature(JSONWriter.Feature.PrettyFormat);
        //输出map中value为null的数据
        converter.addWriterFeature(JSONWriter.Feature.WriteMapNullValue);
        //输出boolean 为 false
        converter.addWriterFeature(JSONWriter.Feature.WriteNullBooleanAsFalse);
        //输出list 为 []
        converter.addWriterFeature(JSONWriter.Feature.WriteNullListAsEmpty);
        //输出number 为 0
        converter.addWriterFeature(JSONWriter.Feature.WriteNullNumberAsZero);
        //输出字符串 为 ""
        converter.addWriterFeature(JSONWriter.Feature.WriteNullStringAsEmpty);
        //对map进行排序
        converter.addWriterFeature(JSONWriter.Feature.MapSortField);
        return converter;
    }
}
