package cn.base.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.base.util.RHUtil;

/**
 * Json处理??br>
 * @author redhigh
 * @since 2013-12-04
 */
public class JsonHelper {
	private static Log log = LogFactory.getLog(JsonHelper.class);
	/**
	 * 将不含日期时间格式的Java对象系列化为Json资料格式
	 * @param pObject 传入的Java对象
	 * @return
	 */
	public static final String encodeObject2Json(Object pObject) {
		String jsonString = "[]";
		if (RHUtil.isEmpty(pObject)) {
			// log.warn("传入的Java对象为空,不能将其序列化为Json资料格式.请检??");
		} else {
			if (pObject instanceof ArrayList) {
				JSONArray jsonArray = JSONArray.fromObject(pObject);
				jsonString = jsonArray.toString();
			} else {
				JSONObject jsonObject = JSONObject.fromObject(pObject);
				jsonString = jsonObject.toString();
			}
		}
		if (log.isInfoEnabled()) {
			log.info("序列化后的JSON资料输出:\n" + jsonString);
		}
		return jsonString;
	}
	/**
	 * 将含有日期时间格式的Java对象系列化为Json资料格式<br>
	 * Json-Lib在处理日期时间格式是????实现其JsonValueProcessor接口,????在这里提供一个重载的方法对含??br>
	 * 日期时间格式的Java对象进行序列??
	 * @param pObject 传入的Java对象
	 * @return
	 */
	public static final String encodeObject2Json(Object pObject, String pFormatString) {
		String jsonString = "[]";
		if (RHUtil.isEmpty(pObject)) {
			// log.warn("传入的Java对象为空,不能将其序列化为Json资料格式.请检??");
		} else {
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonValueProcessorImpl(pFormatString));
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl(pFormatString));
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl(pFormatString));
			if (pObject instanceof ArrayList) {
				JSONArray jsonArray = JSONArray.fromObject(pObject, cfg);
				jsonString = jsonArray.toString();
			} else {
				JSONObject jsonObject = JSONObject.fromObject(pObject, cfg);
				jsonString = jsonObject.toString();
			}
		}
		if (log.isInfoEnabled()) {
			log.info("序列化后的JSON资料输出:\n" + jsonString);
		}
		return jsonString;
	}
	/**
	 * 将分页信息压入JSON字符??
	 * @param JSON字符??
	 * @param totalCount
	 * @return 返回合并后的字符??
	 */
	public static String encodeJson2PageJson(String jsonString, Integer totalCount) {
		jsonString = "{TOTALCOUNT:" + totalCount + ", ROOT:" + jsonString + "}";
		if (log.isInfoEnabled()) {
			log.info("合并后的JSON资料输出:\n" + jsonString);
		}
		return jsonString;
	}
	/**
	 * 直接将List转为分页????要的Json资料格式
	 * @param list ????编码的List对象
	 * @param totalCount 记录总数
	 * @param pDataFormat 时间日期格式??传null则表明List不包含日期时间属??
	 */
	public static final String encodeList2PageJson(List list, Integer totalCount, String dataFormat) {
		String subJsonString = "";
		if (RHUtil.isEmpty(dataFormat)) {
			subJsonString = encodeObject2Json(list);
		} else {
			subJsonString = encodeObject2Json(list, dataFormat);
		}
		String jsonString = "{TOTALCOUNT:" + totalCount + ", ROOT:" + subJsonString + "}";
		return jsonString;
	}
	/**
	 * 将数据系列化为表单数据填充所????Json格式
	 * @param pObject 待系列化的对??
	 * @param pFormatString 日期时间格式??如果为null则认为没有日期时间型字段
	 * @return
	 */
	public static String encodeDto2FormLoadJson(Map pDto, String pFormatString) {
		String jsonString = "";
		String sunJsonString = "";
		if (RHUtil.isEmpty(pFormatString)) {
			sunJsonString = encodeObject2Json(pDto);
		} else {
			sunJsonString = encodeObject2Json(pDto, pFormatString);
		}
		jsonString = "{success:"
				+ (RHUtil.isEmpty(pDto.get("success")) ? "true" : pDto.get("success")) + ",data:"
				+ sunJsonString + "}";
		if (log.isInfoEnabled()) {
			log.info("序列化后的JSON资料输出:\n" + jsonString);
		}
		return jsonString;
	}
	/**
	 * 将单??son对象解析为DTOJava对象
	 * @param jsonString ????的Json对象
	 * @return dto
	 */
	public static Map parseSingleJson2Map(String jsonString) {
		Map dto = new HashMap();
		if (RHUtil.isEmpty(jsonString)) {
			return dto;
		}
		JSONObject jb = JSONObject.fromObject(jsonString);
		dto = (HashMap) JSONObject.toBean(jb, HashMap.class);
		return dto;
	}
	/**
	 * 将复杂Json资料格式转换为List对象
	 * @param jsonString
	 *            复杂Json对象,格式必须符合如下契约<br>
	 *            {"1"\"{"name":"托尼.??,"age":"27"},
	 *            "2":{"name":"甄子??,"age":"72"}}
	 * @return List
	 */
	public static List parseJson2List(String jsonString) {
		List list = new ArrayList();
		JSONObject jbJsonObject = JSONObject.fromObject(jsonString);
		Iterator iterator = jbJsonObject.keySet().iterator();
		while (iterator.hasNext()) {
			Map dto = parseSingleJson2Map(jbJsonObject.getString(iterator.next().toString()));
			list.add(dto);
		}
		return list;
	}
	public static void main(String[] args) {
		String jsonStr = "{\"1\":{\"name\":\"托尼.jia\",\"age\":\"27\"}, \"2\":{\"name\":\"FAN\",\"age\":\"72\"}}";
		List list = parseJson2List(jsonStr);
		for (int i=0;i<list.size();i++) {
			Map map = (Map) list.get(i);
			System.out.println(map.get("name"));
			System.out.println(list.get(i));
		}
	}
	
}
