json串和java对象互相转换by jackson

复制代码
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class JacksonUtils {
    
    private final static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    }

    /**
     * 将对象转换成json字符串,用于将发送的报文打印出到日志
     */
    public static String toString(Object object) {

        String result = null;
        if (null == object)
            return result;
        try {
            result = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            System.out.println("对象转换成json字符串出错");
        }
        return result;
    }
    
    /**
     * 将json字符串转换成对象,用于将发送的报文打印出到日志
     * @return 
     */
    public static <T> T getObjectFromJsonString(String jsonStr, Class<T> responseType) {

        T result = null;
        if (null == jsonStr)
            return result;
        try {
         result = mapper.readValue(jsonStr, responseType);
      } catch (IOException e) {
           e.printStackTrace();
         System.out.println("json字符: " + jsonStr +" 串转换成对象出错");
      }
        return result;
    }
    
    /**
     * 从文件中读取json字符串,转换成对象列表
     * @param fileName
     * @param JsonObjectType
     * @return
     */
    public static <T> List<T> readJsonFromFile(String fileName, Class<T> JsonObjectType){
      List<T> result = new ArrayList<T>();
      try {
         //File file = new File("D:\\work\\raiyitest\\a\\a.txt");
         File file = new File(fileName);
         InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
         BufferedReader br = new BufferedReader(new FileReader(file));
         //BufferedReader br = new BufferedReader(isr);
         String data = null;
         while((data = br.readLine()) != null){
            T myRule = getObjectFromJsonString(data, JsonObjectType);
            result.add(myRule);
         }
         br.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
    

   /**
    * 将对象列表转换成json字符串,写入文件
    * @param fileName
    * @param jsonObjectList
    */
   public static <T> void WriteJsonObjectToFile(String fileName, List<T> jsonObjectList){
      try {
         //File file = new File("D:\\work\\raiyitest\\a\\a.txt");
         File file = new File(fileName);
           OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
           BufferedWriter bw = new BufferedWriter(osw);
         for(T jsonObject : jsonObjectList){
            String myJsonStr = toString(jsonObject);
            System.out.println(myJsonStr);
            bw.write(myJsonStr);
            bw.newLine();
         }
         bw.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
相关推荐
SamDeepThinking6 小时前
裁掉那个差程序员后,给你看团队里高手的代码:这个习惯,希望你有
java·后端·程序员
朕瞧着你甚好7 小时前
技术雷达 & Java 集成评估报告 — Apache Tika 3.3.1
java·ai编程
MacroZheng8 小时前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
SamDeepThinking8 小时前
函数式编程:用BiFunction消除多类型分支的代码重复
java·后端·面试
Flittly1 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了1 天前
Java 生成二维码解决方案
java·后端
人活一口气1 天前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP1 天前
Vibe Coding -- 完整项目案例实操
java
荣码1 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
SimonKing1 天前
Google第三方授权登录
java·后端·程序员