fastjson(十二)Class_Level_SerializeFilter

对于框架来说,如果在toJSONString的时候,传入SerializeFilter,会导致对所有的类型做过滤,性能会受到一定影响。在1.2.10版本之后,fastjson提供类级别的SerializeFilter支持。

Interface

package com.alibaba.fastjson.serializer;

public class SerializeConfig {
    public void addFilter(Class clazz, SerializeFilter filter);
}

Sample

package json.fastjson.filter.classLevelFilter;

public class A {
    public int id;
}
package json.fastjson.filter.classLevelFilter;

public class B {
    public int id;
}
package json.fastjson.filter.classLevelFilter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeConfig;

public class TestClassLevelFilter {

    public static void main(String[] args) {
        NameFilter upcaseNameFilter = new NameFilter() {
            public String process(Object object, String name, Object value) {
                return name.toUpperCase();
            }
        };
        SerializeConfig.getGlobalInstance().addFilter(A.class, upcaseNameFilter);

        System.out.println(JSON.toJSONString(new A()));
        System.out.println(JSON.toJSONString(new B()));
    }

}

输出结果:

{"ID":0}
{"id":0}

你可能感兴趣的:(fastjson,FastJSON)