JackJsonUtilsツールクラス

1100 ワード

package com.abc.itv.read.utils;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils {

	public static final ObjectMapper mapper = new ObjectMapper();

	/**
	 *       json   
	 * 
	 * @param obj
	 * @return
	 */
	public static String writeValue(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 *  json          
	 * 
	 * @param 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static  T readValue(String json, Class clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
}