package com.taiping.facility.tool; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import javax.xml.bind.JAXBException; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class JsonTool { public static String toJson(Object target,String format) { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat(format)); try { return mapper.writeValueAsString(target); } catch (JsonProcessingException e) { LogTool.error(JsonTool.class, e); } catch (IOException e) { LogTool.error(JsonTool.class, e); } return null; } public static String toJson(Object target) { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); try { return mapper.writeValueAsString(target); } catch (JsonProcessingException e) { LogTool.error(JsonTool.class, e); } catch (IOException e) { LogTool.error(JsonTool.class, e); } return null; } /** * @param json * @param clazz * @param dateFormat "yyyy-MM-dd HH:mm:ss" * @return */ @SuppressWarnings("unchecked") public static <T> T toObject(String json, Class<?> clazz,String dateFormat) { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat(dateFormat)); try { return (T) mapper.readValue(json, clazz); } catch (Exception e) { LogTool.error(JsonTool.class, e); } return null; } @SuppressWarnings("unchecked") public static <T> T toObject(String json, Class<?> clazz) { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); try { return (T) mapper.readValue(json, clazz); } catch (Exception e) { LogTool.error(JsonTool.class, e); } return null; } public static JsonNode readValue(String json,String nodeName) throws JsonProcessingException, IOException{ ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(json); return rootNode.path(nodeName); } /** * 读取文件内容字符串 * @param path 文件路径 * @param encode 编码 * @return 文件内容 */ public static String readFile(String path,String encode){ String content = ""; InputStream inputStream = null; try { inputStream = new FileInputStream(new File(path)); content = new String(read(inputStream),encode); } catch (FileNotFoundException e) { LogTool.error(JsonTool.class, e); } catch (UnsupportedEncodingException e) { System.out.println("UnsupportedEncodingException encode:"+encode+"\n"); LogTool.error(JsonTool.class, e); } catch (Exception e) { LogTool.error(JsonTool.class, e); }finally{ if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { LogTool.error(JsonTool.class, e); } } } return content; } public static byte[] read(InputStream in) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (in != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } out.close(); in.close(); return out.toByteArray(); } return null; } }
Java工具类之JsonTool
最新推荐文章于 2024-03-14 11:46:04 发布