JacksonUtil.java 3.65 KB
package com.sitech.ismp.util;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

import java.lang.reflect.Type;
import java.util.*;

/**
 * 
 * <p>
 * Title: JacksonUtil
 * </p>
 * <p>
 * Description: Jackson工具类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2013
 * </p>
 * <p>
 * Company: SI-TECH
 * </p>
 * 
 * @author huojla
 * @version 1.0
 * @createtime 2013-5-30 下午1:13:29
 * 
 */
public class JacksonUtil {
	private static final ObjectMapper mapper = new ObjectMapper();

	static {
		mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		// mapper.getDeserializationConfig().without(
		// DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
		mapper.getDeserializationConfig().set(
				DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	@SuppressWarnings("rawtypes")
	public static class TypeReference<T extends Object> extends
			org.codehaus.jackson.type.TypeReference {
		public int compareTo(Object o) {
			return 0;
		}

		public Type getTypeReference() {
			return this.getType();
		}
	}

	/**
	 * 
	 * @Title: toJson
	 * @Description: 将对象转换为Json字符串
	 * @param
	 * @return String
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-4-28 ����4:47:44
	 */
	public static String toJson(Object obj) {
		try {
			if (obj == null) {
				return null;
			} else {
				return mapper.writeValueAsString(obj);
			}
		} catch (Exception e) {
			throw new RuntimeException("转换Json异常!" + obj.getClass().getName(), e);
		}
	}

	/**
	 * 
	 * @Title: toObject
	 * @Description: 转换Json to 对象
	 * @param
	 * @return Object
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-4-28 ����4:54:25
	 */
	public static Object toObject(String json) {
		return fromJson(json, Object.class);
	}

	/**
	 * 
	 * @Title: fromJson
	 * @Description: 转换json to Java对象
	 * @param
	 * @return T
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-4-28 ����4:51:42
	 */
	public static <T> T fromJson(String json, Class<T> type) {
		try {
			if (json == null) {
				return null;
			} else {
				return mapper.readValue(json, type);
			}
		} catch (Exception e) {
			throw new RuntimeException(json + "解析Json异常!" + type.getName(), e);
		}
	}

	/**
	 * 
	 * @Title: fromJSON
	 * @Description: 转换复杂对象
	 * @param
	 * @return T
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-4-28 ����4:53:39
	 */
	public static <T> T fromJSON(String json, JacksonUtil.TypeReference<T> typeReference) {
		try {
			if (json == null) {
				return null;
			} else {
				return (T) mapper.readValue(json, typeReference);
			}
		} catch (Exception ex) {
			throw new RuntimeException(json + "解析Json异常!", ex);
		}
	}

	/**
	 * @Title: JSONToMap
	 * @Description: 将json字符串转换成Map
	 * @param
	 * @return void
	 * @throws
	 * @author lipengpeng
	 * @version 1.0
	 * @createtime 2013-8-15 下午9:13:57
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> JSONToMap(String jsonstr) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<LinkedHashMap<String, Object>> list = new ArrayList<LinkedHashMap<String, Object>>();
		try {
			list = mapper.readValue(jsonstr, List.class);
			for (LinkedHashMap<String, Object> linkedHashMap : list) {
				for (String key : linkedHashMap.keySet()) {
					map.put(key, linkedHashMap.get(key));
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(jsonstr + "解析Json异常!", e);
		}
		return map;
	}
}