欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Java后端序列化工具 Jackson 和 FastJSON

Java后端序列化工具 Jackson 和 FastJSON

2025/4/22 21:53:12 来源:https://blog.csdn.net/m0_73837751/article/details/146206543  浏览:    关键词:Java后端序列化工具 Jackson 和 FastJSON

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 进行对象转换:

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 对象)

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,可以进行异常处理:

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 和泛型:

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)

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 对象)

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

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

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 简单,适合大数据量处理

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词