加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

FastJsonUtils 需要fastjson

发布时间:2020-12-16 18:40:21 所属栏目:百科 来源:网络整理
导读:import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.PropertyFilter; import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
* Json工具
*/
public class FastJsonUtils {

public static final String SUCCESS_MSG = "数据加载成功";

private static SerializerFeature[] features = {SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.DisableCircularReferenceDetect};

/**
* 生成json返回结果
*/
public static String resultSuccess(Integer code,String msg,Object data){
Map<String,Object> rs= new HashMap<String,Object>();
rs.put("code",code);
rs.put("msg",StringUtils.isNotEmpty(msg)?msg:SUCCESS_MSG);
rs.put("data",data==null ? new Object() : data);
rs.put("error","");
return toString(rs);
}

public static String resultError(Integer code,String error,code);
rs.put("data",StringUtils.isNotEmpty(error)?error:"");
return toString(rs);
}

/**
* 生成json返回结果
*/
public static String resultList(Integer code,Integer pageNo,Integer pageSize,data==null ? new Object() : data);
rs.put("pageNo",pageNo == null ? 0 : pageNo);
rs.put("pageSize",pageSize == null ? 10 : pageSize);
return toString(rs);
}

/**
* 生成json返回结果
*/
public static String resultFeatures(Integer code,Object data,SerializerFeature... feature){
Map<String,data==null ? new Object() : data);
return JSON.toJSONString(rs,feature);
}

/**
* 生成json返回结果
*/
public static String resultDate(Integer code,String dateFormat){
Map<String,data==null ? new Object() : data);
return JSON.toJSONStringWithDateFormat(rs,dateFormat,features);
}
/**
* 生成json返回结果,包含字段
*/
public static String resultIncludes(Integer code,String ...properties){
Map<String,data==null ? new Object() : data);
return toStringIncludes(rs,properties);
}

/**
* 生成json返回结果,包含字段
*/
public static String resultExcludes(Integer code,Class<?> type,data==null ? new Object() : data);
return toStringExcludes(rs,type,properties);
}

/**
* 生成json字符串
*/
public static String toString(Object data){
return JSON.toJSONString(data,features);
}

/**
* 生成json字符串
*/
private static String toStringIncludes(Object data,String ...properties){
PropertyFilter filter = new PropertyFilter() {
@Override
public boolean apply(Object source,String name,Object value) {
if(source.getClass() == HashMap.class && ("code".equals(name) || "data".equals(name) || "msg".equals(name))){
return true;
}
for(String pro : properties){
if(pro.equals(name)){
return true;
}
}
return false;
}
};
return JSON.toJSONString(data,filter,features);
}

/**
* 排除字段
* @param args
*/
private static String toStringExcludes(Object data,Object value) {
if(source.getClass() == type){
for(String pro : properties){
if(pro.equals(name)){
return false;
}
}
}
return true;
}
};
return JSON.toJSONString(data,features); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读