Jackson使用指南
本文主要介绍jackson的基本使用方法
1.Jackson简单序列化与反序列化
- 首先创建一个简单的类
@Setter
@Getter
public class Student {
private String name;
private int age;
}
- 创建一个jackson的objectMapper对象用来序列化
import com.fasterxml.jackson.databind.ObjectMapper;
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
}
- 将一个student对象序列化为字符串
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
Student student=new Student();
student.setName("abc");
student.setAge(18);
String s = objectMapper.writeValueAsString(student);
System.out.println(s);
}
得到以下输出:
{"name":"abc","age":18}
- 将字符串转换为student对象
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
String s = "{\"name\":\"abc\",\"age\":18}";
Student student = objectMapper.readValue(s, Student.class);
System.out.println(student);
}
得到输出:
Student(name=abc, age=18)
2.序列化和反序列化列表对象
- 创建一个包含列表的类
@Setter
@Getter
public class ClassRoom {
List<Student> studentList;
}
- 序列化
public static void main(String[] args) throws IOException{
List<Student> list=new ArrayList<>();
for(int i = 0; i < 3; i++) {
Student student=new Student();
student.setName("name"+i);
student.setAge(i);
list.add(student);
}
ClassRoom classRoom=new ClassRoom();
classRoom.setStudentList(list);
ObjectMapper objectMapper=new ObjectMapper();
String s=objectMapper.writeValueAsString(classRoom);
System.out.println(s);
}
得到输出:
{"studentList":[{"name":"name0","age":0},{"name":"name1","age":1},{"name":"name2","age":2}]}
反序列化同上
- 另外还可以将字符串和列表进行转换
String s = objectMapper.writeValueAsString(list);
List<Student> list1 = objectMapper.readValue(s, new TypeReference<List<Student>>(){});
3.整数与enum之间的序列化和反序列化
- 先定义一个enum类型并添加在Student类中
public enum ScoreEnum {
A,
B,
C;
}
@Getter
@Setter
public class Student {
private String name;
private int age;
private ScoreEnum scoreEnum;
}
- 直接进行序列化
public static void main(String[] args) throws IOException {
Student student = new Student();
student.setName("abc");
student.setScoreEnum(ScoreEnum.A);
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(student);
System.out.println(s);
}
得到输出:
{"name":"abc","age":0,"scoreEnum":"A"}
可以发现默认是解析成字符串的
当我们需要解析的字符串是一个整数时
ObjectMapper objectMapper = new ObjectMapper();
String s = "{\"name\":\"abc\",\"age\":0,\"scoreEnum\":1}";
Student student = objectMapper.readValue(s, Student.class);
System.out.println(student);
得到输出:
Student(name=abc, age=0, scoreEnum=B)
- 将整数转换为enum
显然jackson默认用的是enum的ordinal()来解析,如果我们希望自定义整数与enum的对应关系,就需要使用 @JsonCreator 注解
@AllArgsConstructor
public enum ScoreEnum {
A(1),
B(2),
C(3);
private int num;
@JsonCreator
public static ScoreEnum fromValue(int num) {
for (ScoreEnum enumValue : values()) {
if (enumValue.getNum() == num) {
return enumValue;
}
}
return null;
}
public int getNum() {
return num;
}
}
运行之前的代码得到:
Student(name=abc, age=0, scoreEnum=A)
4.将enum转换为整数
如果要将enum转换为整数而不是字符串也需要额外使用注解 @JsonValue
@AllArgsConstructor
public enum ScoreEnum {
A(1),
B(2),
C(3);
private int num;
@JsonCreator
public static ScoreEnum fromValue(int num) {
for (ScoreEnum enumValue : values()) {
if (enumValue.getNum() == num) {
return enumValue;
}
}
return null;
}
@JsonValue
public int getNum() {
return num;
}
}
这样就完成了整数与enum的互转
4.不同名参数的转换
如果要将一个参数序列化或反序列化为不同名的结果,可以使用 @JsonProperty
将上面例子中的ClassRoom修改为如下所示
@Setter
@Getter
public class ClassRoom {
@JsonProperty("newList")
List<Student> studentList;
}
运行之前的例子,得到输出:
{"newList":[{"name":"name0","age":0},{"name":"name1","age":1},{"name":"name2","age":2}]}
5.ObjectMapper的一些配置
通过以下设置会在序列化和反序列化时忽略无法解析和为空的字段
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);