FastJson

过滤器

简单过滤器

添加不需要序列化的字段

SimplePropertyPreFilter projectPreFilter = new SimplePropertyPreFilter();
projectPreFilter.getExcludes().addAll(Arrays.asList("createUser", "applyUser"));

PropertyPreFilter

根据 PropertyName 判断是否序列化

PropertyFilter

根据 PropertyName 和 PropertyValue 来判断是否序列化

public interface PropertyFilter extends SerializeFilter {
boolean apply(Object object, String propertyName, Object propertyValue);
}

NameFilter

修改 Key,如果需要修改 Key,process 返回值则可

public interface NameFilter extends SerializeFilter {
String process(Object object, String propertyName, Object propertyValue);
}

ValueFilter

修改 Value

public interface ValueFilter extends SerializeFilter {
Object process(Object object, String propertyName, Object propertyValue);
}

BeforeFilter

序列化时在最前添加内容

AfterFilter

序列化时在最后添加内容

联合使用

JSONObject.toJSONString(object, new SerializeFilter[]{simplePropertyPreFilter, valueFilter});

Bean注解

@JSONField(format="yyyyMMdd")
@JSONField(serialize=false)
@JSONField(deserialize=false)

循环引用

JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);