Java工具類之JsonTool

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;
   }
   
 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章