package utils;

import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

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

public class JsonUtils {

	/**
	 * 格式化输出json数据<br>
	 <pre>
{
  "sex" : "男",
  "name" : "lyle",
  "age" : "20"
}
	 </pre>
	 * @param obj
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static String format(Object obj) throws JsonGenerationException, JsonMappingException, IOException{
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		return mapper.writeValueAsString(obj);
	}
	
	/**
	 * 格式化输出json数据<br>
	 *<pre>
{
	"sex":"男",
	"name":"lyle",
	"age":"20"
}
	 *</pre>
	 * @param obj
	 * @return
	 */
	public static String format1(Object obj){
		return JSON.toJSONString(obj, true);
	}
	
	/**
	 * 格式化输出json，单引号
	 * <pre>{'sex':'男','name':'lyle','age':'20'}</pre>
	 * @param obj
	 * @return
	 */
	public static String format2(Object obj){
		return JSON.toJSONString(obj, SerializerFeature.UseSingleQuotes);
		
	}
	
	/**
	 * "2016-09-10 17:49:08"
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date){
		return JSON.toJSONString(date,SerializerFeature.WriteDateUseDateFormat);
	}
	
	/**
	 * 由于序列化带了类型信息，使得反序列化时能够自动进行类型识别
	 * @param obj
	 * @return
	 */
	public static String formatWithClassInfo(Object obj){
		return JSON.toJSONString(obj, SerializerFeature.WriteClassName);
	}
	
	
	
}

@@ -354,44 +354,236 @@
 lize
-rFeature;%0A%0Apublic class JsonUtils %7B%0A
+Config;%0Aimport com.alibaba.fastjson.serializer.SerializerFeature;%0Aimport com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;%0A%0Apublic class JsonUtils %7B%0A%0A%09private static SerializeConfig mapping = new SerializeConfig();  %0A%09
 %0A%09/*
@@ -1889,11 +1889,190 @@
 %09%7D%0A%09%0A%09%0A%09
+public static String formatDate(Date date,String format)%7B%0A%09%09mapping.put(Date.class, new SimpleDateFormatSerializer(format));%0A%09%09return JSON.toJSONString(date, mapping);%0A%09%09%0A%09%09%0A%09%09%0A%09%7D
 %0A%7D%0A

add new function