当前位置: 首页 > news >正文

jackson常用操作

#jackson常用操作

jackson序列化框架,一些常用的操作

依赖

		<!--Jackson包--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.15.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.15.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.15.2</version></dependency>

工具类

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;public class JsonUtils {public static final ObjectMapper OBJECT_MAPPER = createObjectMapper();/*** 初始化ObjectMapper** @return*/private static ObjectMapper createObjectMapper() {ObjectMapper objectMapper = new ObjectMapper();objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);//        objectMapper.registerModule(new Hibernate4Module().enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING));objectMapper.registerModule(new JavaTimeModule());return objectMapper;}public static String object2Json(Object o) {StringWriter sw = new StringWriter();JsonGenerator gen = null;try {gen = new JsonFactory().createGenerator(sw);OBJECT_MAPPER.writeValue(gen, o);} catch (IOException e) {throw new RuntimeException("不能序列化对象为Json", e);} finally {if (null != gen) {try {gen.close();} catch (IOException e) {throw new RuntimeException("不能序列化对象为Json", e);}}}return sw.toString();}public static Map<String, Object> object2Map(Object o) {return OBJECT_MAPPER.convertValue(o, Map.class);}/*** 将 json 字段串转换为 对象.** @param json  字符串* @param clazz 需要转换为的类* @return*/public static <T> T json2Object(String json, Class<T> clazz) {try {return OBJECT_MAPPER.readValue(json, clazz);} catch (IOException e) {throw new RuntimeException("将 Json 转换为对象时异常,数据是:" + json, e);}}/*** 将 json 字段串转换为 List.** @param json* @param clazz* @param <T>* @return* @throws IOException*/public static <T> List<T> json2List(String json, Class<T> clazz) {JavaType type = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);try {return OBJECT_MAPPER.readValue(json, type);} catch (JsonProcessingException e) {throw new RuntimeException("将 Json 转换为List时异常,数据是:" + json, e);}}/*** 将 json 字段串转换为 数据.** @param json* @param clazz* @param <T>* @return* @throws IOException*/public static <T> T[] json2Array(String json, Class<T[]> clazz) {try {return OBJECT_MAPPER.readValue(json, clazz);} catch (JsonProcessingException e) {throw new RuntimeException("将 Json 转换为数组时异常,数据是:" + json, e);}}public static <T> T node2Object(JsonNode jsonNode, Class<T> clazz) {try {T t = OBJECT_MAPPER.treeToValue(jsonNode, clazz);return t;} catch (JsonProcessingException e) {throw new RuntimeException("将 Json 转换为对象时异常,数据是:" + jsonNode.toString(), e);}}public static JsonNode object2Node(Object o) {try {if (o == null) {return OBJECT_MAPPER.createObjectNode();} else {return OBJECT_MAPPER.convertValue(o, JsonNode.class);}} catch (Exception e) {throw new RuntimeException("不能序列化对象为Json", e);}}}
http://www.lryc.cn/news/153224.html

相关文章:

  • ios ipa包上传需要什么工具
  • 科目1基础知识快速入门精简
  • 安卓逆向 - 某东app加密参数还原
  • Visual Studio(2022)生成链接过程的.map映射文件以及.map映射文件的内容说明
  • A. Gift Carpet
  • 技术科普:汽车开放系统架构AUTOSAR
  • 说说HTTP 和 HTTPS 有什么区别?
  • Pygame中Trivia游戏解析6-5
  • Java8新特性2——方法引用
  • Mac“其他文件”存放着什么?“其他文件”的清理方法
  • 46、TCP的“三次握手”
  • libudev 和 libusb 常见API分析
  • [dasctf]misc04
  • Scala的函数式编程与高阶函数,匿名函数,偏函数,函数的闭包、柯里化,抽象控制,懒加载等
  • Axure RP 8.1.0.3400(原型设计工具)
  • 企业微信、飞书、钉钉机器人消息发送工具类
  • 手撕 视觉slam14讲 ch7 / pose_estimation_3d2d.cpp (1)
  • Mac安装Dart时,Homebrew报错 Error: Failure while executing
  • SSM整合~
  • Self-supervised 3D Human Pose Estimation from a Single Image
  • ubuntu下cups部分场景
  • 通过geoserver imageMosic发布多张tif数据
  • 输出图元(四)8-2 OpenGL画点函数、OpenGL画线函数
  • java八股文
  • 算法通关村——解析堆的应用
  • 爬虫源码---爬取小猫猫交易网站
  • Python的由来和基础语法(一)
  • 使用maven创建springboot项目
  • MySQL 基本操作1
  • linux内网yum源服务器搭建