import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/**
* @author - NingZhong.Zeng
* @version - 1.0.0.0
* @since - 2014-6-30 下午4:18:41
* 类说明 -
*/
public class JsonUtil {
private static JsonUtil mJosnUtil = null;
private ObjectMapper mapper = null;
public static JsonUtil getInstance() {
if(mJosnUtil == null){
synchronized (JsonUtil.class) {
if(mJosnUtil == null){
mJosnUtil = new JsonUtil();
mJosnUtil.mapper = new ObjectMapper();
}
}
}
return mJosnUtil;
}
public String writeJson(Object entity){
String str = "";
try {
str = mapper.disableDefaultTyping().writeValueAsString(entity);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public <T> T readJson(String jsonStr, Class<T> T){
T obj = null;
try {
obj = mapper.readValue(jsonStr, T);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return obj;
}
}
jackson 单例工具
最新推荐文章于 2024-06-27 23:15:00 发布