一、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
序列化 : 是指将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序列化时的特性定义
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |