compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime; @Slf4j
public class Json { private static ObjectMapper MAPPER; static {
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() { @Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
gen.writeNumber(value.toInstant().toEpochMilli());
}
});
javaTimeModule.addDeserializer(ZonedDateTime.class, new JsonDeserializer<ZonedDateTime>() {
@Override
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(p.readValueAs(Long.class)), ZoneId.systemDefault());
}
});
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
objectMapper.registerModule(javaTimeModule);
MAPPER = objectMapper;
} public static ObjectMapper getMapper() {
return MAPPER;
} @SneakyThrows
public static String toJson(Object object) {
return MAPPER.writeValueAsString(object);
} @SneakyThrows
public static <T> T toObject(String json, Class<T> clazz) {
return MAPPER.readValue(json, clazz);
} public static <T> T toObject(Object object, Class<T> clazz) {
return MAPPER.convertValue(object, clazz);
} @SneakyThrows
public static <T> T toObject(String json, TypeReference<T> type) {
return MAPPER.readValue(json, type);
} public static <T> T toObject(Object object, TypeReference<T> type) {
return MAPPER.convertValue(object, type);
} }

最新文章

  1. 设置NotePad++设置&quot;不打开上次关闭的文件&quot;
  2. CSS基本知识5-CSS对齐
  3. BZOJ 3156 防御准备
  4. NSISの堆栈操作
  5. PDT已有很大改进
  6. Unity3D开发之NGUI点击事件穿透响应处理
  7. weblogic 日志介绍
  8. ueditor的过滤、转义、格式丢失问题
  9. Delphi 写日志的类
  10. C++学习笔记29,引用变量(1)
  11. Python自动化开发-基础语法
  12. Integer的自动拆箱
  13. leetcode每日刷题计划-简单篇day7
  14. 第8章 IO库 自我综合练习
  15. PS提亮户外儿童照
  16. Confluence 6 数据库和临时目录
  17. Docker Nginx 配置多个子域名
  18. centos7安装部署mysql5.7服务器
  19. QtGUI Module&#39;s Classes
  20. flask学习(五):使用配置文件

热门文章

  1. Java 中,受检查异常 和 不受检查异常的区别?
  2. 2.安装Spark与Python练习
  3. PCB模块化布局系列之时钟电路设计(晶振、晶体)
  4. Codepen 每日精选(2018-4-4)
  5. webstrom Debug 调试vue项目
  6. Javascript Symbol 隐匿的未来之星
  7. java中throws子句是怎么用的?工作原理是什么
  8. 人机交互BS
  9. 第一阶段:Java基础之异常和处理
  10. c语言实现循环单链表