package net.csdn.blog.chaijunkun.util;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
public class JSONUtil {
private static Logger logger= Logger.getLogger(JSONUtil.class);
private static ObjectMapper objectMapper;
/**
* 懒惰单例模式得到ObjectMapper实例
* 此对象为Jackson的核心
*/
private static ObjectMapper getMapper(){
if (objectMapper== null){
objectMapper= new ObjectMapper();
//当找不到对应的序列化器时 忽略此字段
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
//使Jackson JSON支持Unicode编码非ASCII字符
CustomSerializerFactory serializerFactory= new CustomSerializerFactory();
serializerFactory.addSpecificMapping(String.class, new StringUnicodeSerializer());
objectMapper.setSerializerFactory(serializerFactory);
//支持结束
}
return objectMapper;
}
/**
* 创建JSON处理器的静态方法
* @param content JSON字符串
* @return
*/
private static JsonParser getParser(String content){
try{
return getMapper().getJsonFactory().createJsonParser(content);
}catch (IOException ioe){
return null;
}
}
/**
* 创建JSON生成器的静态方法, 使用标准输出
* @return
*/
private static JsonGenerator getGenerator(StringWriter sw){
try{
return getMapper().getJsonFactory().createJsonGenerator(sw);
}catch (IOException e) {
return null;
}
}
/**
* JSON对象序列化
*/
public static String toJSON(Object obj){
StringWriter sw= new StringWriter();
JsonGenerator jsonGen= getGenerator(sw);
if (jsonGen== null){
try {
sw.close();
} catch (IOException e) {
}
return null;
}
try {
//由于在getGenerator方法中指定了OutputStream为sw
//因此调用writeObject会将数据输出到sw
jsonGen.writeObject(obj);
//由于采用流式输出 在输出完毕后务必清空缓冲区并关闭输出流
jsonGen.flush();
jsonGen.close();
return sw.toString();
} catch (JsonGenerationException jge) {
logger.error("JSON生成错误" + jge.getMessage());
} catch (IOException ioe) {
logger.error("JSON输入输出错误" + ioe.getMessage());
}
return null;
}
/**
* JSON对象反序列化
*/
public static <T> T fromJSON(String json, Class<T> clazz) {
try {
JsonParser jp= getParser(json);
return jp.readValueAs(clazz);
} catch (JsonParseException jpe){
logger.error(String.format("反序列化失败, 错误原因:%s", jpe.getMessage()));
} catch (JsonMappingException jme){
logger.error(String.format("反序列化失败, 错误原因:%s", jme.getMessage()));
} catch (IOException ioe){
logger.error(String.format("反序列化失败, 错误原因:%s", ioe.getMessage()));
}
return null;
}
}

Jack_Chai
- 粉丝: 576
最新资源
- 科技活动服务公司如何借助AI+数智应用助力活动组织方提升工作效率?.docx
- 科技活动中如何通过AI+数智应用高效匹配技术供需双方资源?.docx
- 科技平台面临资源、服务、可持续性三大挑战,如何通过AI+数智应用解决?.docx
- 科技平台如何借助AI+数智应用解决资源整合难题?.docx
- 科技平台如何借助AI+数智应用提升服务专业性并降低成本?.docx
- 科技平台如何借助AI+数智应用提升服务专业性和竞争力?.docx
- 面对科技管理系统的局限性,AI+数智应用驱动的全流程创新咨询能带来哪些突破?.docx
- 企业科技管理如何借助AI+数智应用突破传统模式,实现智能化升级?.docx
- 全流程创新服务在科技管理系统中如何借助AI+数智应用实现智能化管理提升?.docx
- 企业作为技术需求方,如何借助AI+数智应用在科技活动中精准找到适配的技术资源?.docx
- 全流程创新服务如何通过AI+数智应用为科技管理工作创造深层次价值?.docx
- 全流程创新管理下,科技管理如何借助AI+数智应用满足个性化需求?.docx
- 全流程创新诊断在科技管理领域有什么作用,能借助AI+数智应用解决现有系统的哪些问题?.docx
- 全流程创新咨询如何借助AI+数智应用实现科技管理的智能化升级?.docx
- 如何借助AI+数智应用保障科技平台的可持续发展与市场化运营?.docx
- 如何借助AI+数智应用技术提升科技管理工作的效率与创新?.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
前往页