Java后端序列化工具 Jackson 和 FastJSON

server/2025/3/15 1:32:41/

1. Jackson(Spring Boot 默认支持,无需额外依赖)

1.1 添加依赖(如果使用 Spring Boot,默认已有,无需添加)

如果你不是 Spring Boot 项目,需要手动添加 Jackson 依赖:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.15.0</version>
</dependency>

1.2 Jackson - 序列化(Java 对象 → JSON)

使用 ObjectMapper 进行对象转换:

java">import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;public class JacksonExample {public static void main(String[] args) {ObjectMapper objectMapper = new ObjectMapper();// 创建一个 Java 对象User user = new User(1, "张三", 25, "developer");try {// Java 对象序列化为 JSONString jsonStr = objectMapper.writeValueAsString(user);System.out.println("序列化后的 JSON: " + jsonStr);} catch (JsonProcessingException e) {e.printStackTrace();System.err.println("JSON 序列化失败:" + e.getMessage());}}
}

1.3 Jackson - 反序列化(JSON → Java 对象)

java">import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;public class JacksonExample {public static void main(String[] args) {ObjectMapper objectMapper = new ObjectMapper();// JSON 字符串String jsonStr = "{\"id\":1,\"name\":\"张三\",\"age\":25,\"job\":\"developer\"}";try {// JSON 反序列化为 Java 对象User user = objectMapper.readValue(jsonStr, User.class);System.out.println("反序列化后的对象: " + user);} catch (JsonProcessingException e) {e.printStackTrace();System.err.println("JSON 反序列化失败:" + e.getMessage());}}
}

1.4 Jackson - 处理 JSON 转换异常

Jackson 会抛出 JsonProcessingException,可以进行异常处理:

java">try {String invalidJson = "{\"id\":1,\"name\":\"张三\",\"age\":\"invalid\",\"job\":\"developer\"}";User user = objectMapper.readValue(invalidJson, User.class);
} catch (JsonProcessingException e) {System.err.println("JSON 解析错误:" + e.getMessage());
}

1.5 Jackson - 复杂类型(Map、List、泛型)

Jackson 还可以解析 MapList 和泛型:

java">import com.fasterxml.jackson.core.type.TypeReference;String jsonList = "[{\"id\":1,\"name\":\"张三\"},{\"id\":2,\"name\":\"李四\"}]";// 反序列化为 List
List<User> userList = objectMapper.readValue(jsonList, new TypeReference<List<User>>() {});
System.out.println(userList);

2. FastJSON 2.x(高性能 JSON 解析)

2.1 添加依赖

FastJSON 1.x 有安全漏洞,推荐使用 FastJSON 2.x

<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.39</version>
</dependency>

2.2 FastJSON - 序列化(Java 对象 → JSON)

java">import com.alibaba.fastjson2.JSON;public class FastJsonExample {public static void main(String[] args) {// 创建 Java 对象User user = new User(1, "张三", 25, "developer");// FastJSON 序列化String jsonStr = JSON.toJSONString(user);System.out.println("FastJSON 序列化后的 JSON: " + jsonStr);}
}

2.3 FastJSON - 反序列化(JSON → Java 对象)

java">import com.alibaba.fastjson2.JSON;public class FastJsonExample {public static void main(String[] args) {// JSON 字符串String jsonStr = "{\"id\":1,\"name\":\"张三\",\"age\":25,\"job\":\"developer\"}";// FastJSON 反序列化User user = JSON.parseObject(jsonStr, User.class);System.out.println("FastJSON 反序列化后的对象: " + user);}
}

2.4 FastJSON - 处理 JSON 转换异常

FastJSON 2.x 提供了 JSON.parseObject(json, clazz),但如果数据格式错误,会抛出 JSONException

java">try {String invalidJson = "{\"id\":1,\"name\":\"张三\",\"age\":\"invalid\",\"job\":\"developer\"}";User user = JSON.parseObject(invalidJson, User.class);
} catch (Exception e) {System.err.println("FastJSON 解析错误:" + e.getMessage());
}

2.5 FastJSON - 复杂类型(Map、List、泛型)

FastJSON 也可以解析 MapList

java">import com.alibaba.fastjson2.TypeReference;// JSON 数组
String jsonList = "[{\"id\":1,\"name\":\"张三\"},{\"id\":2,\"name\":\"李四\"}]";// 反序列化为 List
List<User> userList = JSON.parseObject(jsonList, new TypeReference<List<User>>() {});
System.out.println(userList);

3. Jackson vs FastJSON 对比

特性Jackson(Spring 默认)FastJSON 2.x(高性能)
序列化性能更快(FastJSON 2.x 进行了优化)
反序列化性能更快
Spring Boot 兼容性默认支持需要额外引入
安全性更安全FastJSON 1.x 有漏洞,2.x 已修复
特性功能丰富,支持 JSON Schema、XML、YAMLAPI 简单,适合大数据量处理

http://www.ppmy.cn/server/175026.html

相关文章

Java Web大文件下载:从卡顿到丝滑的优化之旅

文章目录 Java Web大文件下载&#xff1a;从卡顿到丝滑的优化之旅一、引言二、优化前的困境&#xff08;一&#xff09;性能瓶颈初现&#xff08;二&#xff09;内存之殇&#xff08;三&#xff09;网络拥堵&#xff08;四&#xff09;代码示例&#xff1a;基本下载实现 三、优…

正则表达式(复习)

文章目录 一、[]: 一个字符集合二、{}: 重复次数三、特殊符号四、(): 分组五、python代码示例六、注意 正则表达式(regular expression)描述了一种字符串匹配的模式&#xff08;pattern&#xff09;&#xff0c;可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个…

计算机操作系统(一) 什么是操作系统

计算机操作系统&#xff08;一&#xff09; 什么是操作系统 前言一、什么是操作系统二、操作系统的作用三、推动操作系统发展的主要动力总结&#xff08;核心概念速记&#xff09;&#xff1a; 前言 当你打开电脑、点击应用、播放音乐时&#xff0c;是谁在背后默默协调这一切&…

css实现标题跑马灯效果

css实现标题跑马灯效果 <div class"topBar"><span class"scrolling-text">滚动字幕</span></div>keyframes marquee {0% {transform: translateX(300%);}100% {transform: translateX(-300%);} }.topBar {width:100%;height: 45px…

系统架构设计师-第6章 系统配置与性能评价

【本章学习建议】 根据考试大纲&#xff0c;本章主要考查系统架构设计师单选题&#xff0c;预计考1分左右&#xff0c;对应第二版教材2.9节&#xff0c;内容较少&#xff0c;较为简单&#xff0c;容易拿分。 6.1 性能指标 1. 计算机的性能指标 对计算机评价的主要性能指标有…

Spring MVC中的Controller加载控制与Bean加载控制详解

Spring MVC默认通过父子容器实现Web层与非Web组件的隔离。但在实际项目中&#xff0c;若未明确控制组件的扫描路径与加载规则&#xff0c;表现层的Controller、业务层的Service与数据层的Repository往往会被“一刀切”地扫描到同一上下文中。例如&#xff0c;业务层的Service被…

RocketMQ开发实战篇

一、生产者开发指南 1. Java API使用详解 在使用RocketMQ进行消息生产时&#xff0c;首先需要引入相关的依赖。在Maven项目中&#xff0c;可以在pom.xml文件中添加以下依赖&#xff1a; <dependency><groupId>org.apache.rocketmq</groupId><artifactI…

PHP与数据库连接常见问题及解决办法

PHP与数据库连接常见问题及解决办法 在现代Web开发中&#xff0c;PHP与数据库的连接是不可或缺的一部分。无论是构建动态网站、内容管理系统&#xff08;CMS&#xff09;还是电子商务平台&#xff0c;PHP与数据库的交互都是核心功能之一。然而&#xff0c;在实际开发过程中&am…