标签:
/* * 序列化帮助类 */ public class SerializeHelper { private static ObjectMapper objectMapper = new ObjectMapper(); /** * 将对象序列化为JSON字符串 * * @param object * @return JSON字符串 * @throws IOException * @throws JsonMappingException * @throws JsonGenerationException */ public String serialize(Object object) throws JsonGenerationException, JsonMappingException, IOException { Writer write = new StringWriter(); objectMapper.writeValue(write, object); return write.toString(); } /** * 将JSON字符串反序列化为对象 * * @param object * @return JSON字符串 * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public <T> T deserialize (String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { Object object = objectMapper.readValue(json, TypeFactory.rawClass(clazz)); return (T) object; } }
标签:
原文地址:http://www.cnblogs.com/xinziyublog/p/5647812.html