zoukankan      html  css  js  c++  java
  • com.fasterxml.jackson工具类

    老版本的Jackson使用的包名为org.codehaus.jackson,而新版本使用的是com.fasterxml.jackson

    Jackson主要包含了3个模块:

    • jackson-core
    • jackson-annotations
    • jackson-databind
      其中,jackson-annotations依赖于jackson-core,jackson-databind又依赖于jackson-annotations。

    Jackson有三种方式处理Json:

    1. 使用底层的基于Stream的方式对Json的每一个小的组成部分进行控制
    2. 使用Tree Model,通过JsonNode处理单个Json节点
    3. 使用databind模块,直接对Java对象进行序列化和反序列化

    通常来说,我们在日常开发中使用的是第3种方式,有时为了简便也会使用第2种方式,比如你要从一个很大的Json对象中只读取那么一两个字段的时候,采用databind方式显得有些重,JsonNode反而更简单。

    package founder.util;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    
    /**
    * @ClassName: JsonUtils
    * @author hanwl
    * @date 2019年01月22日
    * @Description: TODO
    */
    public class JsonUtils {
    
        private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    
        /**
         * Json格式的字符串向JavaBean转换,传入空串将返回null
         * @param strBody Json格式的字符串
         * @param c 目标JavaBean类型
         * @return JavaBean对象
         * @throws JsonParseException
         * @throws JsonMappingException
         * @throws IOException
         */
        public static <T> T json2Object(String strBody, Class<T> c) throws JsonParseException, JsonMappingException, IOException{
            if (strBody == null || "".equals(strBody)) {
                return null;
            }
            else {
                return OBJECT_MAPPER.readValue(strBody, c);
            }
        }
    
        /**
         * Json格式的字符串向JavaBean转换,传入空串将返回null
         * @param strBody Json格式的字符串
         * @param c 目标JavaBean类型
         * @return JavaBean对象, 如果解析失败返回 null
         */
        public static <T> T decodeJson(String strBody, Class<T> c) {
            if (strBody == null || "".equals(strBody)) {
                return null;
            }
            else {
                try {
                    return OBJECT_MAPPER.readValue(strBody, c);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
    
        /**
         *
         * @param strBody
         * @param c
         * @return
         * @throws JsonParseException
         * @throws JsonMappingException
         * @throws IOException
         */
        public static Object json2ComplexObject(String strBody) throws JsonParseException, JsonMappingException, IOException{
            if (strBody == null || "".equals(strBody)) {
                return null;
            }
            else {
                // 每个属性的实际类型是string
                return OBJECT_MAPPER.readValue(strBody, Object.class);
            }
        }
    
        /**
         * Json格式的字符串向JavaBean List集合转换,传入空串将返回null
         * @param strBody
         * @param c
         * @return
         * @throws JsonParseException
         * @throws JsonMappingException
         * @throws IOException
         */
        @SuppressWarnings("unchecked")
        public static <T> List<T> json2ObjectList(String strBody,Class<T> c) throws JsonParseException, JsonMappingException, IOException{
            if (strBody == null || "".equals(strBody)) {
                return null;
            }
            else {
                JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c);
                return (List<T>) OBJECT_MAPPER.readValue(strBody, javaType);
            }
        }
    
        /**
         * Json格式的字符串向JavaBean List集合转换,传入空串将返回null
         * @param strBody
         * @param c
         * @return 对象列表,解析失败返回 null
         */
        @SuppressWarnings("unchecked")
        public static <T> List<T> decodeJsonToList(String strBody,Class<T> c) {
            if (strBody == null || "".equals(strBody)) {
                return null;
            }
            else {
                JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c);
                try {
                    return (List<T>) OBJECT_MAPPER.readValue(strBody, javaType);
                } catch (IOException e) {
                    e.printStackTrace();
    
                    return null;
                }
            }
        }
    
        /**
         * Json格式的字符串向List<String>集合转换,传入空串将返回null
         * @param strBody
         * @return
         * @throws JsonParseException
         * @throws JsonMappingException
         * @throws IOException
         */
        public static List<String> json2List(String strBody) throws JsonParseException, JsonMappingException, IOException{
            return json2ObjectList(strBody, String.class);
        }
    
        /**
         * Object转为Json格式字符串的方法
         * @param o
         * @return
         * @throws JsonProcessingException
         */
        public static String object2Json(Object o) throws JsonProcessingException{
            return OBJECT_MAPPER.writeValueAsString(o);
        }
            
        /**
         * Object转为Json格式字符串的方法
         * @param o
         * @return 对象的json字符串,如果处理过程中出错,返回null
         */
        public static String encodeObject(Object o) {
            try {
                return OBJECT_MAPPER.writeValueAsString(o);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
  • 相关阅读:
    MySQL 的连接时长控制--interactive_timeout和wait_timeout
    查看MySQL 连接信息--连接空闲时间及正在执行的SQL
    mysql timestamp为0值时,python读取后的对象为None
    MySQL基础普及《MySQL管理之道:性能调优、高可用与监控》
    读《大秦帝国》第三部
    golang mysql 如何设置最大连接数和最大空闲连接数
    如何查看MySQL connection id连接id
    JAVA配置环境变量
    PB常见功能实现代码
    PB中数据窗口自动换行
  • 原文地址:https://www.cnblogs.com/loong-hon/p/10315000.html
Copyright © 2011-2022 走看看