美文网首页
Fastjson 序列化之 SerializerFeature

Fastjson 序列化之 SerializerFeature

作者: Tinyspot | 来源:发表于2023-02-19 07:47 被阅读0次

1. SerializerFeature 枚举类

  • 作用:设置序列化特性
名称 含义
PrettyFormat 格式化
WriteClassName 序列化时写入类型信息,默认为 false
WriteMapNullValue
WriteNullListAsEmpty
WriteNullNumberAsZero
WriteNullBooleanAsFalse
SortField 按字段名称排序后输出。默认为false
// SerializerFeature... features 可以传多个
public static String toJSONString(Object object, SerializerFeature... features) {
    return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);
}

1.1 SerializerFeature.WriteClassName

  • @type 指定类型 "@type":"全类名"
List<User> users = Arrays.asList(new User("Tinyspot", 20));
String jsonStr = JSON.toJSONString(users, SerializerFeature.WriteClassName);

打印结果:[{"@type":"com.example.conboot.starter.entity.User","age":20,"name":"Tinyspot"}]

Map<String, Object> map = new HashMap<>();
map.put("user", new User("Tinyspot", 20));
map.put("key", "other");
String jsonStr = JSON.toJSONString(map, SerializerFeature.WriteClassName);

打印结果:{"@type":"java.util.HashMap","user":{"@type":"com.example.conboot.starter.entity.User","age":20,"name":"Tinyspot"},"key":"other"}

User user = new User("Tinyspot", 20);
Order order = new Order(1001L, "E001");
Object[] obj = new Object[]{user, order};
String jsonStr = JSON.toJSONString(obj, SerializerFeature.WriteClassName);
System.out.println(jsonStr);

打印结果:[{"@type":"com.example.conboot.starter.entity.User","age":20,"name":"Tinyspot"},{"@type":"com.example.conboot.starter.entity.Order","code":"E001","id":1001}]

1.2 SerializerFeature.WriteMapNullValue

public static void main(String[] args) {
    String str = "{\"name\":\"Tinyspot\",\"age\":20}";

    User user = new User();
    user.setName("Tinyspot");
    // user.setAge(20);
    user.setBirthday(new Date());

    // WriteMapNullValue 字段null 序列化为 null
    String jsonStr = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
    // {"age":20,"name":null}

    // WriteMapNullValue 字段null 序列化为""
    jsonStr = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
    // {"age":20,"name":""}

    // WriteMapNullValue 字段null 序列化为""
    jsonStr = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
    // {"age":0,"name":"Tinyspot"}

    // 格式化日期
    jsonStr = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
    
    jsonStr = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.PrettyFormat);

    System.out.println(jsonStr);
}

    @Data
    static class User {
        private String name;
        private Integer age;
        private Boolean flag;
        private Date birthday;
    }

2. 反序列化

当 JSON 串里有指定类型的 @type时,直接反序列化会报错:autoType is not support

2.1 JSONObject

Object obj = new User("Tinyspot", 20);
String jsonStr = JSON.toJSONString(obj, SerializerFeature.WriteClassName);

JSONObject jsonObject = JSON.parseObject(jsonStr, Feature.IgnoreAutoType);
System.out.println(jsonObject);

2.2 JSONArray

User user = new User("Tinyspot", 20);
Order order = new Order(1001L, "E001");
Object[] obj = new Object[]{user, order};
String jsonStr = JSON.toJSONString(obj, SerializerFeature.WriteClassName);

ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
JSONArray jsonArray = JSON.parseArray(jsonStr);
System.out.println(jsonArray);

3. 属性过滤 SimplePropertyPreFilter

包含属性

SimplePropertyPreFilter filter = new SimplePropertyPreFilter(User.class, "name", "age");
// or
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getIncludes().add("name");

排除字段

SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("name");

4. 避免$ref

  • DisableCircularReferenceDetect 禁止循环引用检测,消除对同一对象循环引用的问题
  • 默认为false
String s = JSON.toJSONString(user, SerializerFeature.DisableCircularReferenceDetect);
引用 描述
"$ref":".." 上一级
"$ref":"@" 当前对象,即自引用
"ref":"" 根对象
"ref":".children.0" 基于路径的引用,相当于 root.getChildren().get(0)

{"$ref":"../.."} 引用父对象的父对象

相关文章

网友评论

      本文标题:Fastjson 序列化之 SerializerFeature

      本文链接:https://www.haomeiwen.com/subject/hufvkdtx.html