package com.wx.application.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.util.List;

/**
 *   @description : 转换json类
 *   ---------------------------------
 *   @author zj
 *   @since 2017-10-27
 */
public class JSONUtils {
	
	protected final static ValueFilter filter = new ValueFilter() {
		@Override
		public Object process(Object source, String name, Object value) {
			if (value == null)
				return null;
			return value;
		}
	};

	/**
	 * 将对象转换为JSON
	 * 
	 * @param o
	 * @param prettyFormat
	 * @return
	 */
	public static String c(Object o) {
		return JSON.toJSONString(o, filter);
	}
	
	/**
	 * 将对象转换为JSON
	 * 
	 * @param o
	 * @param prettyFormat
	 * @return
	 */
	public static String c(Object o, SerializerFeature... features) {
		return JSON.toJSONString(o, filter, features);
	}
	
	/**
	 * 将JSON转换为对象
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T p(String json, Class<T> clazz) {
		if (json == null || clazz == null) {
			return null;
		}
		return JSON.parseObject(json, clazz);
	}
	
	/**
	 * 将JSON转换为数组对象
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <E> List<E> pa(String json, Class<E> clazz) {
		if (json == null || clazz == null) {
			return null;
		}
		return JSON.parseArray(json, clazz);
	}

}
