文章目录
一、FastJson介绍
FastJson 是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将JavaBean序列化为JSON字符串
,也可以从JSON字符串反序列化到JavaBean
。
Fastjson 的优点
- 速度快
- fastjson相对其他JSON库的特点是快
- 从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越
- 使用广泛
- fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受
- 在2012年被开源中国评选为最受欢迎的国产开源软件之一
- 测试完备
- fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个
- 每次发布都会进行回归测试,保证质量稳定
- 使用简单
- fastjson的API十分简洁
- 功能完备
- 支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展
二、FastJson序列化API
序列化 : 是指将Java对象转成json格式字符串的过程。
JavaBean对象、List集合对象、Map集合为应用最广泛的。
1、序列化Java对象
@Test
public void objectToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"address":"北京市","age":20,"id":1,"name":"张三"}
2、序列化List集合
@Test
public void listToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
Student student2 = new Student();
student2.setId(2);
student2.setName("李四");
student2.setAge(22);
student2.setAddress("天津市");
List<Student> list = new ArrayList<>();
list.add(student);
list.add(student2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}
结果:
[{"address":"北京市","age":20,"id":1,"name":"张三"},{"address":"天津市","age":22,"id":2,"name":"李四"}]
3、序列化Map集合
@Test
public void mapToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
Student student2 = new Student();
student2.setId(2);
student2.setName("李四");
student2.setAge(22);
student2.setAddress("天津市");
Map<String,Student> map = new HashMap<>();
map.put("s1",student);
map.put("s2",student2);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
}
结果:
{"s1":{"address":"北京市","age":20,"id":1,"name":"张三"},"s2":{"address":"天津市","age":22,"id":2,"name":"李四"}}
三、FashJson反序列化API
1、反序列化Java对象
@Test
public void jsonToObject(){
String jsonString = "{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"}";
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println(student);
}
结果:
Student(id=1, name=张三, age=20, address=北京市)
2、反序列化List集合
@Test
public void jsonToList(){
String jsonString = "[{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"},{\"address\":\"天津市\",\"age\":22,\"id\":2,\"name\":\"李四\"}]";
List<Student> list = JSON.parseArray(jsonString,Student.class);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
结果:
Student(id=1, name=张三, age=20, address=北京市)
Student(id=2, name=李四, age=22, address=天津市)
3、反序列化Map集合(带泛型)
- new TypeReference<T>(){}
- TypeReference构造方法是protected,不可直接访问(不是子类,也不再同一个包下)
- new 对象(){} 是
匿名内部类
,TypeReference的子类,new的是子类而非TypeReference
@Test
public void jsonToMap(){
String jsonString = "{\"s1\":{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"},\"s2\":{\"address\":\"天津市\",\"age\":22,\"id\":2,\"name\":\"李四\"}}";
Map<String,Student> parse = JSON.parseObject(jsonString,new TypeReference<Map<String,Student>>(){});
for(String s : parse.keySet()){
System.out.println(s + ":::"+parse.get(s));
}
}
结果:
s1:::Student(id=1, name=张三, age=20, address=北京市)
s2:::Student(id=2, name=李四, age=22, address=天津市)
四、SerializerFeature枚举
1、默认字段为null的不显示
@Test
public void testDefault(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
student.setDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"age":20,"date":1684245428959,"id":1}
- 枚举常量
WriteMapNullValue
序列化为null的字段
@Test
public void testSerializerFeature(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
}
结果:
{"address":null,"age":20,"id":1,"name":null}
- 枚举常量WriteNullStringAsEmpty字段为null,序列化为""
@Test
public void testSerializerFeature(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
}
结果:
{"address":"","age":20,"id":1,"name":""}
- 枚举常量WriteNullNumberAsZero字段为null,序列化为0
- 枚举常量WriteNullBooleanAsFalse字段值为null,输出false
2、格式化
- 枚举常量
PrettyFormat
格式化输出(为了好看) - 枚举常量
WriteDateUseDateFormat
格式化日期格式
@Test
public void testSerializerFeature() {
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setDate(new Date());
String jsonString = JSON.toJSONString(student,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.PrettyFormat);
System.out.println(jsonString);
}
结果:
{
"age":20,
"date":"2023-05-16 21:54:20",
"id":1,
"name":"张三"
}
五、@JSonField注解
该注解作用于方法上
、字段上
和参数上
。可在序列化和反序列化时进行特性功能定制
。
- 注解属性 : name序列化后的名字
- 注解属性 : ordinal序列化后的顺序
- 注解属性 : format序列化后的格式
- 注解属性 : serialize是否序列化该字段
- 注解属性 : deserialize是否反序列化该字段
- 注解属性 : serialzeFeatures 序列化时的特性定义
@Data
public class Student {
@JSONField(ordinal = 2)
private Integer id;
@JSONField(name = "studentName")
private String name;
@JSONField(ordinal = 1)
private Integer age;
@JSONField(serialize = false)
private String address;
@JSONField(format = "YYYY-MM-dd")
private Date date;
}
@Test
public void test1() {
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
student.setDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"date":"2023-05-16","studentName":"张三","age":20,"id":1}
六、@ JSonType注解
该注解作用于类上
,对该类的字段进行序列化和反序列化时的特性功能定制。
- 注解属性 : includes要被序列化的字段
- 注解属性 : orders序列化后的顺序
- 注解属性 : serialzeFeatures序列化时的特性定义