1 package com.ruoyi.common.core.utils.json;
2
3 import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 import com.fasterxml.jackson.annotation.JsonInclude;
5 import com.fasterxml.jackson.annotation.JsonTypeInfo;
6 import com.fasterxml.jackson.annotation.PropertyAccessor;
7 import com.fasterxml.jackson.core.JsonParser;
8 import com.fasterxml.jackson.core.JsonProcessingException;
9 import com.fasterxml.jackson.core.json.JsonReadFeature;
10 import com.fasterxml.jackson.core.type.TypeReference;
11 import com.fasterxml.jackson.databind.DeserializationFeature;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
15 import com.fasterxml.jackson.databind.node.ObjectNode;
16 import com.ruoyi.common.core.exception.BusinessException;
17 import com.ruoyi.common.core.exception.param.UtilException;
18 import lombok.extern.slf4j.Slf4j;
19
20 import java.text.SimpleDateFormat;
21 import java.util.List;
22 import java.util.Map;
23
24 /**
25 * @Description : json工具类
26 * @Author : wzkris
27 * @Version : V1.0.0
28 * @Date : 2023/1/2 8:37
29 */
30 @Slf4j
31 public class JsonUtils {
32
33
34 private static ObjectMapper MAPPER = new ObjectMapper();
35
36 static {
37 //序列化的时候序列对象的所有属性
38 MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
39 //取消默认转换timestamps形式
40 MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
41 //忽略空Bean转json的错误
42 MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
43 //所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
44 MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
45 //忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
46 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
47 //是否允许使用注释
48 MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
49 //允许转义字符
50 MAPPER.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
51 //不输出空值字段
52 MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
53 //定义识别码在反序列化时是否保留
54 MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
55 //指定java序列化类型,否则会按默认LinkedHashMap序列化
56 MAPPER.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
57 ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
58 }
59
60 private JsonUtils() {
61 }
62
63
64
65 public static ObjectMapper getMapper() {
66 return MAPPER;
67 }
68
69 /**
70 * 对象转Json格式字符串
71 *
72 * @param obj 对象
73 * @return Json格式字符串
74 */
75 public static String toJsonString(Object obj) {
76 try {
77 return MAPPER.writeValueAsString(obj);
78 } catch (Exception e) {
79 log.error("convert error, errorMsg:{}", e.getMessage(), e);
80 throw new BusinessException(e.getMessage());
81 }
82 }
83
84
85 /**
86 * 对象转字节
87 *
88 * @param obj 对象
89 * @return 字节流
90 */
91 public static byte[] toBytes(Object obj) {
92 try {
93 return MAPPER.writeValueAsBytes(obj);
94 } catch (JsonProcessingException e) {
95 log.error("convert error, errorMsg:{}", e.getMessage(), e);
96 throw new UtilException("JsonUtils工具类转换异常");
97 }
98 }
99
100
101 /**
102 * Json 转为 Jave Bean
103 *
104 * @param text json字符串
105 * @param clazz 对象类型class
106 * @param <T> 对象类型
107 * @return 对象类型
108 */
109 public static <T> T parse(String text, Class<T> clazz) {
110 try {
111 return MAPPER.readValue(text, clazz);
112 } catch (Exception e) {
113 log.error("convert error, errorMsg:{}", e.getMessage(), e);
114 throw new UtilException("JsonUtils工具类转换异常");
115 }
116 }
117
118
119 /**
120 * @param text json字符串
121 * @return 转为ObjectNode
122 */
123 public static ObjectNode parse(String text) {
124 try {
125 return MAPPER.readValue(text, ObjectNode.class);
126 } catch (JsonProcessingException e) {
127 log.error("convert error, errorMsg:{}", e.getMessage(), e);
128 throw new UtilException("JsonUtils工具类转换异常");
129 }
130 }
131
132 /**
133 * Json 转为 Map
134 *
135 * @param text json
136 * @param <K> key
137 * @param <V> value
138 * @return map
139 */
140 public static <K, V> Map<K, V> toMap(String text) {
141 try {
142 return MAPPER.convertValue(text, new TypeReference<Map<K, V>>() {
143 });
144 } catch (Exception e) {
145 log.error("convert error, errorMsg:{}", e.getMessage(), e);
146 throw new UtilException("JsonUtils工具类转换异常");
147 }
148 }
149
150
151 /**
152 * Json 转 List, Class 集合中泛型的类型,非集合本身
153 *
154 * @param text json
155 * @param <T> 对象类型
156 * @return List
157 */
158 public static <T> List<T> toList(String text) {
159 try {
160 return MAPPER.convertValue(text, new TypeReference<List<T>>() {
161 });
162 } catch (Exception e) {
163 log.error("convert error, errorMsg:{}", e.getMessage(), e);
164 throw new UtilException("JsonUtils工具类转换异常");
165 }
166 }
167
168 }

