FastJsonFactory me = FastJsonFactory.me();
me.setJsonFactory(me);
FastJson会出现循环引用的问题。
"$ref":"$.data.list[1].category",加1个设置
SerializerFeature.DisableCircularReferenceDetect
解决它:
自定义FastJson
public class MyFastJson extends FastJson{
public String toJson(Object object) {
// 优先使用对象级的属性 datePattern, 然后才是全局性的 defaultDatePattern
String dp = datePattern != null ? datePattern : getDefaultDatePattern();
if (dp == null) {
return JSON.toJSONString(object);
} else {
return JSON.toJSONStringWithDateFormat(object, dp, SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect); // return JSON.toJSONString(object, SerializerFeature.WriteDateUseDateFormat);
}
}
}
public class MyFastJsonFactory extends FastJsonFactory{
private static final MyFastJsonFactory me = new MyFastJsonFactory();
public static MyFastJsonFactory me() {
return me;
}
public Json getJson() {
return new MyFastJson();
}
}
public void configConstant(Constants me) {
me.setDevMode(p.getBoolean("devMode", true));
//me.setJsonFactory(MixedJsonFactory.me());
MyFastJsonFactory me2 = MyFastJsonFactory.me();
me.setJsonFactory(me2);
}
总的来说,MixedJsonFactory 好用一点。
--------------------list集合 序列化之后$ref---------------------------------------
{"category":{"$ref":"$.data.list[1].category"},"categoryId":2,"company":{"$ref":"$.data.list[0].company"},"companyId":1,"createTime":"2018-01-01 10:00:00","id":9,"name":"作物","status":0,"yn":1},{"category":{"createTime":"2018-01-01 10:00:00","id":5,"name":"烘焙蛋糕","remark":"尝试中","status":1,"updateTime":"2018-01-01 00:00:00","yn":1},"categoryId":5,"company":{"address":"北京","content":"文创第一品牌","createTime":"2018-01-02 01:00:00.0","id":2,"mobile":"15978955878","name":"北京十八字金有限公司","remark":"很不错","updateTime":"2018-01-01 00:00:00","yn":1},"companyId":2,"createTime":"2018-01-01 10:00:00","id":8,"name":"蒂丽雪思","status":0,"yn":1},{"category":{"$ref":"$.data.list[1].category"},"categoryId":2,"company":{"$ref":"$.data.list[0].company"},"companyId":1,"createTime":"2018-01-01
感谢分享