FastJson序列化(限制部分属性的序列化)

FastJson序列化(限制部分属性的序列化)

JSONObject继承体系

在这里插入图片描述
JSONObject是实现了Map<String,Object>接口的,内部也是用Map的子类HashMap来存储元素

//默认初始化容量16
private static final int          DEFAULT_INITIAL_CAPACITY = 16;
//存储数据
private final Map<String, Object> map;

传入boolean值true,可以指定存入元素的有序性

/**
ordered:表示有序的,如果为true,表示存储元素的插入顺序,就采用Map的子类LinkedHashMap,如果为false,则使用HashMap
initialCapacity:map的初始化容量,建议根据实际情况而定,事先分配足够的容量可以避免不需要的扩容
**/
public JSONObject(int initialCapacity, boolean ordered){
    if (ordered) {
        map = new LinkedHashMap<String, Object>(initialCapacity);
    } else {
        map = new HashMap<String, Object>(initialCapacity);
    }
}
//调用上面的构造,initialCapacity为DEFAULT_INITIAL_CAPACITY
public JSONObject(boolean ordered){
    this(DEFAULT_INITIAL_CAPACITY, ordered);
}

基本使用toJSONString方法

将对象转换为json字符串

JSONObject.toJSONString(obj);// obj就是需要序列化为字符串的对象

这种方式其实是不被推荐的,因为JSONObject的toJSONString方法是继承自JSON的,所以直接使用

JSON.toJSONString(obj);//obj就是需要序列化为字符串的对象

为什么要使用JSON.toJSONString,而不是JSONObject.toJSONString?

  1. toJSONString是静态方法,是属于JSON类的静态方法,如果通过其子类JSONObject去方法会造成认知混淆,让代码维护者错误的认为toJSONString是JSONObject中的静态方法,这样导致代码的可读性降低了。

这种序列化方式,我们只能被动选择将所有的属性都序列化为字符串

排除不需要的属性

通过查看JSON的源码可以知道,内部还是提供了限制不需要的属性的序列化方式

//方法1
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
    return toJSONString(object, SerializeConfig.globalInstance, new SerializeFilter[] {filter}, null, DEFAULT_GENERATE_FEATURE, features);
}

//方法二
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
    return toJSONString(object, SerializeConfig.globalInstance, filters, null, DEFAULT_GENERATE_FEATURE, features);
}
  1. object:需要序列化的对象
  2. filter:序列化过滤器
  3. features:序列化特性
方法一的使用
@Test
public void TestToJSONString() {
    Person person = new Person(10L, "tianshi", 22);
    //不想id被序列化
    PropertyPreFilters.MySimplePropertyPreFilter propertyPreFilter = new PropertyPreFilters().addFilter();
    propertyPreFilter.addExcludes("id","name");
    String personStr = JSON.toJSONString(person, propertyPreFilter);
    System.out.println(personStr);
}

private static class Person implements Serializable {
    Long id;

    String name;

    Integer age;

    public Person() {
    }

    public Person(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    .... 省略getset方法
}

Person对象就是我们需要序列化的对象,PropertyPreFilters.MySimplePropertyPreFilter是继承自SimplePropertyPreFilter类的,继承体系如图:
在这里插入图片描述
所以MySimplePropertyPreFilter也是序列化过滤器,可以用来指定包含的属性和排除的属性

public MySimplePropertyPreFilter addExcludes(String... filters){
    for (int i = 0; i < filters.length; i++) {
        this.getExcludes().add(filters[i]);
    }
    return this;
}

public MySimplePropertyPreFilter addIncludes(String... filters){
    for (int i = 0; i < filters.length; i++) {
        this.getIncludes().add(filters[i]);
    }
    return this;
}

addExcludes用来指定排除的属性,addIncludes用来指定包含的属性,两个方法可以一起使用(很奇怪,为啥包含了还要排除呢)

如果属性是对象如何排除其属性呢
@Test
public void TestToJSONString() {
    Person person = new Person(10L, "tianshi", 22);
    List<Student> studentList = person.getStudentList();
    studentList.add(new Student(11L,"zhangsan",10));
    studentList.add(new Student(12L,"lisi",11));
    studentList.add(new Student(13L,"wangwu",12));
    //Person的id属性不被序列化,Student的id属性不被序列化
    PropertyPreFilters.MySimplePropertyPreFilter propertyPreFilter = new PropertyPreFilters().addFilter();
    propertyPreFilter.addExcludes("age","studentList.id");
    String personStr = JSON.toJSONString(person, propertyPreFilter);
    System.out.println(personStr);
}

private static class Person {
    Long id;

    String name;

    Integer age;

    List<Student> studentList = new ArrayList<>();

    .... 省略getset方法

}

private static class Student{
    Long id;

    String name;

    Integer age;

    public Student() {
    }

    public Student(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    .... 省略getset方法
}

通过属性名称+"."+属性类型的属性名称指定需要拦截的属性的属性

propertyPreFilter.addExcludes("age","studentList.id");

后续会继续完善

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值