FastJson:自定义序列化Boolean转Integer

FastJson:自定义序列化Boolean转Integer,第1张

FastJson:自定义序列化Boolean转Integer 前言
  • 在前后端分离开发模式下,接口规范统一处理Boolean类型,需要转成0和1,使用SpringBoot + fastjson全局统一配置,具体配置如下
配置自定义序列化规则 自定义序列化
  • 自定义BooleanToIntegerSerializer实现ObjectSerializer接口方法
package com.jyusun.origin.basic.common.config.fastjson;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.clamc.common.util.ObjectUtil;

import java.io.IOException;
import java.lang.reflect.Type;


public class BooleanToIntegerSerializer implements ObjectSerializer {


    @Override
    public void write(JSONSerializer jsonSerializer, Object object, Object o1, Type type, int i) throws IOException {
        SerializeWriter out = jsonSerializer.getWriter();
        int value;
        if (ObjectUtil.isEmpty(object)) {
            value = 0;
        } else {
            value = Boolean.parseBoolean(String.valueOf(object)) ? 1 : 0;
        }
        out.writeInt(value);
    }
}
FastJsonConfiguration
  • 配置自定义的序列化代码 fastJsonConfig.getSerializeConfig().put(Boolean.class, new BooleanToIntegerSerializer());
package com.jyusun.origin.basic.common.config.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.clamc.common.util.DateUtil;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;


@Configuration
@ConditionalOnClass(JSON.class)
@ConditionalOnMissingBean(FastJsonHttpMessageConverter.class)
@ConditionalOnWebApplication
public class FastJsonConfiguration {

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setFastJsonConfig(fastJsonConfig());
        converter.setSupportedMediaTypes(getSupportedMediaType());
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        return converter;
    }

    
    private FastJsonConfig fastJsonConfig() {
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                // Map 默认值
                SerializerFeature.WriteMapNullValue,
                // 数组 默认 空数组
                SerializerFeature.WriteNullListAsEmpty,
                // 数值类型 默认为 0
                SerializerFeature.WriteNullNumberAsZero,
                // Boolean 默认为 false
                SerializerFeature.WriteNullBooleanAsFalse,
                
                SerializerFeature.WriteDateUseDateFormat,
                // 消除循环引用
                SerializerFeature.DisableCircularReferenceDetect,
                // 字段名称排序后输出
                SerializerFeature.SortField
        );

        fastJsonConfig.setDateFormat(DateUtil.PATTERN_YYYY_MM_DD_HH_MM_SS);
        fastJsonConfig.setCharset(Charset.forName("utf-8"));
        // 自定义Boolean类型序列化配置
        fastJsonConfig.getSerializeConfig().put(Boolean.class, new BooleanToIntegerSerializer());
        return fastJsonConfig;
    }

    
    private List getSupportedMediaType() {
        ArrayList mediaTypes = Lists.newArrayList();

        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        mediaTypes.add(MediaType.APPLICATION_PDF);
        mediaTypes.add(MediaType.APPLICATION_RSS_XML);
        mediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        mediaTypes.add(MediaType.APPLICATION_XML);
        mediaTypes.add(MediaType.IMAGE_GIF);
        mediaTypes.add(MediaType.IMAGE_JPEG);
        mediaTypes.add(MediaType.IMAGE_PNG);
        mediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        mediaTypes.add(MediaType.TEXT_HTML);
        mediaTypes.add(MediaType.TEXT_MARKDOWN);
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.TEXT_XML);

        //增加解析spring boot actuator结果的解析
        mediaTypes.add(MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));

        return mediaTypes;
    }
}
运行结果
{
  "code": "S00200",
  "message": " *** 作成功",
  "sign": 1,
  "body": {}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5637462.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存