net.sf.json一个用于排除或包含对象属性的工具类
程序员文章站
2022-04-08 13:58:07
...
import java.util.Arrays;
import net.sf.json.util.PropertyFilter;
/**
* 一个用于排除或包含对象属性的工具类
*
* <pre>
* public static void main(String[] args) {
* JSONObject jsonObject = createJSONObject();
* JsonConfig jsonConfig = new JsonConfig();
* jsonConfig.setJsonPropertyFilter(
* PropertyFilterUtil.includeProperties(new String[] { "username", "sex", "birthday" }));
* System.out.println(JSONObject.fromObject(jsonObject, jsonConfig));
* }
*
* private static JSONObject createJSONObject() {
* JSONObject jsonObject = new JSONObject();
* jsonObject.put("username", "Ouvidia");
* jsonObject.put("sex", "M");
* jsonObject.put("birthday", "Meeeee");
* jsonObject.put("address", "Mddddd");
* return jsonObject;
* }
* </pre>
*
*/
public class PropertyFilterUtil {
/**
* 包含某些属性
*@author:LJH
*@createTime:2018-01-28 22:54:45
*@param includes
*@return
*/
static PropertyFilter includeProperties(final String[] includes) {
return new PropertyFilter() {
@Override
public boolean apply(Object obj, String property, Object value) {
//返回false 说明是要包含的属性,返回true 说明是要排除的属性
return Arrays.asList(includes).contains(property)?false:true;
}
};
}
/**
* 排除某些属性
*@author:LJH
*@createTime:2018-01-28 22:55:19
*@param includes
*@return
*/
static PropertyFilter excludeProperties(final String[] excludes) {
return new PropertyFilter() {
@Override
public boolean apply(Object obj, String property, Object value) {
//返回false 说明是要包含的属性,返回true 说明是要排除的属性
return Arrays.asList(excludes).contains(property)?true:false;
}
};
}
}
上一篇: Heap 堆
下一篇: POJ ACM习题【No.2403】