栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于Jackson的JSON工具类封装 JsonUtils用法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于Jackson的JSON工具类封装 JsonUtils用法

直接上代码,都有注释,一看就懂,完全满足日常开发需求

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.text.SimpleDateFormat;
import lombok.extern.slf4j.Slf4j;
 

@Slf4j
public class JsonUtils { 
  private static ObjectMapper om = new ObjectMapper(); 
  static {
 
    // 对象的所有字段全部列入,还是其他的选项,可以忽略null等
    om.setSerializationInclusion(Include.ALWAYS);
    // 设置Date类型的序列化及反序列化格式
    om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
 
    // 忽略空Bean转json的错误
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    // 忽略未知属性,防止json字符串中存在,java对象中不存在对应属性的情况出现错误
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
    // 注册一个时间序列化及反序列化的处理模块,用于解决jdk8中localDateTime等的序列化问题
    om.registerModule(new JavaTimeModule());
  }
 
  
  public static  String toJson(T obj) {
 
    String json = null;
    if (obj != null) {
      try {
 json = om.writevalueAsString(obj);
      } catch (JsonProcessingException e) {
 log.warn(e.getMessage(), e);
 throw new IllegalArgumentException(e.getMessage());
      }
    }
    return json;
  } 
  
  public static  T parse(String json, Class clazz) { 
    return parse(json, clazz, null);
  }
 
  
  public static  T parse(String json, TypeReference type) {
 
    return parse(json, null, type);
 }
 
  
  private static  T parse(String json, Class clazz, TypeReference type) {
 
    T obj = null;
    if (!StringUtils.isEmpty(json)) {
      try {
 if (clazz != null) {
   obj = om.readValue(json, clazz);
 } else {
   obj = om.readValue(json, type);
 }
      } catch (IOException e) {
 log.warn(e.getMessage(), e);
 throw new IllegalArgumentException(e.getMessage());
      }
    }
    return obj;
  }
}

如何使用就更简单了

public static void main(String[] args) {
  TestModel model1 = new TestModel();
  model1.setId("A");
  model1.setDate1(new Date());
  model1.setDate2(LocalDate.now());
  model1.setDate3(LocalDateTime.now());
  model1.setTime(LocalTime.now());
 
  TestModel2 model2 = new TestModel2<>();
  model2.setId("PA");
  model2.setSub(model1);
 
  String json1 = JsonUtils.toJson(model1);
  String json2 = JsonUtils.toJson(model2);
 
  System.out.println(json1);
  System.out.println(json2);
 
  // 简单对象可以用这个
  TestModel obj1 = JsonUtils.parse(json1, TestModel.class);
  TestModel2 obj2 = JsonUtils.parse(json2, TestModel2.class);
  // 需要准确泛型的复杂对象可以用这个,这种方式与上面有细微差别,读者可以自行debug研究
  TestModel2 obj3 = JsonUtils
    .parse(json2, new TypeReference>() {
    });
 
  System.out.println(obj1);
  System.out.println(obj2);
  System.out.println(obj3);
}

程序输出:

GitHub地址:https://github.com/ye17186/spring-boot-learn

以上这篇关于Jackson的JSON工具类封装 JsonUtils用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号