Solon

solon.serialization.fastjson

v2.7.5 native </> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.serialization.fastjson</artifactId>
</dependency>

1、描述

序列化扩展插件,为 Solon Serialization 提供基于 Fastjson(代码仓库)的框架适配。这插件也可用作 Solon Rpc 的服务端序列化方案。

使用时,会涉及到格式化的定制,其它就不会有显示的感受。

2、主要接口实现类

实现接口备注
FastjsonRenderFactoryJsonRenderFactory用于处理 json 渲染输出
FastjsonActionExecutorActionExecuteHandler用于执行 json 内容的请求

何时会被时用?当 Content-Type 为 application/json(或 text/json)时会执行。

3、快捷格式化输出配置 (v1.12.3 后支持)

solon.serialization.json:
  dateAsFormat: 'yyyy-MM-dd HH:mm:ss' #配置日期格式(默认输出为时间戳)
  dateAsTimeZone: 'GMT+8' #配置时区
  dateAsTicks: false #将date转为毫秒数(和 dateAsFormat 二选一)
  longAsString: true #将long型转为字符串输出 (默认为false)
  boolAsInt: false   #将bool型转为字符串输出 (默认为false)
  nullStringAsEmpty: false
  nullBoolAsFalse: false
  nullNumberAsZero: false
  nullArrayAsEmpty: false
  nullAsWriteable: false #输出所有null值
  enumAsName: false #枚举使用名字(v2.2.1 后支持)

提醒:

  • 配置 null???As??? 时实体的null字段也会输出,但Map的null不会输出
  • longAsString + nullNumberAsZero 时,long 型 null 的不会转成字符串 "0"
  • boolAsInt + nullBoolAsFalse 时,bool 型 null 的不会转成字符串 0

4、高级格式化定制(基于接口)

格式化输出定制和请求处理定制,(v1.12.3 后支持):

@Configuration
public class Config {
    @Bean
    public void jsonInit(@Inject FastjsonRenderFactory factory, @Inject FastjsonActionExecutor executor){
        //示例1:通过转换器,做简单类型的定制
        factory.addConvertor(Date.class, s -> s.getTime());

        factory.addConvertor(LocalDate.class, s -> s.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        factory.addConvertor(Long.class, s -> String.valueOf(s));

        //示例2:通过编码器,做复杂类型的原生定制(基于框架原生接口)
        factory.addEncoder(Date.class, (ser, obj, o1, type, i) -> {
            SerializeWriter out = ser.getWriter();
            out.writeLong(((Date) obj).getTime());
        });

        //示例3:重置序列化特性(例,添加序列化null的特性)
        factory.addFeatures(SerializerFeature.WriteMapNullValue); //添加特性
        //factory.removeFeatures(SerializerFeature.BrowserCompatible); //移除特性
        //factory.setFeatures(...); //重设特性
        
        
        //factory.config()...
        //executor.config()...
    }
}

5、个性化输出定制

public class User{
    public long userId;
    
    public String name;
    
    //排除序列化
    public transient password; //使用 transient 排除具有通用性
    
    //格式化日期
    @JSONField(format = "yyyy-MM-dd") //尽量不使用个性化定制//这样不会依赖具体框架
    public Date birthday;
}