Jackson工具类及其配置的更多相关文章

  1. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  2. javaweb常用工具类及配置文件备份

    Javaweb常用工具类及配置文件备份   做一个代码备份,以后常用到的. hibernate工具类备份 package com.dly.service; /*  * hibernate获取sessi ...

  3. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  4. Jackson 工具类使用及配置指南

    目录 前言 Jackson使用工具类 Jackson配置属性 Jackson解析JSON数据 Jackson序列化Java对象 前言 Json数据格式这两年发展的很快,其声称相对XML格式有很对好处: ...

  5. Jackson工具类使用及配置指南、高性能配置(转)

    Jackson使用工具类 通常,我们对JSON格式的数据,只会进行解析和封装两种,也就是JSON字符串--->Java对象以及Java对象--->JSON字符串. public class ...

  6. jackson工具类 对象转字符串 -- 字符串转对象

    这个一个json的工具类.用的是jackson,当然还有谷歌的gosn,阿里的fastjson ,但是jackson的感觉还是最成熟(网上大神说的...) 实现的功能很简单,对象转字符串  字符串转简 ...

  7. springMVC+redis+redis自定义工具类 的配置

    1. maven项目,加入这一个依赖包即可, <dependency> <groupId>redis.clients</groupId> <artifactI ...

  8. 使用jackson工具类把对象或集合转为JSON格式

    jackson使用方法: 1.加入jar包: jackson-annotations-2.2.2.jar jackson-core-2.2.2.jar jackson-databind-2.2.2.j ...

  9. Jackson工具类(各种转换)

    首先要在项目中引入jackson的jar包(在此不做说明) 下面直接上代码 public class JacksonUtils { private final static ObjectMapper ...

  10. com.fasterxml.jackson工具类

    老版本的Jackson使用的包名为org.codehaus.jackson,而新版本使用的是com.fasterxml.jackson. Jackson主要包含了3个模块: jackson-core ...

随机推荐

  1. loj6851

    (CF1761D Tester Solution in Chinese) 定义 \(L(v)=\log_2\operatorname{lowbit}(v+1)\):也就是说,\(L(v)\) 是 \( ...

  2. Angualr动态加载组件

    <ng-container *ngComponentOutlet="customComponent"></ng-container>

  3. visual studio 2015 IOS开发连接mac时提示错误couldn't connect to xxxx, please try again的一个方法

    本人使用虚拟机MAC.原本使用虚拟机中的VS2015连接正常没有问题. 但是当把MAC的虚拟机文件COPY到另一个机器上,提示"couldn't connect to xxxx,  plea ...

  4. React 事件绑定this指向

    1. 推荐:使用class的实例方法 class Hello extends React.Component { handleClick = () => { this.setState({ .. ...

  5. golang 解决 socket: too many open files, 以及 too many open files

    同事写的一段代,码业务场景:需要多次GET请求一个三方服务的http 接口,获取数据后写入文件.发现有部分文件没有写入.查看日志出现了报错"socket: too many open fil ...

  6. python 处理CAD文件

    注意:如下的两个脚本都是我自己亲自试过的,可以运行得到结果!可以直接运行!可以直接运行! 网上的中文api能找到的不够全,工作中有用到部分功能,自己从中文英文资料里面找到一些,这里做一些总结. 画图a ...

  7. FFmpeg 命令行

    FFmpeg命令行帮助 #>ffmpeg -h #>ffmpeg -h long #>ffmpeg -h full 将视频按照指定的宽高输出 #>ffmpeg -i input ...

  8. 网络编程之 urllib 模块

    首先urlib并不是一个很好用的方法,这里仅作简单介绍.一般我们用requests方法来代替urlib方法. 1. get请求 1 from urllib import request 2 url = ...

  9. [419] C1 Harbingers Of War OpCodez

    [419] C1 Harbingers Of War Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 Request ...

  10. Mysql语句练习

    某宾馆其关系模式如下:Room(房间编号,房间类型,价格)Customer(顾客编号,顾客姓名,年龄,电话)RC(房间号,顾客编号,入住日期,入住天数) 1 create database Hotel ...