http://code.alibabatech.com/wiki/display/FastJSON/Serial+Features
package cn.focus.dc.jiajing.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* JsonResponse class.
* </p>
*
* @author xingtaoshi@gmail.com json工具类
* @version $Id: $Id
*/
public final class JsonResponse {
private JsonResponse() {
}
private static SerializerFeature[] features = {SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.DisableCircularReferenceDetect};
/**
* <p>
* badResult.
* </p>
*
* @param cause a {@link String} object.
* @return a {@link String} object.
*/
public static String badResult(String cause) {
JSONObject result = new JSONObject();
result.put("errorCode",1);
result.put("errorMessage",cause);
// return "@" + result.toJSONString();
return "@" + JSON.toJSONString(result,features);
}
/**
* <p>
* emptyResult.
* </p>
*
* @param cause a {@link String} object.
* @return a {@link String} object.
*/
public static String emptyResult(String cause) {
JSONObject result = new JSONObject();
result.put("errorCode",2);
result.put("errorMessage",features);
}
/**
* jsonp请求失败
*
* @param cause a {@link String} object.
* @param callback a {@link String} object.
* @return a {@link String} object.
*/
public static String badResult(String cause,String callback) {
JSONObject result = new JSONObject();
result.put("errorCode",cause);
// return "@" + callback + "(" + result.toJSONString() + ")";
return "@" + callback + "(" + JSON.toJSONString(result,features) + ")";
}
/**
* <p>
* badResult.
* </p>
*
* @param cause a {@link java.util.Map} object.
* @return a {@link String} object.
*/
public static String badResult(Map<String,String> cause) {
JSONObject result = new JSONObject();
result.put("errorCode",features);
}
/**
* 区分错误类型
*
* @param type a int.
* @param cause a {@link String} object.
* @return a {@link String} object.
*/
public static String badResult(int type,String cause) {
JSONObject result = new JSONObject();
result.put("errorCode",ImmutableMap.of("type",type,"msg",cause));
// return "@" + result.toJSONString();
return "@" + JSON.toJSONString(result,features);
}
/**
* <p>
* ok.
* </p>
*
* @return a {@link String} object.
*/
public static String ok() {
JSONObject result = new JSONObject();
result.put("errorCode",0);
result.put("data","成功");
return "@" + result.toString();
}
/**
* <p>
* okWithEmpty.
* </p>
*
* @return a {@link String} object.
*/
public static String okWithEmpty() {
JSONObject result = new JSONObject();
result.put("errorCode",Collections.emptyList());
return "@" + result.toString();
}
/**
* <p>
* ok.
* </p>
*
* @param key a {@link String} object.
* @param value a {@link Object} object.
* @return a {@link String} object.
*/
public static String ok(String key,Object value) {
JSONObject result = new JSONObject();
result.put("errorCode",ImmutableMap.of(key,value));
return "@" + JSON.toJSONString(result,features);
// return "@" + result.toJSONString();
}
/**
* <p>
* ok.
* </p>
*
* @param object a {@link Object} object.
* @return a {@link String} object.
*/
public static String ok(Object object) {
JSONObject result = new JSONObject();
result.put("errorCode",object);
return "@" + JSON.toJSONString(result,features);
// return "@" + result.toJSONString();
}
/**
* 返回分页结果json字串
*
* @param object 数据对象
* @param pageTotal 结果列表总共多少页
* @param pageSize 每页多少条记录
* @param pageNo 页号
* @return json 字串
*/
public static String okWithPaginate(Object object,int pageTotal,int pageSize,int pageNo) {
JSONObject result = new JSONObject();
result.put("pageTotal",pageTotal);
result.put("pageSize",pageSize);
result.put("pageNo",pageNo);
result.put("data",object);
result.put("errorCode",0);
return "@" + JSON.toJSONString(result,features);
// return "@" + result.toJSONString();
}
/**
* 返回分页结果json字串
*
* @param object 数据对象
* @param pageTotal 结果列表总共多少页
* @param pageSize 每页多少条记录
* @param pageNo 页号
* @param count 数据条数
* @return json 字串
*/
public static String okWithPaginate(Object object,int count) {
JSONObject result = new JSONObject();
result.put("data",object);
result.put("count",count);
result.put("errorCode",features);
// return "@" + result.toJSONString();
}
/**
* <p>
* jsonp.
* </p>
*
* @param object a {@link Object} object.
* @param callback a {@link String} object.
* @return a {@link String} object.
*/
public static String jsonp(Object object,object);
return "@" + callback + "(" + JSON.toJSONString(result,features) + ")";
// return "@" + callback + "(" + result.toJSONString() + ")";
}
/**
* 可以通过map形式传递参数,但是如果是ok,msg这两个参数的值会被覆盖掉
*
* @param params a {@link java.util.Map} object.
* @return a {@link String} object.
*/
public static String ok(Map<String,Object> params) {
JSONObject result = new JSONObject();
result.put("errorCode",params);
return "@" + JSON.toJSONString(result,features);
// return "@" + result.toJSONString();
}
/**
* 可以通过map形式传递参数,但是如果是ok,msg这两个参数的值会被覆盖掉
*
* @param params a {@link com.google.common.collect.ImmutableMap} object.
* @return a {@link String} object.
*/
public static String ok(ImmutableMap<String,features);
// return "@" + result.toJSONString();
}
/**
* 返回分页结果json字串
*
* @param list
* @param pageTotal
* @param pageSize
* @param pageNo
* @param <T>
* @return json 字串
*/
public static <T> String okWithPaginate(List<T> list,pageNo);
result.put("errorCode",list);
return "@" + JSON.toJSONString(result,features);
// return "@" + result.toJSONString();
}
/**
* 返回分页结果的json
*
* @param params
* @param pageTotal
* @param pageSize
* @param pageNo
* @return json 字串
*/
public static String okWithPaginate(Map<String,Object> params,features);
// return "@" + result.toJSONString();
}
/**
* 返回分页结果的json
*
* @param params
* @param pageTotal
* @param pageSize
* @param pageNo
* @return json 字串
*/
public static String okWithPaginateDisableRef(Map<String,features);
// return "@" + JSON.toJSONString(result,SerializerFeature.DisableCircularReferenceDetect);
}
/**
* 返回分页结果json字串,去掉fastjson循环引用功能
*
* @param object 数据对象
* @param pageTotal 结果列表总共多少页
* @param pageSize 每页多少条记录
* @param pageNo 页号
* @param count 数据条数
* @return json 字串
*/
public static String okWithPaginateDisableRef(Object object,0);
//消除fastjson对同一对象循环引用的问题
return "@" + JSON.toJSONString(result,SerializerFeature.DisableCircularReferenceDetect);
}
/**
* <p>
* main.
* </p>
*
* @param args an array of {@link String} objects.
*/
public static void main(String[] args) {
Map<String,Object> params = new HashMap<String,Object>();
params.put("test",1);
params.put("test1","ok");
}
}
原文链接:https://www.f2er.com/json/290374.html