Jfinal 2.1 之拓展JSON,使用阿里巴巴的fastjson,文采不行,直接晒代码。

【1】、第一步,继承Jfinal的基类Json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jfinal.json.Json;

public class MyJson extends Json{
	
	public static String toJsonStr(Object object) {
		return JSON.toJSONString(object,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteMapNullValue);
	}
	
	@Override
	public String toJson(Object object) {
		return JSON.toJSONString(object,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteMapNullValue);
	}

	@Override
	public <T> T parse(String jsonString, Class<T> type) {
		return JSON.parseObject(jsonString, type);
	}

}

【2】第二步,在Jfinal类里配置一下

public class MyConfig extends JFinalConfig {	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {		
		me.setJsonFactory(new IJsonFactory() {
	        public Json getJson() { return new MyJson();}});
	}
}

搞定了。

你可能感兴趣的:(json,fastjson,jFinal,jFinal)